Jump to content
  • Advertisement
Sign in to follow this  
y2jsave

c problem

This topic is 3705 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

in c printf("%c") is giving 8 as the answer. is there any specific reason for it or it's just a garbage value corrected [Edited by - y2jsave on June 26, 2008 12:44:05 PM]

Share this post


Link to post
Share on other sites
Advertisement
Well, it's hard to tell what you mean because you posted incomplete code, but if:
printf("%c", some_var);

Prints '8', then the value of some_var is the ascii code for the character 8, namely 56.

Share this post


Link to post
Share on other sites
It's a garbage value. It's whatever happened to be on the stack at that position at that time.

In conclusion, don't do this! Always pass the right number of type of arguments to printf functions.

Share this post


Link to post
Share on other sites
Well, GNU libc's printf prints nothing: if you do printf("a%cb\n");, it just prints ab and a newline. With Microsofts C library (version 9) it prints a random value (useless).

Share this post


Link to post
Share on other sites
Oh I see. Since he left off the end quote I thought the code was incomplete.

What happens when you call printf is that the first parameter (the format) is on the top of the execution stack. printf examines the string to see what other arguments should have been pushed. It looks up the stack and prints each one in turn - interpreting it according to whatever data type the format string says it was.

Your code essentially tells printf that there is a character variable 1 position up the stack. printf doesn't know any better so it prints this value as if it were a character.

What actually prints is system-dependent. It could be another part of printfs activation record (like part of the frame pointer), or it could be part of another function's activation record.

So basically, yeah, don't do that :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!