Thread: printf question

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    printf question

    Code:
    #include<stdio.h>
    int main()
    {
     short int a=5;
     printf("%d"+1,a);
     return 0;
    }
    output:d
    Why is the output so?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why did you try this in the first place? I mean, I can tell you why, but it is a weird question to begin with.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Indeed, before I'll explain why it is "d", what were you hoping or thinking the output might be?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Need to know how the compiler works..why is it so?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ranjit89
    Need to know how the compiler works..why is it so?
    I do not think randomly trying out snippets of C code will tell you how your compiler works. To learn how your compiler works, you should study compiler design. To learn how to program in C, you should work through some book/tutorial that provides a more structured approach to your learning of C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    So nobody will answer it?

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    OK got it..don't bother

  8. #8
    spaghetticode
    Guest
    Okay, now *I* am curious. Despite the senslessness and given the fact that this kind of knowledge won't get me anywhere, would somebody explain the output anyway?

  9. #9
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    When the string literal "%d" is used as an argument to printf, it becomes a pointer to the address of the string. So when +1 is added to it, the pointer adress is incremented by one and points to "d" instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a question on 'printf'
    By xinwu in forum C Programming
    Replies: 2
    Last Post: 12-12-2010, 09:15 AM
  2. quick printf question
    By ashlee in forum C Programming
    Replies: 1
    Last Post: 10-20-2010, 12:57 PM
  3. scanf and printf question
    By mmattson07 in forum C Programming
    Replies: 8
    Last Post: 10-11-2009, 01:31 PM
  4. printf() question
    By John Connor in forum C Programming
    Replies: 3
    Last Post: 02-08-2008, 03:52 PM
  5. printf question
    By Geko in forum C Programming
    Replies: 1
    Last Post: 01-14-2008, 01:24 PM