Printf arguments

This is a discussion on Printf arguments within the C++ Programming forums, part of the General Programming Boards category; Hi, I just started learning c/c++, and am asking this question out of pure curiosity. Look at this code snippet: ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    13

    Question Printf arguments

    Hi,

    I just started learning c/c++, and am asking this question out of pure curiosity.

    Look at this code snippet:

    #include <stdio.h>

    int main()
    {
    int i=10;
    short j=10;

    unsigned int ui=10;
    unsigned short uj=10;

    printf("%i\n",i);
    printf("%i\n",j);
    printf("%d\n",i);
    printf("%d\n",j);

    printf("%i\n",ui);
    printf("%i\n",uj);
    printf("%d\n",ui);
    printf("%d\n",uj);

    return 0;
    }

    The output of this program is simply 8 "10"s

    My question is this: What is the difference between %i and %d, and how does printf know in each case whether the variable being passed is signed or unsigned or a long or a short integer?

    All I know is somehow it works, but I would be really interested to know where it gets this information from.

    Thanks in advance.

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    if i'm not mistaken [and i might be] the only reason you have %i and %d is for backward compatability. i think they changed it one time or the other, and to provide easy compiling for old code they allowed it. i'm pretty sure they, in and of themselves, are exactly the same. anyone have the source for printf? or perhaps their signedness is opposite, which i doubt...
    hasafraggin shizigishin oppashigger...

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Doubleanti is right in that %d and %i are the same for output (printf), but there are differences for input (scanf).

    However, your code doesn't prove much, since you print everything using %d and %i (ie always signed decimal numbers), so even if you stored a 'negative' value in the unsigned ints (expecting some large unsigned number), it would still come out negative (%d would assume it to be signed).

    In addition, your printing of short values is broken (it should be "%hd" not "%d"), and to print unsigned values, you have "%u" and "%hu" respectively.

    > how does printf know in each case whether the variable being passed is signed or unsigned or a long or a short integer?
    It doesn't, it assumes you've coded the % conversions correctly.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    13
    Thanks, thats made things a bit clearer, like I said. I still have lots to learn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 08:48 PM
  3. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  4. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 05:16 PM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

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