Thread: Literal UL

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    4

    Literal UL

    Hi
    Could anyone please explain what does this literal UL mean? I tried to search on the internet for some article but could not find something.

    I guess it means unsigned long, but why is it used?

    Thanks
    Shani

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you see something like x = 3UL; yes, that means "unsigned long" [and you can use either separately, e.g, 3U or 3L will mean "unsigned" and "long" respectively].

    For most things, it's unnecessary, as the compiler will automatically form whatever type that it thinks is right for the number given, but for example small numeric constants may not be promoted to long when combined with other "small" numbers - for example 1 << 30 may not work right on a 16-bit machine.

    --
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    Thanks Mats.

    Is there some equivalent of UL for unsigned short/signed short?

    Shani

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Is there some equivalent of UL for unsigned short/signed short?
    No, you just use a regular constant without any suffix.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The only suffixes are:
    • F after a floating point literal makes that number a float instead of a double.
    • L after an integral literal makes that number a long instead of an int.
    • U after an inregral literal makes that literal unsigned. This can be combined with L to get an unsigned long, as you have seen.
    • C99 and non-standard C++ only: LL makes an integral literal a long long. This can also be combined with U.


    As you might have surmised, an ordinary integer literal like "123" is of type int, while an ordinary floating-point literal like "3.14159265358979324" is of type double.

    [edit] The reason there are suffixes for longs but not shorts is that a short can be stored in an int, so a short literal can be represented as an int one. You might still get overflow by assigning that int to a short, but that's a different problem. [/edit]

    Note that all suffixes are case-insensitive, though many people prefer uppercase because 4l (four ell) looks like 41 (forty one) while 4L is clearly a long 4.

    You also get prefixes, for example, 0x makes an integral constant a hexadecimal one, while 0 makes it an octal one . . . I'm not sure if they can be combined or not. It would appear so.
    Code:
    $ cat > combinefix.c
    #include <stdio.h>
    
    int main() {
        printf("&#37;lu\n", 0xffUL);
        return 0;
    }
    $ gcc -W -Wall -ansi -pedantic -g combinefix.c -o combinefix && ./combinefix
    255
    $
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer dangling or not
    By vaibhav in forum C++ Programming
    Replies: 16
    Last Post: 08-05-2006, 06:39 PM
  2. "Unescaping" literal strings
    By yoshiznit123 in forum C Programming
    Replies: 4
    Last Post: 06-02-2006, 01:35 PM
  3. const references initialized to a literal
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2006, 08:52 AM
  4. use of hex literal constant
    By happycoder in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2003, 11:45 PM
  5. String literal
    By subdene in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2002, 02:10 PM