473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

amazing result!

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
7 1589
weaknessforcats
9,208 Expert Mod 8TB
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
JosAH
11,448 Expert 8TB
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
Silent1Mezzo
208 100+
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
ekin
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
9,208 Expert Mod 8TB
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
ekin
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
ekin
8
maybe there is something with complier.I tried in tc.But it produced the same result.
Jun 19 '07 #8

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

Similar topics

1
by: Peter Young | last post by:
I'm seeing a problem with IE running on OS-X. When the user logs in to the website (ASP/IIS5), a Session is established properly, but somewhere along the line, a new Session is created and...
2
by: James | last post by:
Are there any style elements that work on select elements on Apple/IE? I want colored text, colored background, or just about anything colored in a select drop-down but can't seem to make it work...
4
by: Peter Larsson | last post by:
Hello everybody, I'm developing a system for a company that has production at two different locations. The production is stored in an Access database at each location, and I need to find a way...
3
by: Roy Osherove | last post by:
Hi folks. I have an ASP.Net application that runs a .Net dll that uses WMI and ADSI(both managed) to connect to a given IIS root and search through it. When not using the ASP.Net client, but...
10
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
6
Atran
by: Atran | last post by:
Hello, you can see an "amazing math trick" on video. Just enter this link: http://www.metacafe.com/watch/308492/amazing_math_trick/
0
by: nokia | last post by:
Here you can find lots of amazing photos http://www.flixya.com/photos/u/superphoto Also you can select useful things for yourself http://popotj.cn/
1
by: yuva | last post by:
"Thank you for this amazing program. Within a weekend, my team increased with 50 new people joining. Every time I logged on there was a new affiliate. Absolutely amazing. " S. Horvath SOUTH AFRICA...
0
by: yokia | last post by:
You can find amazing pictuers ,interesting and lovely . Give you the color of happiness.. http://www.flixya.com/photos/u/superphoto/b You can also breath fresh life at home...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.