By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

amazing result!

P: 8
I really do not understand.look at the code.

Expand|Select|Wrap|Line Numbers
  1. printf("%d,%d,%d\n");
  2.  
there is only a printf in main.but how can this produce a reslut:
2367460,1243068,2147332096

thanks in advance.
Jun 12 '07 #1
Share this Question
Share on Google+
7 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
The three %d's tell printf there are three ints to process as arguments. Unfortunately, the ints were never created so printf displays the contents of memory where printf thinks the ints should be. Result: Garbage.
Jun 12 '07 #2

Expert 10K+
P: 11,448
I really do not understand.look at the code.

Expand|Select|Wrap|Line Numbers
  1. printf("%d,%d,%d\n");
  2.  
there is only a printf in main.but how can this produce a reslut:
2367460,1243068,2147332096

thanks in advance.
That's serendipity; daemons might fly out of your nose instead when you run
that little creep.

kind regards,

Jos
Jun 12 '07 #3

100+
P: 208
That's serendipity; daemons might fly out of your nose instead when you run
that little creep.

kind regards,

Jos
Haha...took me a while to realize the title was result :P ....I wasn't really sure what a reslut was...
Jun 12 '07 #4

P: 8
Thanks friends!

Quote:
the ints were never created so printf displays the contents of memory where printf thinks the ints should be. Result: Garbage

But, where the momory exactly is that should be print?Is there any way i could find ?
Jun 13 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
The ints are passed into printf() as arguments. That means printf() has a copy of the int.

You do one of two ways:
Expand|Select|Wrap|Line Numbers
  1. printf("%d%d%d", 3,4,5);
  2.  
Here the 3,4,5 are copied as arguments. printf() just look in memory (the stack frame) for the three ints. There it will find the copies of the 3, 4 and 5.

Or you could:
Expand|Select|Wrap|Line Numbers
  1. int a = 3;
  2. int b = 4;
  3. int c = 5;
  4. printf("%d%d%d", a,b,c);
  5.  
In this case, the ints (a b c) have been copied to the printf() stack frame so this case is just like the previous one. Here are the copies of a,b and c.

What happens is the printf() function has this prototype:
Expand|Select|Wrap|Line Numbers
  1. size_t printf(const char*, ...);
  2.  
The ellipsis argument (...) means and unspecified number of arguments. What printf() does is go through the first argument looking for % signs. The number of % signs tells printf() how many arguments are represented by the second argument (the ...). The code after each % (like %d) tells print() how big that argument is. With this information, printf() makes a calculation of the address where the argument should be in the stack frame. Then it just displays what is there.

Of course, if you never provided the second argument, the calculation arrives at some location in memory and you are just shown the contents of that memory.

You can read up on the vararg macros if you want to. These are the ones that do that calculating of memory addresses for the ellipsis argument.
Jun 13 '07 #6

P: 8
thanks weaknessforcats! It is very clear. I find a another interesting story.the result of the third %d is awalys changing! different from the first and second %d. very funny.right?is there any relation between the stack?
Jun 14 '07 #7

P: 8
maybe there is something with complier.I tried in tc.But it produced the same result.
Jun 19 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.