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);
}
}
Related
This question already has answers here:
About Tentative definition
(2 answers)
Why does redefining a static global variable give a compile-time error when redefining a global variable does not?
(1 answer)
Closed 6 years ago.
I create a simple test.c file:
#include <stdio.h>
int a;
int a = 100;
void printA(void)
{
printf("a is %d\n", a);
}
Compile it to generate object file:
$ gcc -c test.c
$
It is OK! But per my understanding, the variable a should be redefined, is it right?
A variable is local to block it is defined.Once code in block is executed variable goes out of scope.In your case firstly a is defined globally.In second case it is defined inside a function.Thus, to compiler both a is different in both cases not the same.
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.
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:
How are arguments evaluated in a function call? [duplicate]
(2 answers)
Sequence points and partial order
(3 answers)
Closed 9 years ago.
in C this works as I expect
void foo(int a, int b ) { printf("%i,%i ",a,b ); }
int main()
{
int i=1;
foo(i++,i);
foo(i++,i);
foo(i++,i);
foo(i++,i);
return(0);
}
output: 1,2 2,3 3,4 4,5
Now, the following does not work as I would have guessed:
void foo(int a, int b ) { printf("%i,%i ",a,b ); }
int main()
{
int a[] = { 100,200,300,400,500,600,700 };
int i=0;
foo(a[i++],a[i]);
foo(a[i++],a[i]);
foo(a[i++],a[i]);
foo(a[i++],a[i]);
return(0);
}
It returns
100,100 200,200 300,300 400,400
Following the logic of the previous example, I would have expected
100,200 200,300 300,400 400,500
My first suspicion was that the increment was only called after the function call, however, as the first example shows, i is indeed incremented inside the function call, unless it is used as an index for the array.
I also tried the same using foo(*(a+(sizeof(int)i++)),(a+(sizeof(int)*i))); just to check, and it also acts like the second example.
The compiler is gcc version 4.6.3
My question is, why does it work when I'm using the variable i directly as the function parameter, but not when I use the variable i as an index for an array which is used as the function parameter?
Both of your code invokes undefined behavior. You may get either expected or unexpected result.
Compile your code with -Wall flag. Compiler should give the warning:
[Warning] operation on 'i' may be undefined [-Wsequence-point]
Read the C_FAQ - 3.8. It explains this issue pretty well.
This question already has answers here:
Turbo C++: Why does printf print expected values, when no variables are passed to it?
(5 answers)
Closed 9 years ago.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=11;
clrscr();
printf("%d");
getch();
}
Output=11
How the output is 11 even I am not mentioned the variable name in the printf function.
The 11 is on the stack because of the b variable, and your printf() function is looking on the stack for a value on the stack because that's where variables get passed.
If you add a c=47, you'll probably get 47. But this is undefined behavior.
This is called "undefined behavior", which means that program can do just about anything.
What is actually happening in this case is that both variables and function parameters are put on the stack. Since you aren't passing the parameter that printf is expecting, it ends up pulling something else off the stack, which is your b variable.
But because it is undefined behavior, if you had a different compiler, a different CPU, or even different compile options, such as a higher optimization level, you could get very different results.