%i or %d - where is the difference?

This is a discussion on %i or %d - where is the difference? within the C Programming forums, part of the General Programming Boards category; Question: Where is the difference between %i and %d that are used in e.g. printf(), fscanf() and so on ? ...

  1. #1
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Question %i or %d - where is the difference?

    Question: Where is the difference between %i and %d that are used in e.g. printf(), fscanf() and so on ? When do I have to use %i instead of %d and vice versa?
    Although I do have the C99 Language Standard I don't know what is meant in the description of %i and %d.

    Thank you for your help.
    %d Matches an optionally signed decimal integer, whose format is the same as
    expected for the subject sequence of the strtol function with the value 10
    for the base argument. The corresponding argument shall be a pointer to
    signed integer.
    %i Matches an optionally signed integer, whose format is the same as expected
    for the subject sequence of the strtol function with the value 0 for the
    base argument. The corresponding argument shall be a pointer to signed
    integer.

    7.20.1.4 The strtol, strtoll, strtoul, and strtoull functions
    Synopsis
    1 #include <stdlib.h>
    long int strtol(
    const char * restrict nptr,
    char ** restrict endptr,
    int base);
    long long int strtoll(
    const char * restrict nptr,
    char ** restrict endptr,
    int base);
    unsigned long int strtoul(
    const char * restrict nptr,
    char ** restrict endptr,
    int base);
    unsigned long long int strtoull(
    const char * restrict nptr,
    char ** restrict endptr,
    int base);
    Description
    2 The strtol, strtoll, strtoul, and strtoull functions convert the initial
    portion of the string pointed to by nptr to long int, long long int, unsigned
    long int, and unsigned long long int representation, respectively. First,
    they decompose the input string into three parts: an initial, possibly empty, sequence of
    white-space characters (as specified by the isspace function), a subject sequence
    resembling an integer represented in some radix determined by the value of base, and a
    251) DECIMAL_DIG, defined in <float.h>, should be sufficiently large that L and U will usually round
    to the same internal floating value, but if not will round to adjacent values.
    §7.20.1.4 Library 309
    ISO/IEC 9899:1999 (E) ŠISO/IEC
    final string of one or more unrecognized characters, including the terminating null
    character of the input string. Then, they attempt to convert the subject sequence to an
    integer, and return the result.
    3 If the value of base is zero, the expected form of the subject sequence is that of an
    integer constant as described in 6.4.4.1, optionally preceded by a plus or minus sign, but
    not including an integer suffix. If the value of base is between 2 and 36 (inclusive), the
    expected form of the subject sequence is a sequence of letters and digits representing an
    integer with the radix specified by base, optionally preceded by a plus or minus sign,
    but not including an integer suffix. The letters from a (or A) through z (or Z) are
    ascribed the values 10 through 35; only letters and digits whose ascribed values are less
    than that of base are permitted. If the value of base is 16, the characters 0x or 0X may
    optionally precede the sequence of letters and digits, following the sign if present.
    4 The subject sequence is defined as the longest initial subsequence of the input string,
    starting with the first non-white-space character, that is of the expected form. The subject
    sequence contains no characters if the input string is empty or consists entirely of white
    space, or if the first non-white-space character is other than a sign or a permissible letter
    or digit.
    5 If the subject sequence has the expected form and the value of base is zero, the sequence
    of characters starting with the first digit is interpreted as an integer constant according to
    the rules of 6.4.4.1. If the subject sequence has the expected form and the value of base
    is between 2 and 36, it is used as the base for conversion, ascribing to each letter its value
    as given above. If the subject sequence begins with a minus sign, the value resulting from
    the conversion is negated (in the return type). A pointer to the final string is stored in the
    object pointed to by endptr, provided that endptr is not a null pointer.
    6 In other than the "C" locale, additional locale-specific subject sequence forms may be
    accepted.
    7 If the subject sequence is empty or does not have the expected form, no conversion is
    performed; the value of nptr is stored in the object pointed to by endptr, provided
    that endptr is not a null pointer.
    Returns
    8 The strtol, strtoll, strtoul, and strtoull functions return the converted
    value, if any. If no conversion could be performed, zero is returned. If the correct value
    is outside the range of representable values, LONG_MIN, LONG_MAX, LLONG_MIN,
    LLONG_MAX, ULONG_MAX, or ULLONG_MAX is returned (according to the return type
    and sign of the value, if any), and the value of the macro ERANGE is stored in errno.

  2. #2
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    When using %i to print an integer its same as %d..
    But when used with scanf i -reads an optionall signed decimal,octal,hex ...

    An e.g
    Code:
       int num1, num2;
        
       printf("Enter 2 numbers: " );
       scanf( "%d%i", &num1, &num2 );
    
       printf("Inputs diaplayed in decimal integer: %d %d", num1,      num2 );
    Enter 070 for both for num1 the it will display num1 as 70 and num2 as 56 .....

    56 is the octal equallent of 070
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  3. #3
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    That's good to know! Thank you very much.

    56 is the octal equallent of 070 [/B]
    Don't you mean "56 is the decimal equivalent of the octal 070"?

  4. #4
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Originally posted by Sargnagel
    That's good to know! Thank you very much.


    Don't you mean "56 is the decimal equivalent of the octal 070"?
    YEah sorry about that
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 05:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Replies: 6
    Last Post: 08-26-2006, 11:09 PM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 04:20 AM

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