tags:

views:

343

answers:

8

I want to know how to announce int to make sure it's 4 bytes or short in 2 bytes no matter on what platform. Does C99 have rules about this?

A: 

Try the INT_MAX constant in limits.h

Fred
No, I don't think he's looking for the maximum possible value of an int, he's looking for a way to ensure the number of bytes used by an int.
FrustratedWithFormsDesigner
So? They are inseparable. A 4 byte value can only contain 2^32 values.
Fred
Two sides of the same coin, no need to nitpick on this since the highest value and the number of bytes needed to represent the number are so closely related.
Fred
@Fred: Strictly speaking, I don't think they are inseparable. I think a C implementation is free to artificially limit the allowed integer range independently of the integer size.
jamesdlin
@jamesdlin: I disagree. A variable declared as `int` should not be able to store a value greater than `INT_MAX` or less than `INT_MIN`. The C89 standard says that `INT_MAX` is the "maximum value for an object of type `int`". And I think Fred's recommendation of using INT_MAX is a good way to make compile-time decisions as to whether an `int` is 2 or 4 bytes long. +1 to Fred.
tomlogic
@tomlogic: From section 6.2.6.1 of the C99 standard: "Certain object representations need not represent a value of the object type.... Such a representation is called a *trap representation*."
jamesdlin
@jamesdin: OK, so you're saying that the implementation could limit the `int` type to -32768 to +32767 yet `sizeof(int)` would return 4? I think I see that as a valid interpretation of the spec.
tomlogic
A: 

Do you want to require it to be 4 bytes?

If you just want to see the size of int as it is compiled on each platform then you can just do sizeof(int).

Jared
yes I want to require it to be 4 bytes because I need to put it in a socket message.
+9  A: 

C99 doesn't say much about this, but you can check whether sizeof(int) == 4, or you can use fixed size types like uint32_t (32 bits unsigned integer). They are defined in stdint.h

Ben
I want to know if sizeof(int) != 4 how to make a 4 bytes int myself.thanks
thank you so much, and also thanks all guys
@kkpattern, make sure to "accept" the answer by clicking the greyed-out checkbox beside this answer. It will then turn green and you'll get an extra 2 rep :)
Earlz
@Earlz thank you, I am still learning how to surface on the stackoverflow, This is a so great website.:)
A: 

sizeof (int) will return the number of bytes an int occupies in memory on the current system.

Mike
+6  A: 

If you are using C99 and require integer types of a given size, include stdint.h. It defines types such as uint32_t for an unsigned integer of exactly 32-bits, and uint_fast32_t for an unsigned integer of at least 32 bits and “fast” on the target machine by some definition of fast.

Edit: Remember that you can also use bitfields to get a specific number of bits (though it may not give the best performance, especially with “strange” sizes, and most aspects are implementation-defined):

typedef struct {
    unsigned four_bytes:32;
    unsigned two_bytes:16;
    unsigned three_bits:3;
    unsigned five_bits:5;
} my_message_t;

Edit 2: Also remember that sizeof returns the number of chars. It's theoretically possible (though very unlikely these days) that char is not 8 bits; the number of bits in a char is defined as CHAR_BIT in limits.h.

Arkku
bitfields is amazing!:) thank you
Just note that the exact representation of bitfields is implementation-dependant and may cause the compiler to generate unexpected amounts of extra code, e.g. to handle “strange” field sizes like 3 bits. And you cannot have pointers to bitfield members, etc. It's better to use the `stdint.h` types if you find suitable ones there. But bitfields can be handy if you need to pack data to match hardware requirements or such.
Arkku
@Arkku bitfields is so subtle.Thanks for you note
A: 

I assume you want something beyond just the obvious sizeof (int) == 4 check. Likely you want some compile-time check.

In C++, you could use BOOST_STATIC_ASSERT.

In C, you can make compile-time assertions by writing code that tries to create negatively-sized arrays on failure or that tries to create switch statements with redefined cases. See this stackoverflow question for examples: Ways to ASSERT expressions at build time in C

jamesdlin
A: 

You can use sizeof(int), but you can never assume how large an int is. The C specification doesn't put any assumptions on the size of an int, except that it must be greater or equal to the size of a short (which must be greater or equal to the size of a char).

Often the size of an int aligns to the underlying hardware. This means an int is typically the same as a word, where a word is the functional size of data fetched off the memory bus (or sometimes the CPU register width). It doesn't have to be the same as a word, but the earliest notes I have indicated it should be the preferred size for memory transfer (which is typically a word).

In the past, there have been 18 bit ints (PDP-8) and 24 bit ints (PDP-15). There have been architectures with 36 bit word sizes (PDP-11) but I can't recall what their int size turned out to be.

On Linux platforms, you can peek in

#include <sys/types.h>

to get the actual bit count for each type.

Edwin Buck
A: 

I found last night that visual studio 2008 doesn't support C99 well, and it doesn't support stdint.h. BUT they have their own types. here is a example:

#ifdef _MSC_VER 
typedef __int8  int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t; 
typedef unsigned __int32 uint32_t; 
typedef __int64 int64_t; 
typedef unsigned __int64 uint64_t; 
#else 
#include <stdint.h> 
#endif