124 NOVEMBER 2007 | LINUX FOR YOU | www.
linuxforu.
com
C M Y K
very month, I get lots of mails and queries from
LFY readers.
This column seems to have
become popular, particularly among students.
This month, we’ll cover an interesting question
from...
More
124 NOVEMBER 2007 | LINUX FOR YOU | www.
linuxforu.
com
C M Y K
very month, I get lots of mails and queries from
LFY readers.
This column seems to have
become popular, particularly among students.
This month, we’ll cover an interesting question
from Saravanan Swamy (GCT, Coimbatore).
His
question is: “Why does the following program print 10?”
int i = 10;
printf(“%d”);
Note that printf doesn’t obviously have i as the
matching argument for the format string “%d”.
So, the
program is incorrect and you can reasonably expect the
program to print some garbage value.
But also note that it is
equally possible that you might get 10 as the output.
Why?
To understand this, let us see how the compiler might
transform these statements to a low-level executable code.
Local variables are allocated in the stack frame of a
function.
When the main method is invoked, the storage
space for i is also allocated in the stack frame.
A compiler
should generate code to initialise that value i with 10.
Less