This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 7 years ago.
I study C both from textbooks and websites. Somewhere the sample codes begin the program execution by "void main" and somewhere they begin the function with "int main". What's the difference, really? They both give the same result. Is there anything that makes them slightly different? If so, please mention what does "void" does, and what "int" does.
The int main() and the void main() all do the same, which is process the main process of the program.
void main() it means that the functions main() does not return a value.
The difference is int main() is the only right way of calling it, since every program, when called, returns an "error message" that the OS interprets, in which case, closing the program with a return 0; tells the process calling your program that it ended without a problem.
The ANSI standardization of C says that the main function must return an integer. But if you program embedded systems, for instance, then you will use void main. Please se this question for elaboration: What should main() return in C and C++?
Related
This question already has answers here:
Is it better to use C void arguments "void foo(void)" or not "void foo()"? [duplicate]
(6 answers)
Closed 6 months ago.
So I get that a void function won't return a value, while a int one for example will return an integer. So void main(){} doesnt return, but int main(){return 0;} will return.
My question is, what is the difference between these 3 functions. I know the first one wont return a value, the second one will return an integer. But how about the third one? I know it returns an integer because I've tested it, so what does that (void) does? Why is is there?
void main(){}
int main(){return 0;};
int main(void){return 0;};
I'm a begginer so sorry if it sounded confusing... Thanks in advance!!
(The question is about the programming language C)
Between the parenthesis are the parameters to the function.
In a function definition, the proper way to designate a function that takes no parameters is to simply specify void for the parameter list.
An empty parameter list (in a definition) is also a way of saying the function takes no parameters, however this syntax is considered deprecated and should not be used in new programs.
This question already has answers here:
Why does the main function work with no return value?
(2 answers)
What should main() return in C and C++?
(19 answers)
Closed 2 years ago.
Read all this to understand my problem: In my collage test there was a question like this:
What is the error in this code:
#include<stdio.h>
int main(){
printf("Hello World");
}
Answer of above code: compiling Error
But This is what I got when I try it practically:
As per books we have to return 0; when we use int main()
can any one give perfect or understandable Explanation.
Absence of return statement will never raise compiling error. Especially for main function, that handles differently. If it has type int but have doesn't return anything, a program acts like main returns 0.
Theoretically, it can be compiler-dependent, but in you case, it's likely an error in test.
You should return 0 but not doing that is not a compiling error.
UPD: as #EricPostpischil mentioned in comments, compilers don't raise an error because they have a mode set by default that allows to compile non-standard code, so question in your test is incomplete. It has to specify compiler you are using etc.
This question already has answers here:
Can I omit return from main in C? [duplicate]
(2 answers)
Why do C standards allow you not to return a value from a function?
(5 answers)
What was the rationale for making `return 0` at the end of `main` optional?
(2 answers)
What should main() return in C and C++?
(19 answers)
Closed 3 years ago.
Which is a good practice of int main() declaration in C?
int main(){
stuff;
return 0;
}
or
int main(){
stuff;
}
I have tried searching on the internet and most of them are unclear with some mentioning about compiler stuff. I know that a function should return something. However, both works perfectly normal on my computer. Any help on this topic will be greatly appreciated.
Because many programmers used the second style, causing unspecified exit status to be reported to the system, the C Standard committee decided to make main return 0 implicitly if control leaves its body without a return statement. This behavior is mandated by the C Standard C99 and later versions. As a consequence, return 0; can be omitted by it is better IMHO to still make it explicit.
Note however that it is also considered good style to indent the statements in the body of functions:
int main() {
stuff;
return 0;
}
Note also that the C Standard documents 2 possible prototypes for main:
int main(void);
and
int main(int argc, char *argv[]);
or equivalent variants:
int main(int argc, char **argv[]);
int main(const int argc, char ** const argv);
etc.
Omitting the argument list as you wrote in both examples is supported and would be equivalent to int main(void) in C++, but is not exactly equivalent in C: It means the argument list is unspecified, so the compiler cannot check the arguments passed to main if it encounters a call to main in the program, not can it perform the appropriate conversions.
In this case, it does not matter since the main functions in the examples do not use their arguments and indeed seems more consistent than int main(void) since arguments are indeed passed to it by the startup code.
Both will work, because main is special. There's no difference in the compiled code. The standard guarantees that main returns 0 if it terminates without an explicit return value.
But it's better to be explicit and return 0.
For skeptics, an excerpt from the C standard clause 5.1.2.2.3 (Program termination):
a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.
Normally it is not allowed for the control flow to reach the end of a non-void function without returning something. The main function is handled differently.Refer the below document page 59 & 62
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2960.pdf:
If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;
This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 6 years ago.
I just wanna know what happens internally when we return a value. If we dont return a value what are the consequences?
#include <stdio.h>
int main() {
printf("Hello world");
return 0; //what is use of this?
}
it can be evaluated by the system to determine if a program failed to run (and why it failed)
I think perhaps you have two questions:
Why does main() return int and not void?
This question has already been answered in many forms. Basically: for the program that called it (a shell, perhaps, or your IDE), so that it can distinguish between success and various modes of failure.
What happens if you forget to return a value from main()?
In the first standardized version of C (C89) there was nothing special about main() in this respect. If you forgot to return a value, the behavior was undefined. In practice, most compilers will act as if you had put return 0;.
Since C99, if you do not return a value from main() explicitly, then main() is defined to return 0. main() is special in this way; other functions do not implicitly return anything.
As Weather Vane said in the comments, your program can't not return a value (on platforms where "return value" is a meaningful concept). On Linux, execve() and similar functions return an int that is the return value of the program. If a program were ill-behaved and did not intend to return a value, the return value would probably just be some arbitrary value (but it's possible that on another platform such a program would crash).
Not every program or function returns a value. In c, we use return 0 in order to give back control to the operating system when the program is finished running and also to make sure that the program runs smoothly.
Hope that helps. Cheers
I saw other questions about having two main()'s in C program:
I'm using CodeBlocks
But please consider this program:
void main()
{
void main()
{
printf("hello!");
}
printf("World!");
}
prints only "World!".
I'm actually writing these kind of code pieces to check what's happening under the hood.
I somehow get the doubts( as i get errors and unexpected behaviours in my programs ) by writing weird codes but I'm unable to know why they happened
Is there any reference to C language that i can refer to?
Thanks in Advance.
C: Can there be two main() functions in a program?
No.
And the code you posted is not valid C anyway, there are no nesting functions in C.
Standard C and C++ do not support nested functions, but:
GCC supports nested functions in C, as a language extension.
The D language, which is C-related, has nested functions.
and CodeBlocks use GCC compiler only, hence you're are not getting any error.
For the question
about having two main() in C program
No you can't, this is how compiler interprets where to start executing the program. It will take one of the main as local.
Also,
you are not getting "hello" printed
because when compiler starts executing your first main() function, it takes the second main() as local, and because you haven't called the second main(), the string doesn't get printed.
No, there can't be two main functions in C, or in any other programming language. The code you posted is not a valid C program. It will NOT compile.
Yes, Having two main functions in C is possible. It could be done by messing up the compiler using macros/pre-defined functions. Here is the code snippet,
#include <stdio.h>
void main()
{
printf("Inside 1st main\n");
func1();
}
#define main func1
void main()
{
printf("Inside 2nd main\n");
}
Keep the preprocessor be as it is. Else you would get error.