Hi,
I was wondering if there's a way to do something like this in C:
Code:#if sizeof( long ) != 4 # error "sizeof( long ) != 4" #endif
Hi,
I was wondering if there's a way to do something like this in C:
Code:#if sizeof( long ) != 4 # error "sizeof( long ) != 4" #endif
My best suggestion would be to use <limits.h>, which contains "LONG_BIT", which should be 32 in your case.
--
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.
What exactly are you trying to test, the numeric range of a long?
There are #define constants in limits.h for that.
Besides, on some machines, sizeof(long) == 1, and it contains 32-bits.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I'm trying to test how many bytes are in a long, int, short... I have a function that will only run on machines where short = 2 bytes, int & long = 4 bytes. If the code is ever compiled on a system where that's not true, I need to generate a compile error so the person porting the code can take the appropriate steps to fix it.
The sort of code that assumes specific sizes for values should really use platform specific typdefs, e.g.
Similarly for unsigned, etc, etc.Code:typedef int int32; #if LONG_BIT == 64 typedef long int64; #else typedef long long int64; #endif
--
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.
Maybe I should just do this:
Code:#if UINT_MAX != 0xFFFFFFFF # error "ints aren't 4 bytes here!" #endif
When I looked it up on the web, I got the impression it was standard - but as you say, it's not in the VC <limits.h>.
You can of course use the constants of LONG_MAX and INT_MAX for example.
--
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.
A good optimizing compiler can do it at compile time even if you use code instead of the preprocessor.
The sizeof(long) folds to the constant 4, then the constant comparison 4 == 4 folds to the constant 1, then the compiler sees if(1), and optimizes the other case away.Code:if(sizeof(long) == 4) { do_something(); } else { do_something_else(); }
Of course this doesn't work if you wanted to, say, declare a certain variable only if sizeof(long) == 4.
But that wouldn't issue a compile error, which is what I want.
Basically, in my code I say the htons()/ntohs()... functions should be used.
If, for some reason, those functions don't exist on the platform being ported to, they can use my own version of those functions. But if sizeof( short ) != 2 or sizeof( long ) != 4, they need to make some changes to get it to work.
So, a runtime check that prints a noticable message and aborts would work.
Alternatively, you can use a "compile to check" method, so a program that prints the size of relevant types and/or returns a "ok"/"fail" based on that. This is quite often done in automated build scripts.
--
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.
Sorry but,
when you type:
or:Code:long long var_name;
what doesn't that mean?Code:long int var_name;
This is part of the standard integer types (well, at least in the C99 version). This is the complete list
- char
- short int
- int
- long int
- long long int
or, if you prefer,
- char
- short
- int
- long
- long long
Both lists are equivalent.
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill