-
Which is the right way?
After a recent programming test, that my friend got back, my teacher counted his code wrong because he used this:
Code:
int main()
{
//code here
return 0;
}
instead of
Code:
void main()
{
//code here
}
My friend tried to argue that the int way was better, and more standard, but the teacher said no. What do you guys have to say to this? Which is better?
-
I would say that int is correct. mainCRTStartup() expects an int return value as well.
Z.
-
cool, thanks for the input. other opinions are welcome too.
if anyone has any credible source too, that would be great!
thanks guys
-
Up to and including Ansi 89 C this:
Code:
main(){
//code here
return 0;
}
was guaranteed to work
because main was implicitly defined as int - C expected int in other words. This is because in the unix environment, processes are usually expected, but not required, to return a value on exit.
In C99 this is not true - you have to explicitly declare main, and there is NOTHING about void main(void). If he/she were doing this correctly which he/she is not doing:
the function main() has to be typed, the arguments also have to be typed.
void main()
is flat wrong in C99.
-
Thanks. How about for c++?
-
Jim, give me your brain.... "Bbbbbrrraaaaiiiinnnnsssss" =).
Z.
-
The real ANSI main is:
int main(int argc, char* argv[])
But compilers allow
void main()
and
int main()
because sometimes you don't need a return value or the program arguments. But I learned that programming teachers aren't always right - a friend of mine is in a class where the teacher must be really terrible.
-
On Windows, it stores the return value in EAX - never had a chance to test on Unix. Jim, any clue?
This means that if you declare a function as void, there's no intrinsic problem other than you'll get a random return value.
-
i see, well, there is no convincing this guy otherwise anyway. thanks for the info.
-
CB - For what its worth - I was just qutoting the standard.
It defines:
what is required and has to be supported.
what is optional but has to be supported.
That's all.
Using your point of view the real C99 ANSI standard might be something interesting like this:
Code:
int main(int argc, char *argv[], char *envp)
(I made this example up, C99 doesn't provide required support for the envp pointer, but most do allow it)
not because you need any of them but because they are available. The C99 ANSI standard lists these args as optional, with datatype for arguments required. It doesn't say which one you have to use.
Therefore:
void is okay.
meets the standard. Even if it's ugly.
ANSI C is a moving target. :D It changes. Currently the above code compiles with my C99 ANSI-compliant compiler on HPUX. It is marginal practice, I think, but legal.
Unfortunately, I'm not making this up, I've got the ANSI C99 standards (in an appendix) in a book on my desk. Somewhere. Under the paper.
You may want to consider common good practice separate from all ANSI standards. You use good practice. The ANSI standard is often less. Way less.
You don't like setjmp() and longjmp(). But they are part of the standard, too.
-
I think MIPSpro gives a warning about it, but it works. Will have to check...
-
jim: wouldn't have thought you remember the jmp things :)
park: GNU C gives a warning, I tried (main return value should be int)
-
sorry, this is probably stupid, but what exactly is "C99"
-
It would probably be the 1999 version of the ANSI C-standard...
-
ahhhh, makes sense, doesn't it? :D
Well, thanks again