top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What if we dont provide the variables in the printf?

+1 vote
378 views

Say we have program like this

void main()
{
int a=10, b=20;
printf("%d %d");
}

above we are not providing a and b but program compiles and print some junk values.

Any explanation?

posted Jul 28, 2014 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

If you pass less number of arguments to printf than it is expecting, the value "%d" will be read form the CPU registers I think (since formal arguments are of type register storage class) and if you don't pass anything, printf reads it from where it is expected to read. (In this case, the registers).

So it can be anything, its undefined, just a random memory value.

Anyone who wants to share their views on this??

answer Jul 28, 2014 by Ankush Surelia
Is there any reason behind to keep formal arguments in registers ?
As per the standards, the only storage class specifier that can be applied to the function parameters is register storage class.
Formal arguments are copied from the calling stack into a register.
0 votes

When we use " %d" the compiler internally uses it to access the argument in the stack (argument stack). Ideally compiler determines the offset of the data variable depending on the format specification string ie. (%d, %f, %s ... etc).

Normal Case
Now when we call "printf("%d",a)" then compiler first access the top most element in the argument stack of the printf() which is "%d" and depending on the format string it calculated the offset to the actual data variable in the memory(stack) which is to be printed.

In our case:
Only "%d" is present in the printf() then compiler will calculate the correct offset (which will be the offset to access the integer variable) but as the actual data object is to be printed is not present at that memory location so it will print what ever the contents of that memory location.

answer Aug 21, 2014 by Arshad Khan
Similar Questions
+3 votes

I am using 2.7. I need to print the time in seconds from the epoch with millisecond precision. I have tried many things but have failed.

 from time import time, strftime
 from datetime import datetime, time

 # write date, time, then seconds from epoch
 self.logfile.write('%st'%(strftime("%Y-%m-%d",)))
 self.logfile.write('%st'%(now.strftime("%H:%M:%S",)))
 self.logfile.write('%st'%(now.time()))

What am i doing wrong? What should I be doing here?

0 votes

Is print thread safe? That is, if I have two threads that each call print, say:

print "spam spam spam" # thread 1
print "eggs eggs eggs" # thread 2

I don't care which line prints first, but I do care if the two lines are mixed in together, something like this:

spam spaeggs eggs m seggspams

Does print perform its own locking to prevent this?

+1 vote

I wish to align my output.

An example-

fprintf($fptr1, "%st %stn", $row1[0],$row1[1]);

In each row of output if the strings are wider than the tab setting, the text will indent.

Is there anyway I can formulate the fprinf so that each string will print at a specific place?

...