0
0
Cprogramming~5 mins

Type modifiers in C

Choose your learning style9 modes available
Introduction

Type modifiers change the size or sign of a variable's data type. They help you store data more efficiently or correctly.

When you want to store only positive numbers to save memory using 'unsigned'.
When you need a larger or smaller range of numbers using 'long' or 'short'.
When you want to make sure a number is always signed or unsigned.
When you want to optimize memory usage for embedded systems.
When you want to clarify the type size for portability across different machines.
Syntax
C
type_modifier data_type variable_name;

Type modifiers come before the base data type.

Common modifiers are signed, unsigned, short, and long.

Examples
This declares an integer variable that can only hold positive values.
C
unsigned int age;
This declares an integer variable that can hold larger values than a normal int.
C
long int distance;
This declares an integer variable that uses less memory for smaller numbers.
C
short int temperature;
This declares a character variable that can hold positive or negative values.
C
signed char letter;
Sample Program

This program shows how to declare variables with different type modifiers and prints their values.

C
#include <stdio.h>

int main() {
    unsigned int positiveNumber = 300;
    signed int negativeNumber = -300;
    long int bigNumber = 1000000;
    short int smallNumber = 10;

    printf("Positive number: %u\n", positiveNumber);
    printf("Negative number: %d\n", negativeNumber);
    printf("Big number: %ld\n", bigNumber);
    printf("Small number: %d\n", smallNumber);

    return 0;
}
OutputSuccess
Important Notes

Using 'unsigned' means the variable cannot hold negative values.

'long' and 'short' change the size of the variable, but exact sizes depend on the system.

Always match the correct format specifier in printf for the type modifier used.

Summary

Type modifiers adjust the size or sign of data types.

Common modifiers: signed, unsigned, short, long.

They help store data efficiently and correctly.