% sequence

This is a discussion on % sequence within the C Programming forums, part of the General Programming Boards category; I am trying to figure out the %sequence for short, unsigned long, long, unsigned long and long double....

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    % sequence

    I am trying to figure out the %sequence for short, unsigned long, long, unsigned long and long double.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    hmm... turns out my usual resource does not have all of these listed. Anyway, they should be:

    short: hd
    unsigned long: lu
    unsigned long long: llu
    long double: Lf
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    hmm... turns out my usual resource does not have all of these listed. Anyway, they should be:

    short: hd
    unsigned long: lu
    unsigned long long: llu
    long double: Lf
    Are those non-standard (or new standard)? All the sources I've seen suggest that all ints (unsigned char, short, int, long) use %d and just get upgraded on their way in?

    Where did you find those?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Where did you find those?
    From C99, since my knowledge of C is too deficient for me to figure it out myself offhand

    EDIT:
    But that they were introduced in C99 could explain why cppreference.com does not list them. For %h the standard states that "the argument will have been promoted according to the integer promotions, but its value shall be converted to short int or unsigned short int before printing".
    Last edited by laserlight; 01-28-2008 at 12:14 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    From C99, since my knowledge of C is too deficient for me to figure it out myself offhand
    OK, but where exactly? Did you just have them memorized, or is there a website that I can go to check that stuff?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    OK, but where exactly? Did you just have them memorized, or is there a website that I can go to check that stuff?
    My PDF copy of ISO/IEC 9899:1999 "Programming languages - C", section 7.19.6.1 "The fprintf function".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by cpjust View Post
    OK, but where exactly? Did you just have them memorized, or is there a website that I can go to check that stuff?
    Google for "man printf." And if there's some other function X you need to understand, Google "man X."

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >All the sources I've seen suggest that all ints (unsigned char,
    >short, int, long) use %d and just get upgraded on their way in?
    What sources? Except for long long, those are all C89 format specifiers, and unless you have a good reason, you should match the type as closely as you can.

    For future reference, here's the C99 draft:
    http://web.archive.org/web/200502070...c99-draft.html

    And the C89 draft:
    http://web.archive.org/web/200502070...c89-draft.html
    My best code is written with the delete key.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Thanks, those should come in handy.
    I was looking at the MSDN & an online Linux Man Pages site, but when I checked again, I found them. They're just not that easy to find or understand.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And arguments to printf (and all other variadic functions) are upgraded to integer and double for "short / char" and "float" respectively, but where "long" is different from "int", the int is not turned into a long, nor is "double" turned into a "long double", so using the correct sequence is important to ensure portability.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 11:52 AM
  2. sequence
    By braddy in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 01:15 PM
  3. wsprintf and format specifiers
    By incognito in forum Windows Programming
    Replies: 2
    Last Post: 01-03-2004, 09:00 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. %i or %d - where is the difference?
    By Sargnagel in forum C Programming
    Replies: 3
    Last Post: 11-12-2002, 02:21 PM

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