locked
Printf %d and %ld RRS feed

  • Question

  • Hello everyone,


    I think printf %d works fine with signed, unsigned and negative integer values, and the same as %ld for long. Is that correct? If not, do we need to special conversion?


    thanks in advance,
    George

    Wednesday, October 24, 2007 9:14 AM

Answers

  •  

    First thing these are known as Format Specifier. you can use . Like in following Example you can see few of them

    Code Block

    %d or %i for int

    %c for character

    %f for float

    %lf for double

    %s for string

    %x for hexadecimal

    %ox for octal

     

     

    Calling wrong format specifier doesn't give you error , it's depend sometime they perform internal casting.

    Why not you gave a try on all of this and see what's going to be happen. A Small example

     

    Code Block
    int main()
    {
       printf ("Characters: %c %c \n", 'a', 65);
       printf ("Decimals: %d %ld\n", 1977, 650000);
       printf ("Preceding with blanks: %10d \n", 1977);
       printf ("Preceding with zeros: %010d \n", 1977);
       printf ("Some different radixes: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
       printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
       printf ("Width trick: %*d \n", 5, 10);
       printf ("%s \n", "A string");
       return 0;
    }

     

     

    output of the all above printf is

     

    Characters: a A
    Decimals: 1977 650000
    Preceding with blanks:       1977
    Preceding with zeros: 0000001977
    Some different radixes: 100 64 144 0x64 0144
    floats: 3.14 +3e+000 3.141600E+000
    Width trick:    10
    A string

     

     have a look on the following link

    http://www.cplusplus.com/reference/clibrary/cstdio/printf.html

     

    thanx

    Wednesday, October 24, 2007 2:26 PM
  • %d is for signed int, Use %u specifically for unsigned int. Long and int types are same. %ld is for long int.

     

    printf Type Field Characters (CRT)

    Wednesday, October 24, 2007 10:21 AM
  • There wont be any error like access violation, printf will treat the argument as signed, that might display the output differnetly that what you expected.

     

    Signed int's value range is –2,147,483,648 to 2,147,483,647, & unsigned int has 0 to 4294967295, both types are 4 bit wide, having same capacity, actual storage does not know any signed or unsignedness, it only stores it as hex in 4 bytes.

     

    Consider a case where your unsigned int has a unsigned value 2864434397(0xAABBCCDD), you expect to see value in output as 2864434397 but use of %d makes it show -1430532899.

     

    The hex values from 0x00000000 to 0xFFFFFFFF are interpreted diff. by unsigned and signed read, unsigned read see the 0xFFFFFFFF as 4294967295 but signed read gets it as -1.

     

    Signed

     

    0---------------- 2147483647, (-2147483648), (-2147483647), (-2147483646) ----------------------(-1)

     

    Unsigned

    0---------------- 2147483647, 2147483648, 2147483649, -----------------------------------------------4294967295

     

    Wednesday, October 24, 2007 11:11 AM

All replies

  • %d is for signed int, Use %u specifically for unsigned int. Long and int types are same. %ld is for long int.

     

    printf Type Field Characters (CRT)

    Wednesday, October 24, 2007 10:21 AM
  • Thanks Ramkrishna,

     

     

    What happens if I use unsinged int and %d? Could you show me a sample please which will cause error? Currently I met with some strange output number and suspect I am using wrong parameter for printf.

     

     Ramkrishna Pawar wrote:

    %d is for signed int, Use %u specifically for unsigned int. Long and int types are same. %ld is for long int.

     

    printf Type Field Characters (CRT)

     

     

    regards,

    George

    Wednesday, October 24, 2007 10:43 AM
  • There wont be any error like access violation, printf will treat the argument as signed, that might display the output differnetly that what you expected.

     

    Signed int's value range is –2,147,483,648 to 2,147,483,647, & unsigned int has 0 to 4294967295, both types are 4 bit wide, having same capacity, actual storage does not know any signed or unsignedness, it only stores it as hex in 4 bytes.

     

    Consider a case where your unsigned int has a unsigned value 2864434397(0xAABBCCDD), you expect to see value in output as 2864434397 but use of %d makes it show -1430532899.

     

    The hex values from 0x00000000 to 0xFFFFFFFF are interpreted diff. by unsigned and signed read, unsigned read see the 0xFFFFFFFF as 4294967295 but signed read gets it as -1.

     

    Signed

     

    0---------------- 2147483647, (-2147483648), (-2147483647), (-2147483646) ----------------------(-1)

     

    Unsigned

    0---------------- 2147483647, 2147483648, 2147483649, -----------------------------------------------4294967295

     

    Wednesday, October 24, 2007 11:11 AM
  •  

    First thing these are known as Format Specifier. you can use . Like in following Example you can see few of them

    Code Block

    %d or %i for int

    %c for character

    %f for float

    %lf for double

    %s for string

    %x for hexadecimal

    %ox for octal

     

     

    Calling wrong format specifier doesn't give you error , it's depend sometime they perform internal casting.

    Why not you gave a try on all of this and see what's going to be happen. A Small example

     

    Code Block
    int main()
    {
       printf ("Characters: %c %c \n", 'a', 65);
       printf ("Decimals: %d %ld\n", 1977, 650000);
       printf ("Preceding with blanks: %10d \n", 1977);
       printf ("Preceding with zeros: %010d \n", 1977);
       printf ("Some different radixes: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
       printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
       printf ("Width trick: %*d \n", 5, 10);
       printf ("%s \n", "A string");
       return 0;
    }

     

     

    output of the all above printf is

     

    Characters: a A
    Decimals: 1977 650000
    Preceding with blanks:       1977
    Preceding with zeros: 0000001977
    Some different radixes: 100 64 144 0x64 0144
    floats: 3.14 +3e+000 3.141600E+000
    Width trick:    10
    A string

     

     have a look on the following link

    http://www.cplusplus.com/reference/clibrary/cstdio/printf.html

     

    thanx

    Wednesday, October 24, 2007 2:26 PM
  • Thanks Pintu,

     

     

    The link is very useful. I am clear now.

     

     Pintu Shukla wrote:

     

    First thing these are known as Format Specifier. you can use . Like in following Example you can see few of them

    Code Block

    %d or %i for int

    %c for character

    %f for float

    %lf for double

    %s for string

    %x for hexadecimal

    %ox for octal

     

     

    Calling wrong format specifier doesn't give you error , it's depend sometime they perform internal casting.

    Why not you gave a try on all of this and see what's going to be happen. A Small example

     

    Code Block
    int main()
    {
       printf ("Characters: %c %c \n", 'a', 65);
       printf ("Decimals: %d %ld\n", 1977, 650000);
       printf ("Preceding with blanks: %10d \n", 1977);
       printf ("Preceding with zeros: %010d \n", 1977);
       printf ("Some different radixes: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
       printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
       printf ("Width trick: %*d \n", 5, 10);
       printf ("%s \n", "A string");
       return 0;
    }

     

     

    output of the all above printf is

     

    Characters: a A
    Decimals: 1977 650000
    Preceding with blanks:       1977
    Preceding with zeros: 0000001977
    Some different radixes: 100 64 144 0x64 0144
    floats: 3.14 +3e+000 3.141600E+000
    Width trick:    10
    A string

     

     have a look on the following link

    http://www.cplusplus.com/reference/clibrary/cstdio/printf.html

     

    thanx

     

     

    regards,

    George

    Thursday, October 25, 2007 9:04 AM
  • Thanks for your great sample, Ramkrishna!

     

     

    My question is solved.

     

     Ramkrishna Pawar wrote:

    There wont be any error like access violation, printf will treat the argument as signed, that might display the output differnetly that what you expected.

     

    Signed int's value range is –2,147,483,648 to 2,147,483,647, & unsigned int has 0 to 4294967295, both types are 4 bit wide, having same capacity, actual storage does not know any signed or unsignedness, it only stores it as hex in 4 bytes.

     

    Consider a case where your unsigned int has a unsigned value 2864434397(0xAABBCCDD), you expect to see value in output as 2864434397 but use of %d makes it show -1430532899.

     

    The hex values from 0x00000000 to 0xFFFFFFFF are interpreted diff. by unsigned and signed read, unsigned read see the 0xFFFFFFFF as 4294967295 but signed read gets it as -1.

     

    Signed

     

    0---------------- 2147483647, (-2147483648), (-2147483647), (-2147483646) ----------------------(-1)

     

    Unsigned

    0---------------- 2147483647, 2147483648, 2147483649, -----------------------------------------------4294967295

     

     

     

    regards,

    George

    Thursday, October 25, 2007 9:11 AM