This question already has answers here:
Implicit int return value of C function
(7 answers)
Closed 7 years ago.
Can someone please explain the behavior of the following code. How come the function message() with return type int is returning the no of characters printed by printf() function without any return statement ?
#include <stdio.h>
int message();
int main() {
int c;
printf("C before:%d\n",c);
c=message();
printf("C after:%d\n",c);
return 0;
}
int message(){
printf("From the message");
}
This is caused by undefined behaviour.
Here's a similar question, and I couldn't put it any better than the second answer does:
That's simply undefined behaviour; if you don't populate the return area [...], it'll have the value last set up through some side-effect in your function.
...which is the value returned by printf.
It's undefined behavior ...
As there is no return set in message(), it will set C to garbage.
It is undefined behavior.
For example, if you compile it with clang and run it, results are different.
gcc -O0 yields the string length, -O2 and -O3 yield 0.
Maybe the return value of printf is put into the same register as message's would be. And message does not reset the register before returning.
Related
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:
warning: return type defaults to ‘int’ [-Wreturn-type]
(4 answers)
Closed 3 years ago.
Here's the code, straight from 'The C programming Language, Second edition'.
#include <stdio.h>
main ()
{
printf("hello, world\n");
}
Here's the GCC error:
user#root:~/bin$ gcc helloworld.c
helloworld.c:2:1: warning: return type defaults to ‘int’ [-Wimplicit- int]
main ()^~~~
edit: I have just realized that the code did, in fact, compile. I just didn't realize it had overwritten the file for another program I compiled. I've edited the question itself, as I think that has more value. (Originally I asked why this program was not compiling)
You haven't included a return type for main. You probably want to return 0 at the end of it, as that is the standard exit code for no error. Revised your code would be
#include <stdio.h>
int main() {
printf("hello, world\n");
return 0;
}
Try to:
Use int main() since int is return type of main.
EDIT:
In C89, the default return type is int. This default was removed in
C99 and compilers are helpful reminding you that your C-style with no
int before main() is out of date.
That is not an error, it's just a warning. Your code does compile, trying running the resulting binary with ./a.out
GCC is just warning you that you didn't specify a return type for main, and that it will automatically return an integer.
Your main signauture is wrong. You should consider reading this answer to get a C11 standardized signature of the main function :
https://stackoverflow.com/a/2108208/8141369
This question already has answers here:
Implicit int return value of C function
(7 answers)
Closed 6 years ago.
I am rather in doubts about getting some problems while coding with a few recursive functions. Now here I am giving a simple code with nested function call,so that I can point to my exact problem.
int main(void)
{
int i;
i=a();
printf("%d\n",i);
return 0;
}
int a()
{
return b();
}
int b()
{
return 9;
}
well,no problem at all,it gives output as 9. But if i redefine function a() as:
int a()
{
b();
int new=0; //not significant
}
Again it producing valid output, .i.e,
9
here, though I removed return keyword I was not getting any compilation error,neither the value of i in the output was wrong...(I expected garbage or something like that).
How these things are handled?
In your second piece of code:
int a()
{
b();
int new=0; //not significant
}
The return type of a() is int. However, in your code, you are not returning anything. This typically results in undefined behavior. So, you may get any value (you said you are getting 9, while using gcc on ideone, I am getting 0). It is undefined behavior, so no-one can say exactly what you'll get.
It will depend on the compiler, as pointed out in the SO Answer, but the compiler isn't obliged to document what happens, nor is it required to be consistent in the returned value. Different calls may return different values.
This question already has answers here:
C function defined as int but having no return statement in the body still compiles
(7 answers)
Closed 8 years ago.
#include <stdio.h>
int func()
{
}
int main(void)
{
printf("%d\n",func());
return 0;
}
the function "func()" is of "int" return type but is not returning anything. When the function is called in the print function, why is it giving an output 0? And why does it compile successfully although the function definition does not agree to the function code?
If you enable warnings you will see a diagnostic. Traditionally, C implicitly allowed all functions to return int. The behavior of the return value is undefined, so it is not guaranteed to be 0. The behavior could change on different compilers or on different platforms or with different optimization flags or even just by adding or removing other unrelated code. The reason you are probably seeing 0 is because you are running unoptimized and whatever previous value happens to be in the register or stack position is 0. This is pure chance, and relying on the behavior will ultimately result in bugs in your code.
This question already has answers here:
C++ global and local variables
(8 answers)
Closed 8 years ago.
If i compile and run the following code, it is printing 0 instead of 10.
#include<stdio.h>
main()
{
int Var=10;
{
char Var=Var;
printf("%d",Var);
}
}
Why this is printing 0 and why not 10 ?
Because in local declaration
char Var=Var;
the right occurrence of Var refers to the local Var, not the upper one. As Alk commented, this is undefined behavior to assign from an uninitialized variable.
So your declaration does not initialize Var at all, i.e. Var contains garbage. In your particular case, that garbage happens to be 0.
BTW, having two homonyms Var in the same function is really bad taste.
As this answer suggests, you should compile with gcc -Wall -Wshadow then you'll get some warnings on your code. (also add -g to get debug information to be able to debug with gdb)
Assuming you are using gcc, you would want to turn on -Wshadow (http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html).
This would pop up an error, at the inner variable having same name as outer. The zero is a random value. It could print any garbage in that place.
try this
#include<stdio.h>
main()
{
int Var1=10;
{
char Var=Var1;
printf("%d",Var);
}
}