Miscellaneous > Programming & Networking
A simple C query.
Calum:
okay, well i've been meaning to ask this for a good few months now, and it's not detrimental, so don't worry about it or anything, well, for a start off, here's this hello world program i wrote. It was the first thing i wrote off the top of my head, not in any way copied out of a book. We all have to start somewhere, i told myself:
--- Code: ---
--- End code ---
Now, to me, that looks okay, but when i compile it, look what happens:
--- Code: ---
--- End code ---
why is this? what are these two errors? i don't know what they mean, and even more mystifying to me, the program runs exactly as expected, printing Hello, World! and then a newline. So what's the deal?
thanks, but as i say, it's not important, i'm just nosey! :D
choasforages:
ummm, actally the code would go like this
#include <stdio.h>
int main()
{
printf("hello world");
return 0;
}
always have your includes at the top, and not in the body of the prog, you must also to return in everyfunction. somebody with more experiance correct this
Calum:
thanks! so, if one were to compile the above, then it would get no errors? i will try this later..
DC:
quote:Originally posted by Calum:
hello.c:2: warning: return type defaults to `int'
hello.c:2: warning: function declaration isn't a prototype
hello.c: In function `main':
hello.c:5: warning: control reaches end of non-void function
--- End quote ---
Choasforages is right, but for a more detailed note on what it actually says:
warning: return type defaults to `int'
Your 'main' function doesn't have a return type. This must be something. 'void' is for functions that don't have to return anything, int and char are what it says. The standard for the main() function is int, most programs use that (it's standard to use that for error codes, where '0' is normal execution).
GCC defaults to 'int'
warning: control reaches end of non-void function
main() is now a function that returns an integer (it isn't 'void', which means it has to return something), but it reached the end without seeing a 'return'.
warning: function declaration isn't a prototype
hello.c: In function `main':
This probably has to do with #include being in the body of the program. #include copies everything in the given header file in the source (the preprocessor does this), and so all the functions in the include file land *in* the main() function which isn't good.
Hope that helped cure your noseyness ;)
foobar:
More noseyness:
What if you do return 1, or 2, or even 74836 ??
Navigation
[0] Message Index
[#] Next page
Go to full version