What is the result of the following code?

int main() { int x=5,y=10,z=15; printf("%d %d %d"); return 0; } 

Output: 15 10 5 // In Turbo C 4.5

  3 Garbage values in gcc compiler 

My teacher told me when we define variables like int x = 5, y = 10, z = 15; they are by default accepted as automatic types and stored on the stack. When you try to print 3 integer values ​​without using their names by printf (), it will print these 3 values ​​in LIFO format, as the Turbo C compiler does. But what I think when we define 3 integer variables, they may not stored in places of continuous memory. Therefore, when I try to print 3 integer values ​​without using their names, the compiler will print three values ​​from the top of the stack. the output will have 3 garbage values, as in gcc ..

+4
source share
6 answers

This code simply shows that Turbo C does not optimize the code well and pushes everything on the stack, while gcc is more aggressive and holds it in registers or throws it together, because these three variables have no purpose.

In any case, calling printf with a template that requires three arguments without providing these arguments is an error.

Update:

As an explanation: I assume that printf () will always accept its argument from the stack, as it is a function with a variable argument. Or does anyone know any other calling convention for functions like printf ()? Moreover, I assume that there is no need to push anything else onto the stack, since there are no other variables. Thus, this erroneous call to printf will print everything that is on top of the stack in main (). But there may be other architectures and cause conventions in which my assumptions are not fulfilled.

+10
source

This behavior is undefined.

With an optimizing compiler, these 3 values ​​can be optimized since they are not used. He will print trash.

+4
source

The behavior is undefined, that is, the compiler is free to handle the situation as it sees fit. As for the locale, both Turbo C and gcc do the “right” thing.

+3
source

If there are not enough arguments for the format, the behavior is undefined.

+2
source

Your teacher is mistaken, but not completely.

Variables declared in a function are auto by default, not static .

 int foo(void) { static int x; int y; auto int z; /* ...other code... */ } 

This means that in the function above, y is auto, just like z, even if the auto keyword is not used in its declaration. By the way, the auto keyword is almost never used. Many C (and C) programmers do not even know that auto is a keyword because it is used so rarely.

Being an auto variable usually means that the variable is stored on the software system stack or in registers or some combination thereof. It can be in different places at different times during the execution of a function, and local variables that are in the register are often pushed onto the stack when another function is called. Some local variables can even be optimized, which means that at some point the compiler was able to determine that the specific value of the future variable is no longer required to satisfy the input needs of the future code (or the variable does not change and its value is simply encoded in the instructions). This complicates the use of debuggers on optimized code.

When you take the address of a local variable, the compiler then tries to lock it to a specific address (possibly by storing it on the stack).

When most compilers look at your code, they will see that the names of these local variables are not used after they are declared and may decide that they simply do not store their values ​​anywhere. Even if it stores these values ​​on the stack, it can also push other values ​​on the stack before setting up the printf call. Just as the compiler should not contain the variables you named, it can also create its own temporary variables.

+1
source

In MingW-GCC: 3 garbage values.

In VC ++ 2010: 0, 0 and the value of garbage.

0
source

All Articles