Hi there,

Here's a basic question from the basics of C.
Woukd like to know the use of appending L or UL at the end of integer constant which is assigned to an unsigned int type variable. for eg: on a 32-bit machine

unsigned int i;
i= 543727232UL;

what way it is going to help?

Thanks
Swapna

It actually explicitly states that the assignment to i is of unsigned long type. Given the piece of code you've posted above, if you use something like splint to check your code, you get a warning, stating that the assignment to i is of incompatible type.

There are few cases I can think of in which the suffix really is helpful, but I remember something about a floating-point constant being treated like a double by default, suffixing it with f making it of float type.

Edited 6 Years Ago by creeps: n/a

Hi There,

I was going through C test by Nigel Jones. Its popular test, I am sure many of us are aware of. The first question is about defining a macro to get the number of seconds in a year and the supposedly right answer to this is:
#define SECONDS_YEAR 365*24*60*60UL

And thats when it got me inot thinking the real use of suffixing UL/L at the end of the constant. This is what Nigel Jones gives the reasoning:
"A realization that the expression will overflow an integer argument on a 16-bit
machine-hence the need for the L, telling the compiler to treat the variable as
a Long"

I am still wondering of its purpose.

Thanks
Swapna

Picture how it works:
1) The pre processor substitutes the numbersUL, for the expression SECS IN YEAR.

2) Now the compiler comes along and see's the number, but what data type (size) should it be given?

General default is integer, which is not big enough on a 16 bit machine.

So the suffix L or UL, is a little "cheat" to tell the compiler "this value should be treated as a long or unsigned long".

It's the compilers version of "pillow talk", imo. ;)

This article has been dead for over six months. Start a new discussion instead.