What's the difference between the 'd' and 'i' conversion character?

Discussion in 'C Programming' started by ling, Aug 12, 2003.

  1. ling

    ling Guest

    Hi,

    I'm trying to do fscanf from a file with integers.
    I've tried using both %d and %i hoping that I would figure out their difference.

    When I checked the man pages, the only difference I could tell was

    d: Matches an optionally signed decimal integer ...
    i: Matches an optionally signed integer ...

    What's the difference between an integer and a decimal integer?

    Thanks!!!

    Ling
     
    ling, Aug 12, 2003
    #1
    1. Advertising

  2. ling

    Kurt Watzka Guest

    ling wrote:
    >
    > Hi,
    >
    > I'm trying to do fscanf from a file with integers.
    > I've tried using both %d and %i hoping that I would figure out their difference.
    >
    > When I checked the man pages, the only difference I could tell was
    >
    > d: Matches an optionally signed decimal integer ...
    > i: Matches an optionally signed integer ...
    >
    > What's the difference between an integer and a decimal integer?


    The integer is not necessarily in decimal notation. 27, 033, and 0x1B
    represent the
    same integer, and scanf("%i", &intVariable) will use the proper base
    conversion if
    oneof those representations can be read from stdin.

    Kurt Watzka
     
    Kurt Watzka, Aug 12, 2003
    #2
    1. Advertising

  3. ling

    Alex Guest

    ling <> wrote:
    > Hi,


    > I'm trying to do fscanf from a file with integers.
    > I've tried using both %d and %i hoping that I would figure out their difference.


    > When I checked the man pages, the only difference I could tell was


    > d: Matches an optionally signed decimal integer ...
    > i: Matches an optionally signed integer ...


    > What's the difference between an integer and a decimal integer?


    A 'decimal integer' means an integer in base 10.

    Hence, *scanf will expect a decimal integer with the 'd' conversion
    specifier and will attempt to detect the base first with 'i'.

    Alex
     
    Alex, Aug 12, 2003
    #3
  4. ling wrote:
    > Hi,
    >
    > I'm trying to do fscanf from a file with integers.
    > I've tried using both %d and %i hoping that I would figure out their difference.
    >
    > When I checked the man pages, the only difference I could tell was
    >
    > d: Matches an optionally signed decimal integer ...
    > i: Matches an optionally signed integer ...
    >
    > What's the difference between an integer and a decimal integer?


    Notice below that 0423 is an integer, but is interpreted differently with
    %d (decimal value 423) and %i (octal 0423 = decimal 275) and 0x46 is 0 with
    %d (the scan stops at the non-digit 'x') but hex 46 = decimal 70 with %i.

    If you want decimal values with possibly leading zeroes, you must use %d
    since %i will interpret it as octal.

    #include <stdio.h>

    int main(void)
    {
    char input[] = "375 0423 0x46";
    int a, b, c;
    unsigned ua, ub, uc;

    printf("The input string is \"%s\"\n\n", input);
    printf("attempting to read as integer\n");
    a = b = c = 0;
    sscanf(input, "%i %i %i", &a, &b, &c);
    printf("decimal values: %d %d %d\n\n", a, b, c);

    printf("attempting to read as decimal integer\n");
    a = b = c = 0;
    sscanf(input, "%d %d %d", &a, &b, &c);
    printf("decimal values: %d %d %d\n\n", a, b, c);

    printf("attempting to read as octal integer\n");
    ua = ub = uc = 0;
    sscanf(input, "%o %o %o", &ua, &ub, &uc);
    printf("unsigned decimal values: %u %u %u\n\n", ua, ub, uc);

    printf("attempting to read as hex integer\n");
    ua = ub = uc = 0;
    sscanf(input, "%x %x %x", &ua, &ub, &uc);
    printf("unsigned decimal values: %u %u %u\n\n", ua, ub, uc);
    return 0;
    }


    The input string is "375 0423 0x46"

    attempting to read as integer
    decimal values: 375 275 70

    attempting to read as decimal integer
    decimal values: 375 423 0

    attempting to read as octal integer
    unsigned decimal values: 253 275 0

    attempting to read as hex integer
    unsigned decimal values: 885 1059 70





    --
    Martin Ambuhl
     
    Martin Ambuhl, Aug 12, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,233
  2. Santa
    Replies:
    1
    Views:
    1,090
    Mark A. Odell
    Jul 17, 2003
  3. arnuld
    Replies:
    33
    Views:
    1,187
  4. PerlFAQ Server
    Replies:
    0
    Views:
    361
    PerlFAQ Server
    Jan 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    266
    PerlFAQ Server
    Apr 15, 2011
Loading...

Share This Page