Open In App

Data Type Modifiers in C

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In C, data type modifiers are used to modify the length or size of data that various data types hold such as int, char, and double as per the requirements. By using these data type modifiers we can precisely utilize the computer memory.

Primary Uses of Data Type Modifiers

  • To modify the size of the data type.
  • To modify the sign of the data type.

Types of Modifiers

In C, we have 4 data type modifiers that are used to modify data types. They are used as prefixed to the basic data types. For example, unsigned int.

  1. short
  2. long
  3. signed
  4. unsigned

Note: These modifiers cannot be used with a float data type.

‘short’ Type Modifier in C

The short modifier works with integer data type. It decreases the size of the int to 2 bytes along with the range that decreases to -32,768 to 32,767.

Example

In the below code, we initialized a normal integer ‘a’ and short integer ‘b’. After that we have print the size of both the variables and we can clearly see in the output that variable declared with data modifier with ‘short’ became 2 bytes.

C




// C program to illustrate difference between short int
// and int
#include <stdio.h>
  
int main()
{
  
    int a;
    // Modify the int using short
    short int b;
  
    // Print the size of a.
    printf("Size of a: %d", sizeof(a));
  
    // Print the size of b.
    printf("\nSize of b: %hd", sizeof(b));
  
    return 0;
}


Output

Size of a: 4
Size of b: 2

‘long’ Type Modifier in C

The long data type modifier is used to increase the size of a int or double data type by two times that also increases the range of these data types. For example, if int take 4 bytes of space it will take 8 bytes of space after using long with int.

Data Types with Long

The long can be used with following data types as:

  • long int
  • long long int
  • long double

Example

In the below code, we have declared the variables of integer and double type with and without using long modifier and then printed the size of each variables. In the output we can see that the size of long data type become double.

C




// C Program to illustrate size difference between int,
// long int, double and long double
#include <stdio.h>
  
int main()
{
    // defining variables with long
    int num;
    long int long_num;
    long long int long_long_num;
  
    double dub_num;
    long double long_dub_num;
  
    printf("Size of num: %d", sizeof(num));
    printf("\nSize of long_num: %d", sizeof(long_num));
    printf("\nSize of long_long_num: %d",
           sizeof(long_long_num));
    printf("\nSize of dub_num: %d", sizeof(dub_num));
    printf("\nSize of long_dub_num: %d",
           sizeof(long_dub_num));
  
    return 0;
}


Output

Size of num: 4
Size of long_num: 8
Size of long_long_num: 8
Size of dub_num: 8
Size of long_dub_num: 16

To know more, refer to the article – Long in C

‘unsigned’ Type Modifier in C

We can store positive and negative values in integer data type but many times we don’t need to store the negatives values therefore the size reserved for negative range is wasted. We can utilize that space to store positive values using ‘unsigned’ data type modifier.

After using ‘unsigned’ with a data type we cannot store negative values in it. For example, if the range of int is “-2,147,483,648 to 2,147,483,647” then after using unsigned with int the range became “0 to 4,294,967,295”.

Data Types with Unsigned

  • unsigned int
  • unsigned short int
  • unsigned long int
  • unsigned long long int

Example

In the below example we initialize the integer variable num1 and an unsigned integer variable num2 and store a large value in them we can see in the output that num1 value is -1 because num1 is of int type and cannot hold larger values, and on the other hand num2 is of ‘unsigned int’ type and can hold large value up to 429496295.

C




// C Program to illustrate difference between int and
// unsigned int
#include <stdio.h>
  
int main()
{
  
    int num1 = 4294967295;
    unsigned int num2 = 4294967295;
  
    printf("num1: %d", num1);
    printf("\nnum2: %u", num2);
  
    return 0;
}


Output

num1: -1
num2: 4294967295

‘signed’ Type Modifier in C

It is default modifier of int and char data type if no modifier is specified. It indicates that we can store both negative and positive values.

Example

C++




// C Program to illustrate int and signed int
#include <stdio.h>
  
int main()
{
  
    int num1 = 248;
    signed int num2 = 23124;
  
    printf("size of num1: %d", sizeof(num1));
    printf("\nsize of num2: %u", sizeof(num2));
  
    return 0;
}


Output

size of num1: 4
size of num2: 4

Related Article:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads