SeanMc wrote:
Code: Select all
int main()
{
printf ("Hello, <true name censored>!\n");
getchar ();
}
And yes, I know about the return 0 function, I didn't bother because if we're not giving a function any arguments, it's redundant.
main() must return a value if your C compiler operates per the C standard from 1989 (AKA ANSI C). The language standard from 1999 lifts this requirement and main() that executes until its end without return is equivalent to returning 0.
Further, the C standard states that unless other forms of definitions of main() are supported by the compiler, main() must come in one of two forms:
or
I'm not saying this to confuse you or deliberately make your life harder because I'm in some evil mood. I'm saying this because most tutorials (and even some books purporting to be teaching C) are written without taking the language standard seriously and teach bad things or explain things incorrectly or give the reader an impression that C is a lax language and you can bend it any way you wish. Why is it important you might wonder if your program appears to work as you expect?
Well, the reason is that the language by its design (and as reflected in its standard) comes with a number of things that aren't defined or are left to be decided and defined by the compiler writer in different ways. If your C code has things whose behavior isn't defined neither by the standard nor by your compiler documentation, your code may still compile here and now and it may even work here and now, but should you switch to a different version of the compiler or to a completely different compiler or change its optimization options or move the code to a different architecture (ARM or MIPS or PPC or whatever else is out there besides x86), you will run the risk of your code ceasing to work. There have been many cases. Some are nasty as the broken code opens up security holes. Some people who don't get the language and don't care about how its defined and pretend to know better file stupid bugs against gcc and other compilers, demand them fixed and get nothing other than annoying others with their ignorance and arrogance and wasting everyone's time.
IOW, just because your C (or C++) program works here and now, it doesn't necessarily mean it's correct in the face of the language. C (and C++) isn't a particularly friendly language and its compilers won't tell you of all language-specific bugs in your code, even if you ask the compiler to generate all possible warnings (which are signs of bugs, but some are just annoying overly patronizing false alarms, unfortunately). This means that you should actually learn to write proper code, that is, avoid doing bad things that the language standard tells not to do.
Get yourself K&R 2nd edition. It won't teach you bad things if you read it with attention.