Question:
i wanna ask questn regarding c programming related to functns?
2011-01-05 05:14:58 UTC
i have heard dat its better to use intmain rather than using voidmain bcoz of the fact dat ,v can find errors in d programs very easily using return0 used in intmain.....if d program doesnt return zero ....,den v can conclude dat dere r errors in exucting the program.But how will v come 2 know whether the program has returned zero or anyother thing(other than zero)..
pls help me in dis regard.
Four answers:
C B S
2011-01-05 08:10:45 UTC
http://tutorial.newhind.com/example/C++/cppintroduction/
l_town_51
2011-01-05 05:28:47 UTC
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 specialized systems only. If you're not sure if you're using one of these specialized 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 difference; 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.
citizen
2016-12-24 19:06:00 UTC
you may spend 5 factors to invite a query. you get 3 returned in case you chosen a suited answer. You income 2 for answering, yet lose 5 if that's pronounced, so specific, you are able to ask a query interior the respond, yet whilst it is not an answer, and basically a query you will be pronounced and finally end up with 5 much less.
Chris C
2011-01-05 05:27:15 UTC
Actually, it is using standard C that you would define it as:

   int main()

And return a value, so that any other programs accessing this program will be able to retrieve any error codes (if your program returns anything other than zero).





Declaring it as:

   void main()



Is allowed in TurboC and possibly a couple of other compilers (but I don't use all of them, so I don't know of any other ones off the top of my head).





To see what the return value is pretty simple:

In CSH (using Unix flavors): The system variable is '$status' (without the single quotes)



In Windows: You can use the system variable %ERRORLEVEL% (without the single quotes)


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