Question:
what is the difference between void main() and main(), if we run the same program what will be the output for ?
Narmada
2008-10-07 02:10:49 UTC
tell me clearly the difference between void main() and main() in c language
Three answers:
2008-10-07 02:24:11 UTC
main() is the the point from the execution begins. But main() itself is a function. So when you write void main() this means that the function return s anything once its called(by os).

There is no difference between void main() and main() in OLD COMPILERS. But in the new C/CPP compilers, you have to specify the return type before the main function or you can use void. Otherwise your program won't compile. such as



//returns nothing OK

void main()



//returns zero OK

int main()

return 0;



//Bad Coding

main()
2008-10-07 02:53:50 UTC
Narmada, here is my answer to make U understand about "The difference between void main() and main()", here are the answers!



AND READ THIS CAREFULLY! PROBABLY THIS ANSWER WILL OPEN YOUR MIND WIDER! ^_^



FAQ > What's the difference between... > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) ???



A very common question is "What's the difference between void main and int main?". This particular FAQ tries to answer that and more, covering other versions of the main() implementation.



The first thing to note is that this is one of those topics that people seem to like to argue over for hours, days and more. Some arguments are valid, some are not, and some are just plain old opinion.



The C and C++ standards differ when it comes to main(), so I'll detail each one separately.



For C



Under C89, main() is acceptable, although it is advisable to use the C99 standard, under which only these are acceptable:



int main ( void )

int main ( int argc, char *argv[] )



Slight variations of the above are acceptable, where int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.



The first option is used when you do not require access to the command line arguments.



The names argc and argv are identifiers that can be changed if you so desire, but sticking to argc/argv is convention.



The return type of main() must always be an int, this allows a return code to be passed to the invoker.



Under C89, the return statement at the end of main() is required, whereas under C99 if no return statement is present, return 0 is implied. However, it is good programming practice to always use a return statement, even if you don't have to.



For C++



The following are acceptable uses:



int main ( int argc, char *argv[] )

int main ()



The first option follows similar conventions to those used by C99.



The second option is used when you do not require access to the command line arguments, and is equivalent to the int main(void) option used by C99.



Again, the return type must always be an int, and the function should return 0; at the end, but it is not required by the standard.



(C) The difference between int main() and int main(void)



A common misconception for C programmers, is to assume that a function prototyped as follows takes no arguments:



int foo();



In fact, this function is deemed to take an unknown number of arguments. Using the keyword void within the brackets is the correct way to tell the compiler that the function takes NO arguments.



What's the deal with void main()



Under regular function calling/returning in C and C++, if your don't ever want to return anything from a function, you define it's return type as void. For example, a function that takes no arguments, and returns nothing can be prototyped as:



void foo(void);



A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int. There are some exceptions where void main() is allowed, but these are on specialised systems only. If you're not sure if you're using one of these specialised systems or not, then the answer is simply no, you're not. If you were, you'd know it.



Be warned that if you post your "void main" code on the forums, you're going to get told to correct it. Responding with "my teacher said it's OK" is no defence; teachers have a bad habit of being wrong. Be safe, and post only standard code, and you'll find people concentrate on answering your other problems, rather than waste time telling you about this type of thing.



But what about int main(int argc, char *argv[], char *envp[])



As an extension to the guaranteed standard, an additional parameter to main() can, on some systems, be used to gain access to the environment variables. This is isn't guaranteed to work on all compilers, so use it with care if you want to keep your code portable.



And finally, this answer gives some more background information as to why void main is bad!!!



Narmanda, please give me 5 stars if my answers help U so much!!! THX! ^_^
?
2016-05-30 19:20:00 UTC
1) the scanner allows you to read text from any object which implements the Readable interface > console, file 2) used for running your program. It is what is called when you first run the program. Doesn't matter where it is as long as it is in a class. 3) println = print line, meaning it prints on a line, then line feed and return print = just print, no line feed or return printf = print formatted ex: System.out.println("hello world"); > hello world > System.out.print("hello "); System.out.print("there."); > hello there. int tmp = 18; System.out.printf("number == %d",tmp); > number == 18


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...