|
-
Nov 19th, 2001, 07:06 PM
#1
Thread Starter
PowerPoster
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?
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Nov 19th, 2001, 07:13 PM
#2
I would say that int is correct. mainCRTStartup() expects an int return value as well.
Z.
-
Nov 19th, 2001, 07:18 PM
#3
Thread Starter
PowerPoster
cool, thanks for the input. other opinions are welcome too.
if anyone has any credible source too, that would be great!
thanks guys
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Nov 20th, 2001, 07:13 AM
#4
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.
-
Nov 20th, 2001, 07:55 AM
#5
Thread Starter
PowerPoster
Thanks. How about for c++?
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Nov 20th, 2001, 07:57 AM
#6
Jim, give me your brain.... "Bbbbbrrraaaaiiiinnnnsssss" =).
Z.
-
Nov 20th, 2001, 10:05 AM
#7
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 20th, 2001, 02:34 PM
#8
Monday Morning Lunatic
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 refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 20th, 2001, 04:16 PM
#9
Thread Starter
PowerPoster
i see, well, there is no convincing this guy otherwise anyway. thanks for the info.
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Nov 21st, 2001, 05:16 PM
#10
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. 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.
-
Nov 21st, 2001, 05:47 PM
#11
Monday Morning Lunatic
I think MIPSpro gives a warning about it, but it works. Will have to check...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 22nd, 2001, 09:30 AM
#12
jim: wouldn't have thought you remember the jmp things 
park: GNU C gives a warning, I tried (main return value should be int)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 22nd, 2001, 09:47 AM
#13
Thread Starter
PowerPoster
sorry, this is probably stupid, but what exactly is "C99"
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Nov 22nd, 2001, 09:55 AM
#14
It would probably be the 1999 version of the ANSI C-standard...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 22nd, 2001, 09:58 AM
#15
Thread Starter
PowerPoster
ahhhh, makes sense, doesn't it?
Well, thanks again
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|