What is the different between those main()? I quite confusing about it
- void main (void)
- void main ()
- int main (void)
- int main ()
What is the function of void and int when starting, haha
Thank You
What is the different between those main()? I quite confusing about it
- void main (void)
- void main ()
- int main (void)
- int main ()
What is the function of void and int when starting, haha
Thank You
the biggest thing that stands out to me is that void main is just simply wrong. int main is the only correct way to define main.
apart from that, what do you think the difference is?
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
"void main()" is simply not valid C, in either of the forms you mentioned. However, void indicates a function that returns no value. Yes, I know some books tell you main() can return void. Some compilers even support that. But it is not guaranteed with all compilers. Why? Because the C standard says all permitted forms of main() return int.
The int in "int main()" means the function returns an integer to the caller (which in the case of main() means that the calling environment can retrieve that integral value, and use it as an error code or something). The way that value is retrieved, after main() returns, is implementation defined (i.e. it depends on your compiler, operating system, etc).
When defining (aka implementing) a main() function, as in
the () on main() means it accepts no parameters. The 0 on the return statement is the value that the caller can retrieve. In this context, int main() and int main(void) mean the same thing - that the function accepts no arguments.Code:#include <stdio.h> int main() /* definition of main() */ { printf("Hello World\n"); return 0; }
If you were declaring main() rather than defining (aka implementing) it, then you might do
orCode:int main(); /* Invalid declaration of main() */
In this case (when declaring but not defining main()) the () means that the function can accept a variable number of parameters, while (void) still means it accepts no arguments. Since main() is not allowed to accept a variable number of arguments (again, that's the law, as laid down by standard) only the second declaration here is valid C.Code:int main(void); /* Valid declaration of main() - has to be defined elsewhere */
In practice, it is very unusual for programmers (other than people who implement compilers and the associated libraries) to separately declare main(), since it is very rare that a programmer needs to write code that calls main() directly (and most programmers who think they need to are not understanding what they are actually doing).
Thanks Elkvis, grumpy,
i am new beginner from c programming, i doesnt know so much haha,
mostly we use main() in the program and didnt care about int main() and other, because lecturer said main() is default, so that we use it haha. mostly i see programming book, they always use int main(). and doesnt know what function,
i doesnt know why, mostly the book put void main()/int main() in coding there, i tap the code and run, dk why the program auto end.
what is the function of return 0.
thanks
grumpy the analysis that you have done about main according to its defining and declarations I think is the same for the other functions?
Code:int foo( ); // Declaration of function of which has a variable number of parameters
I think you can denote that a function has a variable number of arguments with ellipsis.
http://publications.gbdirect.co.uk/c...r9/stdarg.html
there is a difference between declaration and definition.
consider this declaration:
it declares a function that returns an int and accepts an unknown number of arguments.Code:int foo();
now consider this function definition:
this defines a function that returns an int and accepts exactly zero arguments.Code:int foo() { return 0; }
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
So there is no problem if I declare a function :
and then define it like :Code:int foo(void) ;
but not vice versaCode:int foo() { // ... return some_integer_ value ; }
Code:int foo() ; //declarationorCode:int foo(void) { //... return some_integer_value; }
Code:int foo() ;right?Code:int foo() { // ... return some_integer_value; }
EDIT: Although I checked it with compiler and does not seem to complain even we use a () in declaration and after void in definition.
Last edited by Mr.Lnx; 06-05-2013 at 10:58 AM.
that is correct.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
Last edited by Elkvis; 06-05-2013 at 11:08 AM.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
while its true that the various (void) and () declarations may be acceptable per the standard, it is bad form to have different definitions and declarations of a function (or variable, or anything for that matter). everything should match up exactly. otherwise it is sloppy programming.
Ok guys. Thank you.
Yes, except for the fact that the standard specifies the acceptable forms of main(). The only constraints on acceptable forms for other functions is syntactic and semantic correctness.
As Epy points out, C++ is a little different. A function declared with no arguments accepts no arguments, rather than a variable argument list.
In ANSI C, main() always returns an integer to the environment, which can be 0 or EXIT_SUCCESS (almost always 0), to indicate that the program completed normally,, and EXIT_FAILURE to indicate that it encountered some sort of error.
However many environments simply throw that integer away, so a lot of compilers will accept void main(). This is not portable, a really strict compiler won't accept it.
main takes two parameters, argc and argv, which are the number of comm and line arguments passed to the program, and the arguments themselves. However it's not uncommon not to use any arguments. So main can be declared as void or with an empty parameter list to indicate this. There's a technical difference between the two, but it doesn't really matter.
I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
Visit my website for lots of associated C programming resources.
https://github.com/MalcolmMcLean