Possible Duplicate:
What is the difference between a definition and a declaration?
I am confuse about the function declare on head file. does this is same as java, declare function on one file,in that that file has function also has method.
does in C, make the function and method in two different files?
here is a example:
song.h
typedef struct {
int lengthInSeconds;
int yearRecorded;
} Song;
Song make_song (int seconds, int year);
void display_song (Song theSong);
song.c
#include <stdio.h>
#include "song.h"
Song make_song (int seconds, int year)
{
Song newSong;
newSong.lengthInSeconds = seconds;
newSong.yearRecorded = year;
display_song (newSong);
return newSong;
}
void display_song (Song theSong)
{
printf ("the song is %i seconds long ", theSong.lengthInSeconds);
printf ("and was made in %i\n", theSong.yearRecorded);
}
Can I write those two functions on just one file?
Thanks. I am new to C.
Regards.
Ben.
Functions in C exist in two forms
A declaration is best visualized as a promise of a later function definition. It’s declaring what the function will look like but not telling you what it actually does. It’s common practice to add the declaration of functions shared amongst files in a header.
Now other files can use
make_songby including Song.h. They need to know nothing about it’s implementation just what it’s signature is. The definitions are typically placed in a .c file of the same name as the .h that defines them.This is the standard convention for functions shared amongst files but by no means the only way to define functions. It’s possible for functions in C to