Possible Duplicate:
A riddle (in C)
I have a couple of questions regarding the following snippet:
#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);
return 0;
}
Here the output of the code does not print the array elements as expected. But when I add a typecast of (int) the the macro definition of ELEMENTS as
#define TOTAL_ELEMENTS (int) (sizeof(array) / sizeof(array[0]))
It displays all array elements as expected.
- How does this typecast work?
Based on this I have few questions:
Does it mean if I have some macro definition as:
#define AA (-64)
by default in C, all constants defined as macros are equivalent to signed int.
If yes, then
But if I have to forcibly make some constant defined in a macro behave as an unsigned int is there any constant suffix than I can use (I tried UL, UD neither worked)?
How can I define a constant in a macro definition to behave as unsigned int?
Look at this line:
In the first iteration, you are checking whether
The operator size_of returns unsigned value and the check fails (-1 signed = 0xFFFFFFFF unsigned on 32bit machines).
A simple change in the loop fixes the problem:
To answer your other questions: C macros are expanded text-wise, there is no notion of types. The C compiler sees your loop as this:
If you want to define an unsigned constant in a macro, use the usual suffix (
u
forunsigned
,ul
forunsigned long
).sizeof
returns the number of bytes in unsigned format. That's why you need the cast.See more here.
Regarding your question about
See Macro definition and expansion in the
C preprocessor
:Constants defined as macros have no associated type. Use
const
where possible.Answering just one of your sub-questions:
To "define a constant in a macro" (this is a bit sloppy, you're not defining a "constant", merely doing some text-replacement trickery) that is unsigned, you should use the 'u' suffix:
This will insert an
unsigned int
literal wherever you typeUNSIGNED_FORTYTWO
.Likewise, you often see (in <math.h> for instance) suffices used to set the exact floating-point type:
This inserts a
float
(i.e. "single precision") floating-point literal wherever you typeFLOAT_PI
in the code.