+ 1
I don’t understand something of C language
Hi everyone,I’m studying CS at university and I really interested in C&C++,so I read some Programming book with my language but I still confusing about main(),int main(),main(void),void main() and also what’s the job of return 0;.I wish someone could explain me,
6 odpowiedzi
+ 1
Sure!
[1]
int main()
This can be interpreted as a function called "main". It is the entry point to every C/C++ program and it is also known as the "driver function". Every program will have this function so get used to it! Don't worry about what functions are too much, for now let's think of functions as though they're machines. Machines (functions) take in some input to produce some output, like a recycling robot that eats recyclable waste to produce recycled products, or a printer that takes in paper and printing info to print an image on that paper. Functions are similiar in a way, they take in some input (called "arguments") and return some output.
The above can be broken down into a few parts:
int - This is the return type of the function, int here stands for integer (whole number), every function needs a return type! Like the analogy above this is the type of thing we want to output (an integer).
main - This is the name of our function, in the future you'll see that functions can have different names you give them, such as "abc" or "sum" or "Pyae_Sone_Function".
() - This is an empty parameter list. The parameter list defines the set of inputs our function is going to take in, it is a list of input types our function will accept. An empty parameter list means that the function doesn't take any input in C++. In C this means our function can take an *unspecified amount of inputs.
{} - This is the body of the function, what the function does and how it does it is all in here defined by you, the programmer, and when starting C/C++, whatever's in here is the only thing you should really care about to avoid confusion.
Well yeah my program doesn't need any input, but why does it output an integer?
Every main function *needs* to return an int, this int is the "status code" of your program.
...
+ 4
don't worry these things are hard to understand the first time
some say that return 0 within main serves to resolve a compilation error, but it's actually because every function that isn't void needs to return a value, in which case int main returns an integer value 0
we can say that int main () is the C and C ++ main function, so every code of yours must have this function, and every other function created inside the code is called inside main to execute
a void function is a function that will not return any value, just perform a task, so there is no return at the end
I hope I helped a little, if you want you can ask me more :)
+ 3
Similar questions have also been asked before. A search may also have revealed some answers.
+ 1
[2]
int main(void)
Woah what's this? Its a main function that has a "void" in it's parameter list. What is a void anyway? void in C/C++ is another type just like int! void is a type that usually represents "nothing", in this parameter list it means that this function doesn't take any input. It only makes sense to do this in C because an empty parameter list in C is an unspecified parameter list.
void main()
This is a variation of the first thing I mentioned, except that it's return type is void; it doesn't return/output anything. This is not a valid entry point unlike `int main()` (there are exceptions) but I've seen it being used in pseudocode (https://www.webopedia.com/TERM/P/pseudocode.html)
main()
This can be seen 2 ways: it is a function call to main, it is an old C declaration of main (using implicit return type). A function call, using the anology we've been going with, is like starting the machine. The parentheses "()" here is like the parameter list, except that it is called the argument list (the list of inputs we give to our machine), the argument list must match the types of the parameter list in order. Calling the main function is not practical in C/C++ and you won't see it often. The other way I've mentioned is that not writing down the "int" (return type) may still compile in older versions of C. When the compiler sees this, it automatically assumes it is going to return an integer https://www.geeksforgeeks.org/implicit-return-type-int-c-language/.
...
0
[3]
return 0;
Remember what I said about the return type being an integer? You'll usually see this at the end of the main function. The 0 here is the output of the main function. As I've mentioned before, the main function returns the status code of the program. The status code 0 means that the program succeeded without errors, status code 1 usually means your program had a failure. There are other status codes which mean other things and those are decided by your OS. I'll go one step further and tell you that you don't have to "return 0;" in your main function, your compiler is usually smart enough to do this for you.
For now, the body is all you should care about but hey, you can learn all you want and nobody will stop you because there's never too much to learn!
Good luck and have fun in your CS course!
0
Thanks guys,I haven’t any confusing now.