11

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?

P Shved
  • 96,026
  • 17
  • 121
  • 165
kkpattern
  • 948
  • 2
  • 7
  • 31

8 Answers8

22

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
  • 7,372
  • 8
  • 38
  • 46
  • I want to know if sizeof(int) != 4 how to make a 4 bytes int myself.thanks – kkpattern Apr 16 '10 at 17:08
  • @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 Apr 16 '10 at 17:33
  • @Earlz thank you, I am still learning how to surface on the stackoverflow, This is a so great website.:) – kkpattern Apr 16 '10 at 17:42
8

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
  • 41,011
  • 10
  • 62
  • 84
  • 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 Apr 16 '10 at 17:20
  • @Arkku bitfields is so subtle.Thanks for you note – kkpattern Apr 16 '10 at 17:47
0

Try the INT_MAX constant in limits.h

Fred
  • 449
  • 1
  • 5
  • 13
  • 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 Apr 16 '10 at 17:10
  • So? They are inseparable. A 4 byte value can only contain 2^32 values. – Fred Apr 16 '10 at 17:12
  • 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 Apr 16 '10 at 17:25
  • @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 Apr 16 '10 at 18:28
  • @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 Jun 01 '10 at 15:03
  • @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 Jun 01 '10 at 18:06
  • @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 Jun 01 '10 at 21:12
0

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
  • 5,977
  • 10
  • 38
  • 48
0

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

Karmic Coder
  • 17,569
  • 6
  • 32
  • 42
0

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

Community
  • 1
  • 1
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
0

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
  • 69,361
  • 7
  • 100
  • 138
0

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 
kkpattern
  • 948
  • 2
  • 7
  • 31