Type Modifiers in C
Sometimes int is too small, and sometimes you never need negative numbers. C lets you adjust a basic type with four words: signed, unsigned, short and long.
These are called type modifiers. In this lesson you will learn what each one does to the size and range of a type.
What You Will Learn
- What type modifiers are.
- The difference between
signedandunsigned. - What
short,longandlong longdo. - The size and range table of integer types.
- What happens when a value goes out of range (overflow).
Type Modifiers in C Explained
Exam definition: Type modifiers are keywords used with basic data types to change their size or range. C has four modifiers: signed, unsigned, short and long.
| Modifier | What it does | Used with |
|---|---|---|
signed | Allows positive and negative values (default for int) | char, int |
unsigned | Only zero and positive values, so the positive range doubles | char, int |
short | Smaller size (usually 2 bytes) | int |
long | Bigger size | int, double |
Memory trick: signed = has a sign (+ or −). unsigned = no sign, so no negatives. short = small box, long = big box.
Size and range table (usual on 64-bit systems)
| Type | Size | Range | Format |
|---|---|---|---|
signed char | 1 byte | −128 to 127 | %c / %d |
unsigned char | 1 byte | 0 to 255 | %c / %u |
short | 2 bytes | −32,768 to 32,767 | %hd |
unsigned short | 2 bytes | 0 to 65,535 | %hu |
int | 4 bytes | −2,147,483,648 to 2,147,483,647 | %d |
unsigned int | 4 bytes | 0 to 4,294,967,295 | %u |
long | 8 bytes (4 on Windows) | Same as long long (or int on Windows) | %ld |
long long | 8 bytes | about ±9.22 × 1018 | %lld |
long double | 8, 12 or 16 bytes | More precision than double | %Lf |
Why does unsigned double the positive range?
- A 1-byte
charcan hold 256 different values. - signed: half for negatives, half for zero and positives → −128 to 127.
- unsigned: all 256 values for zero and positives → 0 to 255.
Syntax of Type Modifiers
unsigned int students = 4000000000u;
short int year = 2026; /* "int" can be left out: short year */
long long stars = 9000000000LL;
unsigned char pixel = 255;
short int and short mean the same. unsigned alone means unsigned int. The suffixes u and LL mark a number as unsigned or long long.
Example 1: Print the Range of int Using limits.h
The header limits.h has ready-made names for the smallest and largest values of each type.
#include <stdio.h>
#include <limits.h>
int main(void)
{
printf("int minimum : %d\n", INT_MIN);
printf("int maximum : %d\n", INT_MAX);
printf("unsigned int max: %u\n", UINT_MAX);
printf("short maximum : %d\n", SHRT_MAX);
return 0;
}
Output
int minimum : -2147483648 int maximum : 2147483647 unsigned int max: 4294967295 short maximum : 32767
Code Explanation
#include <limits.h>: brings in the limit names.INT_MIN,INT_MAX: smallest and largestint.UINT_MAX: largestunsigned int. It is printed with%u(unsigned).SHRT_MAX: largestshort.
How Range Works (Overflow)
- Each type has a fixed number of bits, so it can only hold values in its range.
- For unsigned types, going past the maximum wraps around to 0, like a car's odometer going from 99999 to 00000.
- For signed types, going past the maximum is undefined behaviour: the result is unpredictable. Never rely on it.
- So choose a type big enough for your data. Population of India? Use
long long, notint.
Example 2: Unsigned Overflow Wraps Around
#include <stdio.h>
int main(void)
{
unsigned char level = 255; /* the maximum for unsigned char */
printf("Before: %u\n", level);
level = level + 1; /* goes past the maximum */
printf("After : %u\n", level);
return 0;
}
Output
Before: 255 After : 0
255 + 1 does not fit in 1 byte, so it wraps around to 0. Bugs like this happen when the type is too small.
Important Points to Remember
- 4 type modifiers: signed, unsigned, short, long.
intis signed by default.unsigned= no negative values, double the positive range.shortis usually 2 bytes;long longis 8 bytes.- Unsigned overflow wraps to 0; signed overflow is undefined.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Storing −5 in an unsigned int | Becomes a huge positive number (4294967291) | Use a signed type for values that can be negative |
Printing long long with %d | Wrong value | Use %lld |
Using int for very big values | Overflow, wrong results | Use long long |
Assuming long is 8 bytes everywhere | It is 4 bytes on Windows | Use long long for 8 bytes |
Type Modifiers Practice Questions
- Name the four type modifiers in C.
- What is the difference between
signedandunsigned? - What is the range of
unsigned char? - Which format specifier prints
long long? - What happens when an
unsigned charwith value 255 is increased by 1?
Show answers
signed,unsigned,short,long.signedstores positive and negative values;unsignedstores only zero and positive values, with double the positive range.- 0 to 255.
%lld- It wraps around to 0.
Coding Exercise
Write a program that stores the distance from the Earth to the Sun in kilometres (149600000) in a suitable type and the number of seconds in 100 years (100 × 365 × 24 × 60 × 60) in a long long. Print both.
Show solution
#include <stdio.h>
int main(void)
{
long long distance_km = 149600000LL;
long long seconds = 100LL * 365 * 24 * 60 * 60;
printf("Earth to Sun: %lld km\n", distance_km);
printf("Seconds in 100 years: %lld\n", seconds);
return 0;
}
Earth to Sun: 149600000 km Seconds in 100 years: 3153600000
3,153,600,000 is bigger than INT_MAX, so int would overflow. Writing 100LL makes the whole calculation use long long.
Quick Quiz: Type Modifiers in C (MCQ)
-
Which is NOT a type modifier?
A)
shortB)longC)staticD)unsigned -
The range of
unsigned charis:A) −128 to 127 B) 0 to 255 C) 0 to 127 D) −255 to 255
-
The usual size of
shortis:A) 1 byte B) 2 bytes C) 4 bytes D) 8 bytes
-
Which format specifier prints an
unsigned int?A)
%dB)%uC)%ldD)%f -
intis ______ by default.A) unsigned B) signed C) short D) long
Show answers
- C:
staticis a storage class, not a type modifier. - B: 0 to 255.
- B: 2 bytes.
- B:
%u. - B: signed.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Modifiers | signed, unsigned, short, long |
|---|---|
| signed char | −128 to 127 |
| unsigned char | 0 to 255 |
| short | 2 bytes, −32,768 to 32,767 |
| int | 4 bytes, about ±2.1 billion |
| unsigned int max | 4,294,967,295 |
| long long | 8 bytes, %lld |
| Limits header | limits.h (INT_MAX, INT_MIN) |
Frequently asked questions
- What is the difference between signed and unsigned int?
- signed holds negative and positive values; unsigned holds only positive values with double the positive range.
- What happens on unsigned overflow?
- The value wraps around to 0.
- What happens on signed overflow?
- It is undefined behaviour: the result cannot be predicted.
- Is long always 8 bytes?
- No. It is 8 bytes on 64-bit Linux and macOS but 4 bytes on Windows.
long longis always at least 8 bytes.
Remember
- signed = has a sign; unsigned = no negatives.
- Formats:
%uunsigned,%hdshort,%lldlong long.
Next Topic
Next lesson: Constants and Literals in C. You will learn how to create values that can never change.