Back to: C
In C there are four basic data types: int, char, float and double. C supports modifiers of these: signed, unsigned, short and long:
data type | minimum range |
---|---|
char | |
signed char | −128 to 127 |
unsigned char | 0 to 255 |
signed short | −32,768 to 32,767 |
unsigned short | 0 to 65,535 |
int | |
signed int | −32,768 to 32,767 |
unsigned int | −32,768 to 32,767 |
signed short int | −32,768 to 32,767 |
unsigned short int | 0 to 65,535 |
signed long int | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 0 to 4,294,967,295 |
double float | |
long double float |
char
char is an integer type, that is one byte in size. char is ALWAYS the smallest datatype! A char is always one byte but this does not mean that a char will always be 8 bits! In almost all scenarios when we say a byte, we expect it to equal 8 bits. Although this is usually true, it does not have to be. Technically a byte can be any other number. If you ever want to check, here is some code to check how many bits a char is on a system:
printf("On this system the number of bits a 'char' has is: %zu\n", sizeof(char) * CHAR_BIT)
A signed char means that it can be a negative or positive number and an unsigned char means that it can only be a positive number.
From table 1 you will also see that have also listed char without a modifier. This is legitimate but I would say that you should think about if you should be using it! This is because char is implementation-specific! If you use char, you could result in either a signed or unsigned char. Therefore, you need to be careful when using it! If you are writing a program that contains chars with negative numbers and your implementation has char set to unsigned, you will run into problems. The negative sign will be ignored! What I suggest is that you always use either signed or unsigned char. Unsigned chars are ideal for storing ASCII characters, as the range is 0 to 255.
int
The int datatype is used to represent integers. Like char, you can type int on its own. However, unlike char, it is safer to use. This is because int on its own is ALWAYS signed! Therefore, when you want a signed int, you can use int or signed int. If you only want positive numbers, then you need to use unsigned int. The C standard dictates that an int must have a range of at least -32768 to 32767.
printf("minimum int value = %d\n"
"maximum int value = %d\n"
"size of int in bytes = %zu\n"
"size of int in bits = %zu\n\n\n",
INT_MIN, INT_MAX, sizeof(int),
sizeof(int) * CHAR_BIT);