could someone look at this....  | |
January 19th, 2003, 11:23 PM
|
#1 (permalink)
| | Member
Join Date: Oct 2002 Location: Salem, Oregon
Posts: 480
| could someone look at this....
im really new to C still, but i tried to write this program:
#include "main.h"
#include <stdio.h>
main()
{
#include "main.h"
char first[15], last[15];
float weight;
int age;
printf("Please enter your first name: ");
scanf(" %s", first);
printf("Now enter your last name: ");
scanf(" %s", last);
printf(" Now enter your age please: ");
scanf(" %d", &age);
printf(" Now enter your weight: ");
scanf(" %f", &weight);
printf(" %s %s\n", first, last);
printf("Weight: %f", weight);
printf("Age: %d", age);
return 0;
}
the main.h header file contains:
main()
{
printf("Hello");
}
i just read about header files, and im still a little confused. the compiler doesnt like this, and i know its because of me. can anyone help me correct this little program? |
| |
January 20th, 2003, 12:54 AM
|
#2 (permalink)
| | Member
Join Date: Feb 2002
Posts: 161
|
In this case, you don't need the file main.h at all. Just remove it, and delete both '#include "main.h"' lines.
In general, you use header files when you need to access functions or variables which exist in another source file (that is, another .c file). They have other uses, of course, but this is the simplest. So for example, if you wanted a function to print 'Hello', you might create a source file called hello.c, containing the following function:
void PrintHello(void)
{
printf("Hello!\n");
}
You would then create a header file, hello.h, containing the definition of the PrintHello function:
void PrintHello(void);
By including this file from your main source file (say, main.c), you will now be able to use the PrintHello() function, even though it exists in a separate source file. |
| |
January 20th, 2003, 02:11 PM
|
#3 (permalink)
| | Member
Join Date: Oct 2002 Location: Salem, Oregon
Posts: 480
|
ok, thanks. that helps a lot. im not sure if i should ask, but why put void before the name of the function? just wondering, as the book im learning from doesnt talk about that yet  |
| |
January 20th, 2003, 11:25 PM
|
#4 (permalink)
| | Member
Join Date: Feb 2002
Posts: 161
|
You put void in front of the function to indicate that it doesn't return a value to the caller. If you wanted the function to return, say, an int, then you would put an int in front of it in place of the void, and add a statement like 'return 0;'. |
| |
January 22nd, 2003, 12:05 AM
|
#5 (permalink)
| | Member
Join Date: Oct 2002 Location: Salem, Oregon
Posts: 480
|
hmm, a little confusing. thats ok, hopefully ill learn it later on  |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |