Data Types & Variables
Learn to store different kinds of data: numbers (int), decimals (float), and characters (char). Understand how much memory each type uses.
Track Your Progress
Sign in to save your learning progress
What You Will Learn
- ✓Declare variables to store data
- ✓Choose the right data type (int, float, char)
- ✓Know the size and range of each type
- ✓Create constants that never change
01What are Data Types?
A data type tells the computer:
What kind of data?
Is it a number? A letter? A word? Each type stores different kinds of information.
How much memory?
Different types need different amounts of space. An int uses 4 bytes, a char uses only 1 byte.
Quick Example
1#include <stdio.h>23int main() {4 int age = 25; // Whole number5 double price = 19.99; // Decimal number6 char grade = 'A'; // Single character7 8 printf("Age: %d\n", age);9 printf("Price: %.2f\n", price);10 printf("Grade: %c\n", grade);11 12 return 0;13}Age: 25
Price: 19.99
Grade: A
Categories of Data Types
| Category | Data Types | What They Store |
|---|---|---|
| Integer Types | int, short, long, long long | Whole numbers (no decimals) |
| Floating-Point | float, double, long double | Decimal numbers |
| Character | char | Single characters ('A', 'b', '@') |
| Boolean | _Bool, bool (C99+) | True or False |
| Void | void | No value / Unknown type |
02Integer Types (Whole Numbers)
Integers store whole numbers without decimals. C has several integer types with different sizes.
| Type | Size | Range | When to Use |
|---|---|---|---|
| char | 1 byte | -128 to 127 | Tiny numbers, characters |
| short | 2 bytes | -32,768 to 32,767 | Small numbers, save memory |
| int | 4 bytes | -2.1 billion to 2.1 billion | Most common! Use this. |
| long | 4 or 8 bytes | ±2.1B or ±9.2 quintillion | Large numbers |
| long long | 8 bytes | ±9.2 quintillion | Very large numbers |
Signed vs Unsigned
signed (default)
Can store negative AND positive numbers.
int age = -5; // OKint temp = 100; // OKsigned int x = -1; // Same as intunsigned
Only positive numbers (0 and up). Double the max value!
unsigned int count = 0; // OKunsigned int x = 4000000000; // OK (more than int max)unsigned int y = -1; // BAD! Becomes huge numberSimple Integer Program
1#include <stdio.h>23int main() {4 int age = 25;5 int score = 100;6 int temperature = -10;7 8 printf("Age: %d years\n", age);9 printf("Score: %d points\n", score);10 printf("Temperature: %d degrees\n", temperature);11 12 // Simple calculation13 int sum = age + score;14 printf("Age + Score = %d\n", sum);15 16 return 0;17}Age: 25 years
Score: 100 points
Temperature: -10 degrees
Age + Score = 125
Rule of Thumb
Just use int for most cases! Only use other types when you have a specific reason (saving memory, need larger range, etc.)
03Floating-Point Types (Decimal Numbers)
Floating-point types store numbers with decimal points, like 3.14, -0.5, or 123.456.
| Type | Size | Precision | When to Use |
|---|---|---|---|
| float | 4 bytes | ~7 decimal digits | Save memory, graphics |
| double | 8 bytes | ~15 decimal digits | Most common! Use this. |
| long double | 8-16 bytes | ~18+ decimal digits | Scientific calculations |
Simple Float Program
1#include <stdio.h>23int main() {4 double price = 19.99;5 double discount = 0.20; // 20% off6 7 // Calculate discounted price8 double savings = price * discount;9 double final_price = price - savings;10 11 printf("Original Price: $%.2f\n", price);12 printf("Discount (20%%): $%.2f\n", savings);13 printf("Final Price: $%.2f\n", final_price);14 15 return 0;16}Original Price: $19.99
Discount (20%): $4.00
Final Price: $15.99
Important Notes
- • Add
fsuffix for float literals:3.14f - • Add
Lsuffix for long double:3.14L - • Without suffix, decimals are treated as
double
04Character Type (char)
The char type stores a single character — one letter, digit, or symbol. Characters use single quotes: 'A'
Size
1 byte
8 bits
Range
-128 to 127
or 0 to 255 (unsigned)
Stores
1 character
ASCII value internally
Simple Character Program
1#include <stdio.h>23int main() {4 char first = 'J'; // First initial5 char middle = 'K'; // Middle initial6 char last = 'R'; // Last initial7 8 printf("Initials: %c.%c.%c.\n", first, middle, last);9 10 // Characters are numbers! (ASCII codes)11 printf("'A' as number: %d\n", 'A'); // 6512 printf("'a' as number: %d\n", 'a'); // 9713 14 // Character math15 char next = 'A' + 1; // B (65 + 1 = 66)16 printf("Letter after A: %c\n", next);17 18 return 0;19}Initials: J.K.R.
'A' as number: 65
'a' as number: 97
Letter after A: B
Common ASCII Values
| Character | ASCII | Character | ASCII | Character | ASCII |
|---|---|---|---|---|---|
| '0' - '9' | 48 - 57 | 'A' - 'Z' | 65 - 90 | 'a' - 'z' | 97 - 122 |
char vs String
'A' (single quotes) = one character"A" (double quotes) = string (array of characters)
05Modern Data Types (C99/C11/C23)
Newer versions of C introduced additional data types for specific needs. Here are the most useful ones you should know about.
Boolean Type (true/false)
C99 - C17
Include stdbool.h
#include <stdbool.h>bool isActive = true;bool isComplete = false;C23 (Latest)
No include needed!
// bool, true, false are keywords!bool isActive = true;bool isComplete = false;1#include <stdio.h>2#include <stdbool.h> // For C99-C1734int main() {5 bool isRaining = false;6 bool hasUmbrella = true;7 8 printf("Is it raining? %d\n", isRaining); // 0 (false)9 printf("Have umbrella? %d\n", hasUmbrella); // 1 (true)10 11 // Use in conditions12 if (isRaining && !hasUmbrella) {13 printf("You'll get wet!\n");14 } else {15 printf("You're fine!\n");16 }17 18 return 0;19}Is it raining? 0
Have umbrella? 1
You're fine!
# Fixed-Width Integers (C99)
Regular int size varies by system. For guaranteed sizes, use types from <stdint.h>:
| Type | Size | Range | Use Case |
|---|---|---|---|
| int8_t / uint8_t | 1 byte | -128 to 127 / 0 to 255 | Byte-level data, RGB colors |
| int16_t / uint16_t | 2 bytes | -32K to 32K / 0 to 65K | Audio samples, Unicode |
| int32_t / uint32_t | 4 bytes | -2.1B to 2.1B / 0 to 4.3B | General integers |
| int64_t / uint64_t | 8 bytes | ±9.2 quintillion | File sizes, timestamps |
1#include <stdio.h>2#include <stdint.h> // For fixed-width types34int main() {5 // Exact sizes - works the same on ANY computer!6 int8_t tiny = 100; // Exactly 1 byte7 int32_t normal = 1000000; // Exactly 4 bytes8 uint64_t big = 9000000000; // Exactly 8 bytes (unsigned)9 10 // Great for colors (0-255 each)11 uint8_t red = 255;12 uint8_t green = 128;13 uint8_t blue = 0;14 15 printf("Color: RGB(%d, %d, %d)\n", red, green, blue);16 printf("Big number: %llu\n", big);17 18 return 0;19}Color: RGB(255, 128, 0)
Big number: 9000000000
size_t (For Sizes and Counts)
size_t is an unsigned type designed to hold any size or count. It's what sizeof returns.
1#include <stdio.h>2#include <stddef.h> // or <stdlib.h>34int main() {5 int numbers[5] = {10, 20, 30, 40, 50};6 7 // size_t for array sizes and loop counters8 size_t length = sizeof(numbers) / sizeof(numbers[0]);9 10 printf("Array has %zu elements:\n", length); // %zu for size_t11 12 for (size_t i = 0; i < length; i++) {13 printf(" numbers[%zu] = %d\n", i, numbers[i]);14 }15 16 return 0;17}Array has 5 elements:
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
C23 New Types (Latest Standard)
Note: C23 is the newest standard (2023). Not all compilers fully support it yet. Use GCC 13+ or Clang 16+ with -std=c23.
| C23 Type | What It Does | Example |
|---|---|---|
| bool | Now a keyword (no header needed) | bool flag = true; |
| nullptr | Type-safe null pointer | int *ptr = nullptr; |
| _BitInt(N) | Arbitrary-width integers | _BitInt(128) huge; |
| typeof(x) | Get the type of an expression | typeof(x) y = x; |
1// C23 Example (requires -std=c23)2#include <stdio.h>34int main() {5 // bool is now a keyword!6 bool isReady = true;7 8 // nullptr for pointers (safer than NULL)9 int *ptr = nullptr;10 11 if (ptr == nullptr) {12 printf("Pointer is null\n");13 }14 15 // typeof - copy the type of another variable16 int x = 42;17 typeof(x) y = 100; // y is also an int18 19 printf("x = %d, y = %d\n", x, y);20 21 return 0;22}Quick Reference
| Need | Use | Header |
|---|---|---|
| True/False values | bool | <stdbool.h> (C99-C17) / none (C23) |
| Exact 8/16/32/64-bit integers | int8_t, int32_t, etc. | <stdint.h> |
| Array sizes, counts | size_t | <stddef.h> or <stdlib.h> |
| Null pointers (C23) | nullptr | none (keyword) |
For Beginners
Focus on int, double, and char first. Learn bool and fixed-width types when you need them for specific projects.
06What is a Variable?
A variable is a named container that stores a value in your computer's memory.
Think of it like a labeled box
age
int
price
double
grade
char
Simple Example
1#include <stdio.h>23int main() {4 // Create a variable named "age" and store 25 in it5 int age = 25;6 7 // Print the value stored in "age"8 printf("Your age is: %d\n", age);9 10 // Change the value (variable = can change!)11 age = 26;12 printf("Next year: %d\n", age);13 14 return 0;15}Your age is: 25
Next year: 26
Why Do We Need Variables?
✓ Store data for later
Save user input, calculation results, or any value you need to use later.
✓ Give data a meaningful name
age is clearer than just 25.
✓ Change values as needed
Unlike constants, variables can be updated during program execution.
✓ Reuse values multiple times
Calculate once, use the result in many places.
07Declaring Variables
Declaration = telling the computer "I need a variable with this name and type."
Syntax
1// Declaring variables (reserving space in memory)2int age; // An integer named "age"3double salary; // A double named "salary"4char initial; // A character named "initial"56// Declaring multiple variables of the same type7int x, y, z; // Three integers8float width, height; // Two floatsVariable Naming Rules
Variable names must follow specific rules (start with letter, no spaces, etc.). Use descriptive names like studentAge instead of x.
08Initializing Variables
Initialization = giving a variable its first value. You can do this at declaration or later.
1. At Declaration
Recommended! ✓
int age = 25;double pi = 3.14;char grade = 'A';2. After Declaration
Also valid
int age;age = 25; // Assign laterdouble pi;pi = 3.14;3. Without Value
Dangerous!
int age;// age contains GARBAGE!// Random memory valueprintf("%d", age); // BAD!Complete Example
1#include <stdio.h>23int main() {4 // Create and initialize variables5 int age = 25;6 double price = 19.99;7 char grade = 'A';8 9 printf("Before:\n");10 printf(" Age: %d, Price: $%.2f, Grade: %c\n", age, price, grade);11 12 // Change the values13 age = 26; // Birthday!14 price = 24.99; // Price went up15 grade = 'B'; // Grade changed16 17 printf("After:\n");18 printf(" Age: %d, Price: $%.2f, Grade: %c\n", age, price, grade);19 20 return 0;21}Before:
Age: 25, Price: $19.99, Grade: A
After:
Age: 26, Price: $24.99, Grade: B
Always Initialize!
Uninitialized variables contain garbage values — random data left in memory. This causes unpredictable bugs!
09Constants (Values That Don't Change)
Constants are like variables, but their value cannot be changed after initialization.
const Keyword
Creates a typed constant (recommended)
const int MAX_AGE = 120;const double PI = 3.14159;const char NEWLINE = '\n';// MAX_AGE = 130; // ERROR! Can't change#define Macro
Text replacement (no type, no semicolon)
#define MAX_SIZE 100#define PI 3.14159#define GREETING "Hello"// These are replaced before compilationConstants Example
1#include <stdio.h>23#define TAX_RATE 0.08 // 8% tax (no semicolon!)45int main() {6 const double PRICE = 100.0; // Can't change this!7 8 double tax = PRICE * TAX_RATE;9 double total = PRICE + tax;10 11 printf("Price: $%.2f\n", PRICE);12 printf("Tax (8%%): $%.2f\n", tax);13 printf("Total: $%.2f\n", total);14 15 // PRICE = 200.0; // ERROR! Can't change a const16 17 return 0;18}Price: $100.00
Tax (8%): $8.00
Total: $108.00
10Finding the Size of Data Types
Use the sizeof operator to find how many bytes a data type or variable uses.
1#include <stdio.h>23int main() {4 printf("=== Size of Data Types (in bytes) ===\n\n");5 6 // Integer types7 printf("char: %zu byte\n", sizeof(char));8 printf("short: %zu bytes\n", sizeof(short));9 printf("int: %zu bytes\n", sizeof(int));10 printf("long: %zu bytes\n", sizeof(long));11 printf("long long: %zu bytes\n", sizeof(long long));12 13 printf("\n");14 15 // Floating-point types16 printf("float: %zu bytes\n", sizeof(float));17 printf("double: %zu bytes\n", sizeof(double));18 printf("long double: %zu bytes\n", sizeof(long double));19 20 printf("\n");21 22 // Size of a variable23 int age = 25;24 printf("Variable 'age': %zu bytes\n", sizeof(age));25 26 return 0;27}Typical Output (64-bit system):
short: 2 bytes
int: 4 bytes
long: 8 bytes
long long: 8 bytes
float: 4 bytes
double: 8 bytes
!Code Pitfalls: Common Mistakes & What to Watch For
These are the most common mistakes that trip up beginners. Study them carefully to avoid hours of debugging!
Using uninitialized variables
Wrong:
int x; printf("%d", x);Contains garbage!
Correct:
int x = 0; printf("%d", x);Integer division surprise
Surprising:
int result = 5 / 2;Result is 2, not 2.5!
For decimals:
double result = 5.0 / 2;Result is 2.5 ✓
Missing float suffix
Warning:
float f = 3.14;3.14 is double, gets truncated
Correct:
float f = 3.14f;Wrong quotes for char
Wrong:
char c = "A";Double quotes = string!
Correct:
char c = 'A';Single quotes for char
13Frequently Asked Questions
Q:What's the difference between float and double? Which should I use?
float uses 4 bytes and provides ~7 decimal digits of precision. double uses 8 bytes and provides ~15 decimal digits of precision.
Use double by default! It's the standard for floating-point calculations in C. Only use float when you need to save memory (like in large arrays or graphics programming) and don't need high precision.
Q:Why does int size vary on different computers?
C was designed for portability, so it only guarantees minimum sizes, not exact sizes. The actual size depends on the CPU architecture and compiler.
On 32-bit systems, int is typically 4 bytes. On some embedded systems, it might be 2 bytes. If you need exact sizes, use int32_t, int64_t, etc. from <stdint.h>.
Q:What happens if I store a number too big for its data type?
This is called overflow. For signed integers, the behavior is undefined — your program might crash or give unpredictable results.
For unsigned integers, the value "wraps around." For example, if you store 256 in an unsigned char (max 255), it becomes 0. Store 257? It becomes 1.
This is a common source of bugs! Always check if your values fit within the type's range.
Q:Why is char used for both characters AND small numbers?
In C, characters are just numbers! The char type stores an integer value (typically -128 to 127), and we interpret that number as a character using ASCII codes.
For example, 'A' is stored as 65, 'a' is 97, and '0' is 48. This is why you can do math with characters: 'A' + 1 = 'B'.
Q:Should I use const or #define for constants?
Use const when possible! It's type-safe, respects scope, and the debugger can see it.
const (preferred)
const int MAX = 100;Type-checked, scoped, debuggable
#define (macros)
#define MAX 100Text replacement, no type safety
Use #define only for compile-time constants like array sizes in C89, or for conditional compilation.
13Summary
Key Takeaways
Basic Types
- •
int— whole numbers - •
double— decimals - •
char— single character - •
float— less precise decimal
Modern Types
- •
bool— true/false - •
int32_t— exact sizes - •
size_t— sizes/counts - •
nullptr— null (C23)
Variables
- • Declare:
int x; - • Initialize:
int x = 5; - • Always initialize!
- • Use
constfor constants
Next Steps
Learn naming rules, how to read and print values, do calculations, and convert between types.
Test Your Knowledge
Related Tutorials
Basic Syntax in C
Learn the fundamental building blocks of C programs. Understand the main() function, how to write comments, and why proper formatting makes code readable.
Identifiers in C
Learn the rules for naming things in C - variables, functions, and more. Know what names are valid, and follow naming conventions that professionals use.
Input/Output in C
Make your programs interactive! Learn to display output with printf() and read user input with scanf(). Essential for every C program.
Have Feedback?
Found something missing or have ideas to improve this tutorial? Let us know on GitHub!