Here we are going to see the Integer data types in C language.

int Data Type:

In C, the int data type occupies 2 bytes (16 bits) of memory to store an integer value.

int or signed int data type denotes a 16 – bit signed integer, which can hold any value between -32,768 (-2 15 ) and 32,767 (2 15 -1). unsigned int data type denotes a 16 – bit integer and does not use a bit to store the sign. Hence, it can hold only positive values between 0 and 65535 (2 16 -1).

The OS architecture (i.e., either 16 – bit, 32 – bit or 64 – bit) plays an important role in determining the memory occupied by a numeric data type. In a 16 – bit OS, 2 bytes (16 bits) of memory is allocated to an int data type.

Similarly, 4 bytes (32 bits) of memory is allocated in a 32 – bit OS, and 8 bytes (64 bits) of memory is allocated in a 64 – bit OS for an int data type. Consider the following example using int data type:

#include <stdio.h>
void main() {
     int a = 17;
     int b = 35;
     int sum;
     sum = a + b;
     printf("Sum of the given two numbers  = %d\n", sum);
}

Here, 2 bytes of memory is allocated to each variable a, b and they are initialized with integer constants 17 and 35 respectively.

If a variable is not initialized, then by default, the local variable contains garbage value (some preexisting value in the allocated memory which may not make any sense). Some compilers like GCC automatically initialize the uninitialized int variables to zero.

We cannot store decimal values (number with a fractional part) using the int data type. If we use the int data type to store decimal values, the decimal values will be truncated and only the whole number will be stored. float or a double data type is used to store decimal numbers.

To print unsigned values in the console,  use %u format character instead of %d in the print() function.

Whenever an attempt is made to assign a negative number to an unsigned int (For ex: unsigned int num = -2😉 the compiler does not flag it as an error. Instead, it will automatically convert the negative number to a positive number as shown below:

unsigned int num = -2;

The value stored in num = unsigned int maximum_value + 1 – num;

The final value in num = 429497295 (in a 32 – bit processing system).

short int Data Type:

In C, the short int data type occupies 2 bytes  (16 bits) of memory to store an integer value.

short int or signed short int data type denotes a 16 – bit signed integer, which can hold any value between 32,768 (-2 15) and 32,767 (2 15-1). unsigned short int data type denotes a 16 – bit integer and does not use a bit to store the sign. Hence, it can hold only positive values between 0 and 65535 (2 16-1).

The OS architecture (I,e., either 16 – bit, 32 – bit, or 64 – bit) plays an important role in determining the memory occupied by the short int data type. In a 16 – bit OS, 2 bytes (16 bits) of memory is allocated to a short int. However, only 1 byte is used to store the value, while the other remains empty.

Unlike a normal int data type, 2 bytes (16 bits) of memory is allocated in a system with 32-bit OS and 4 bytes (32 bits) of memory in a system with 64-bit OS to the short int data type.

Consider the following example using short int data type:

#include <stdio.h>
void main() {
     short int p = 17;
     short int q = 35;
     short int value;
     value = p * q;
     printf("Product of the two numbers = %d\n", value);
}

Here, 2 bytes of memory is allocated to each variable p, q, and they are initialized with integer constants  17 and 35 respectively.

If a variable is not initialized, then by default, the local variable contains garbage value (some preexisting value in the allocated memory which may not make any sense). Some compilers like GCC automatically initialize the uninitialized int variables to zero.

long int Data Type:

In C, the long int data type occupies 4 bytes (32 bits) of memory to store an integer value.

long int or signed long int data type denotes a 32 – bit signed integer that can hold any value between -2,147,483,648 (-2 31) and 2,147,483,647 (2 31 -1).

unsigned long int data type denotes a 32 – bit integer. It does not use a bit to store the sign. Hence it can hold only positive values between 0 and 4,294,967,295 (2 32 – 1).

long is used whenever an int data type is not sufficient to represent a number. Consider the following example that uses long int data type:

#include <stdio.h>
void main() {
     long int x = 17L;
     long int y = 35L;
     long int z;
     z = x + y;
     printf("Sum of two numbers = %ld\n", z);
}

Here, 4 bytes of memory are allocated to each variable xy, and they are initialized with long literals 17L and 35L respectively. The long literal values are suffixed with char ‘L‘ (lowercase of ‘L‘ can also be used, however, the uppercase is more readable).

If a variable is not initialized, then by default, the local variable contains garbage value (some preexisting value in the allocated memory which may not make any sense). Some compilers like GCC automatically initialize the uninitialized int variables to zero.

Special Note: Variables such as integers can be represented in two ways, i.e., signed and unsignedSigned numbers use a sign flag or can distinguish between negative values and positive values. Whereas unsigned numbers stored only positive numbers but not negative numbers.

char Data type:

In C, a char data type is used to represent individual characters. Generally, a char type will occupy only one byte (8 bits) of memory.

char data type will permit a range of values between 0 to 255 in case of unsigned char, and between -128 to +127 in case of signed char or simply char. Consider the following example using the char data type:

#include <stdio.h>
void main() {
  char gender1 = 'M';
  char gender2 = 'F';
  printf("gender1 = %c\n", gender1);
  printf("gender2 = %c\n", gender2);
}

A character constant must be placed within a single quote. It cannot span multiple lines.

Each character is internally represented as a number which is called its ASCII value. (ASCII stands for American Standard Code for Information Interchange)

ASCII ranges for uppercase, lowercase characters and digits are as follows:

'A' - 'Z' : 65 - 90
'a' - 'z' : 97 - 122
'0' - '9' : 48 - 57

The character data type can be divided into two types:

  1. signed char
  2. unsigned char

Signed char:

It is a character data type where the variable consumes 7 bits of memory for storing data (also known as data bits) and 1 bit of memory for storing sign (also known as sign bit). It ranges from -128 to + 127.

 Unsigned char:

It is a character data type where the variable consumes all 8 bits of memory and there is no sign bit (which is present in signed char). It ranges from 0 to 255.

References:

Happy Learning