Type Modifiers in C
💻 Type Modifiers in C (short, long, signed, unsigned)
In C (developed by Dennis Ritchie), type modifiers are used to modify the size or range of basic data types like int and char.
👉 They help control:
- Memory usage 📦
- Range of values 🔢
🔹 What are Type Modifiers? 🤔
Type modifiers are keywords that change how a data type behaves
👉 Main modifiers:
-
short -
long -
signed -
unsigned
🔹 1. short 🧩
Used to reduce memory size of integer
✅ Example:
short int a = 100;
📌 Size: 2 bytes
📌 Range: -32,768 to 32,767
👉 Same as:
short a;
🔹 2. long 📏
Used to increase size of integer or double
✅ Example:
long int b = 100000;
📌 Size: 4 or 8 bytes (system dependent)
👉 For large decimal numbers:
long double pi = 3.141592653589793;
📌 Very high precision 🎯
🔹 3. signed ➕➖
Used to store both positive and negative values
👉 By default, int is signed
✅ Example:
signed int x = -50;
📌 Range: - to +
🔹 4. unsigned ➕
Used to store only positive values (0 and above)
👉 Negative values not allowed ❌
✅ Example:
unsigned int y = 50;
📌 Range: 0 to large positive number
🔹 Important Concept 🧠
| Modifier | Meaning |
|---|---|
| short | smaller size |
| long | bigger size |
| signed | + and - values |
| unsigned | only + values |
🔹 All Combination Examples 🔥
✔ Integer Combinations
short int a = 10;
unsigned short int b = 20;
int c = -30;
signed int d = -40;
unsigned int e = 50;
long int f = 100000;
unsigned long int g = 200000;
long long int h = 123456789;
unsigned long long int i = 987654321;
✔ Character Combinations
char ch1 = 'A';
signed char ch2 = -10;
unsigned char ch3 = 255;
✔ Floating Type Combinations
float f1 = 10.5;
double d1 = 20.12345;
long double d2 = 30.123456789;
👉 Note:
-
signed/unsigned→ NOT used with float/double ❌ -
Only
longworks with double
🔹 Example Program 🧠
#include <stdio.h>
int main() {
short int a = 10;
unsigned int b = 20;
long int c = 100000;
long double d = 3.14159;
printf("%d %u %ld %Lf", a, b, c, d);
return 0;
}
🔹 Format Specifiers 📢
| Type | Format |
|---|---|
| short int | %d |
| unsigned int | %u |
| long int | %ld |
| long double | %Lf |
🔹 Real-Life Understanding 🤔
👉 short = small box 📦
👉 long = big box 📦
👉 signed = can go below zero ⬇️
👉 unsigned = only above zero ⬆️
🔹 Final Summary 📝
-
short→ less memory -
long→ more memory -
signed→ + and - -
unsigned→ only +
Comments
Post a Comment