According to the coding below:-
Are there any unexpected syntax error will occur since there's no return value to caller function during compilation?Code:main() { printf( "Welcome to C Programming.\n" ); }
According to the coding below:-
Are there any unexpected syntax error will occur since there's no return value to caller function during compilation?Code:main() { printf( "Welcome to C Programming.\n" ); }
This should be the first thing you ever learn about C/C++.
What you posted may compile and function ok but it is against the standard.Code:int main(){ return 0; }
If I told you what syntax errors to expect, then there would not be an unexpected syntax errorOriginally Posted by adnilsah017
Anyway, there should not be any syntax errors in that snippet. However, you should:
- #include <stdio.h> for printf. The problems with not doing this may not appear in this example, but better safe than sorry.
- Explicitly state the return type of main as int instead of relying on it defaulting to int. This defaulting to int feature is obsolescent.
- Explicitly return 0; from the end of main, especially if you are not compiling with respect to C99 (in which case it would be optional).
You may also wish to explicitly state that the parameter list is empty with the void keyword.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i have read through book that give me example of the program as above.... but after i compile it have an error and ask me to return value.
why it happen?
Your book is probably old. If the example really did not #include <stdio.h>, or if the book did not inform you that you should include a certain set of headers with the examples, then your book would also be wrong.Originally Posted by adnilsah017
Because your compiler is looking out for youOriginally Posted by adnilsah017
Well, I don't know why that happened: I don't recall that a diagnostic is required, and I don't think there's undefined behaviour as long as the return value is not used. I could be wrong, but it does not matter: just fix it.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)