NO

Author Topic: decleration and defination  (Read 2344 times)

Sango

  • Guest
decleration and defination
« on: October 12, 2012, 04:41:59 PM »
Code: [Select]
#include<stdio.h>
#include<stdlib.h>

int create_list(int);

struct node
{
int dat;
struct node *link;
} *start;

int main()
{
int data;
printf("enter data to be inserted: ");
scanf("%d",&data);
create_list(data);
//start==NULL? printf("no element present in the list\n"): printf("elements r there\n");
}

create_list(data)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->dat=data;
return EXIT_SUCCESS;
}

am getting
link list.c(25): warning #2099: Missing type specifier; assuming 'int'.
if i replace "create_list(data)" the function definition wit "int create_list(data)" then there is no warning.. the code seem perfect to compile..
My question is that if a function is  declared to be of int then do i have to again mention it to be of int during definition..
« Last Edit: October 12, 2012, 09:07:14 PM by Stefan Pendl »

CommonTater

  • Guest
Re: decleration and defination
« Reply #1 on: October 12, 2012, 06:11:15 PM »
Code: [Select]
create_list(data)
{

Should be int create_list(int data)

The way you had it is how you call the function, not how you define it.  Definitions and declarations must match.
 
Also, if you move the function above main, you don't need the declaration at the top. 

 
« Last Edit: October 12, 2012, 06:13:41 PM by CommonTater »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: decleration and defination
« Reply #2 on: October 12, 2012, 06:21:07 PM »
In the prototype you can omit the name of parameters, but their type is mandatory.
so the code
Code: [Select]
int create_list(int);Is correct.
When you define (instance) the function it's mandatory to specify type returned by function (that you have not specified) and type and name of parameters (to also crosscheck the forward declaration above).
You get a warning and not an error just because when the return type is not specified C assumes that it is an integer, that is legal with original K&R syntax, but is deprecated in C99 and C11. These standards require type specification.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide