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
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
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.
Thanks Mats.
Is there some equivalent of UL for unsigned short/signed short?
Shani
No, you just use a regular constant without any suffix.Is there some equivalent of UL for unsigned short/signed short?
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("%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.