decleration and defination

Started by Sango, October 12, 2012, 04:41:59 PM

Previous topic - Next topic

Sango

#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..

CommonTater

#1

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. 


frankie

In the prototype you can omit the name of parameters, but their type is mandatory.
so the code
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