Like Tree5Likes
  • 1 Post By laserlight
  • 1 Post By iMalc
  • 1 Post By laserlight
  • 2 Post By iceaway

printf question

This is a discussion on printf question within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main() { short int a=5; printf("%d"+1,a); return 0; } output:d Why is the output so?...

  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
    21,712
    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.
    dennis.cpp likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,304
    Indeed, before I'll explain why it is "d", what were you hoping or thinking the output might be?
    dennis.cpp likes this.
    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
    21,712
    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.
    Salem likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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?
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  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.
    dennis.cpp and ranjit89 like this.

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, 08: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, 02:52 PM
  5. printf question
    By Geko in forum C Programming
    Replies: 1
    Last Post: 01-14-2008, 12:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21