Printf %d and %ld

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
George said:
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?

George:

%d will only work for unsigned int if the value is not bigger than the
largest allowed value of int. Otherwise, a negative value will be displayed.
 
Hi David,


Only works for unsigned int? I have tried that -1 works. :-)

int i = -1;
printf ("i is: %d", -1);

I have tried it on Visual Studio 2005.


regards,
George
 
George said:
Hi David,


Only works for unsigned int? I have tried that -1 works. :-)

int i = -1;
printf ("i is: %d", -1);

I have tried it on Visual Studio 2005.


regards,
George

George:

I meant:

For unsigned int, %d will only work if the value is not bigger than the
largest allowed value of int. Otherwise, a negative value will be
displayed. %u should be used with unsigned int.

%d is intended for use with int, and either positive or negative values
may be displayed.

Although not so well suited to localization, C++ streams handle this
issue much better, because they are overloaded for different types.
 
Thanks David,


I agree.


regards,
George

David Wilkinson said:
George:

I meant:

For unsigned int, %d will only work if the value is not bigger than the
largest allowed value of int. Otherwise, a negative value will be
displayed. %u should be used with unsigned int.

%d is intended for use with int, and either positive or negative values
may be displayed.

Although not so well suited to localization, C++ streams handle this
issue much better, because they are overloaded for different types.
 
Back
Top