Printf %d and %ld

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?R2Vvcmdl?=

    Printf %d and %ld

    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
  • David Wilkinson

    #2
    Re: Printf %d and %ld

    George wrote:
    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.

    --
    David Wilkinson
    Visual C++ MVP

    Comment

    • =?Utf-8?B?R2Vvcmdl?=

      #3
      Re: Printf %d and %ld

      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

      "David Wilkinson" wrote:
      George wrote:
      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.
      >
      --
      David Wilkinson
      Visual C++ MVP
      >

      Comment

      • David Wilkinson

        #4
        Re: Printf %d and %ld

        George wrote:
        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
        >
        "David Wilkinson" wrote:
        >%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.
        >>
        >--
        >David Wilkinson
        >Visual C++ MVP
        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.

        --
        David Wilkinson
        Visual C++ MVP

        Comment

        • =?Utf-8?B?R2Vvcmdl?=

          #5
          Re: Printf %d and %ld

          Thanks David,


          I agree.


          regards,
          George

          "David Wilkinson" wrote:
          George wrote:
          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

          "David Wilkinson" wrote:
          %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.
          >
          --
          David Wilkinson
          Visual C++ MVP
          >
          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.
          >
          --
          David Wilkinson
          Visual C++ MVP
          >

          Comment

          Working...