The CyberPlus Blog

Our Interface to the Community!

  • Home
  • About

22

Jul

C FAQ: What are the sizes of char, short, int and long?

Posted by Nagesh Rao  Published in C, Technical

What are the sizes of char, short, int and long?

The C standard does not provide standard sizes for these data types! Hence the sizes will always be implementation dependent, which means different compilers can give different sizes. That said, there are some rules however that all compilers are obliged to follow:

1. The size of char should be at least 1 byte (most implementations will have sizeof(char)=1)

2. The size of short should be less than or equal to the size of int, and should be at least 2 bytes (most implementations will have sizeof(short)=2)

3. The size of int should be the machine word size (the optimal size that the system can handle at one go). Thus, a 16-bit compiler will have sizeof(int)=2 (16 bits = 2 bytes) whereas a 32-bit compiler will have sizeof(int)=4 (32 bits = 4 bytes)

4. The size of long should be greater than or equal to size of int, and should be at least 4 bytes (most implementations will have sizeof(long=4)

Thus, the following condition will always hold good, no matter which C compiler you use:

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

Typical values are shown below:

char short int long
16-bit platform 1 2 2 4
32-bit platform 1 2 4 4
64-bit platform 1 2 8 8

 

Related Questions:

Q: Are char, short, int and long signed or unsigned by default?

Q: How do I choose between char, short, int and long?

Q: How do I find the ranges of char, short, int and long?

Q: What if I want something larger than long?

Q: Is C a middle-level language?

Related Articles

  • C FAQ: How do I compile multiple C programs and link them together? (August 7th, 2014)
  • C FAQ: Size of int and int* on 64-bit machine (July 31st, 2014)
  • C FAQ: Is there a difference between variable definition and variable declaration? (July 20th, 2014)
  • A Brief History of C (July 17th, 2014)
  • Memory Models and Types of Pointers (October 16th, 2008)

No user responded in this post

Subscribe to this post comment rss or