tags:

views:

175

answers:

2

I have some ancient memories of writing C code like:

long value = 0;

in the bad old Win16 days and ending up with value being only half-initialized: i.e. the lower 16 bits were 0 and the upper 16 bits were whatever random bits were at that location in memory. As such, I became conditioned to write:

long value = 0L;

Is this still required in this day-and-age under C99 and/or C++? I know that ILP32 specifies that int and long are both 32-bit, but suppose we're using, say, LP64 where ints are 32-bits and longs are 64-bits. Are the suffixes required or will the modern forms of C and C++ implicitly extend literals to the length of the variable they're being assigned to?

How about unsigned values? I.e. is this required?

unsigned long value = 0UL;
+4  A: 

They are not required in the examples you give. However, they may be needed in some somewhat recondite circumstances. For example, the following may produce different values:

sizeof(1)
sizeof(1L)

anon
A simple no would have done.
Martin York
@Martin: really? Surely on a website like this, which tries to educate its readers, even a simple yes/no question requires some explanation.
Steve Melnikoff
+2  A: 

No this should not be required any more. The behavior you are describing, if visible in the program and not just the debugger, is a bug.

JaredPar