๐ฆ What is a Variable?
A variable is a named storage location in memory that holds a value which your program can read or change during its execution.
๐ Real-life Analogy:
Imagine you have labeled containers in your kitchen:
One labeled โSugarโ with 2kg inside
One labeled โSaltโ with 1kg
In C:
The label is the variable name (e.g.,
sugar)The container is the memory space
The content is the value (e.g.,
2)
โ๏ธ Declaring a Variable
To use a variable in C, you need to declare it like this:
data_type variable_name;You can also initialize (assign a value) at the same time:
int age = 25;๐งพ Examples with Explanation:
int marks = 85; // 'marks' is an integer variable storing 85
float weight = 67.8; // 'weight' is a float variable storing 67.8
char grade = 'A'; // 'grade' is a character variable storing 'A'Each line:
Creates a box in memory
Labels it with the variable name
Stores a value inside it
๐งฌ What is a Data Type?
A data type tells the computer what kind of data is being stored in a variable โ like whether itโs:
A whole number?
A decimal?
A character?
Just like we use different containers for water, grains, or milk, C uses different data types to store different kinds of information.
๐งพ Format Specifiers
In C, format specifiers are used in functions like printf() and scanf() to tell the compiler what type of data you're working with.
๐ Common Format Specifiers:
Specifier | Data Type | Example Value | Example |
|---|---|---|---|
| int | 10 |
|
| float/double | 3.14 |
|
| float with 2 decimals | 3.1415 |
|
| char | 'A' |
|
| string | "Hello" |
|
| unsigned int | 15 |
|
| long int | 100000L |
|
| double | 3.1415 |
|
| Hexadecimal | 255 |
|
| pointer address |
|
|
๐ก Example with multiple specifiers:
int age = 25;
float height = 5.9;
char grade = 'A';
printf("Age: %d, Height: %.1f, Grade: %c", age, height, grade);Age: 25, Height: 5.9, Grade: Aโ ๏ธ Notes:
Use correct specifier for correct data type โ mismatches can cause wrong output or crash.
%fis used for bothfloatanddoubleinprintf().Always match order of arguments with the format string.
๐ Types of Data Types in C
๐น Basic (Primary) Data Types
These are the most commonly used types.
Data Type | Description | Size (Typical*) | Format Specifier |
|---|---|---|---|
| Integer (whole number) | 2 or 4 bytes |
|
| Decimal (single precision) | 4 bytes |
|
| Decimal (double precision) | 8 bytes |
|
| Single character | 1 byte |
|
| No value | 0 bytes | โ |
๐ Note: Size may vary based on the system (32-bit vs 64-bit)
๐น Derived Data Types
These are made using basic types:
Arrays
Pointers
Functions
Structures
๐น Enumeration (enum)
An enum is a user-defined data type in C that consists of named integer constants. It is used to assign names to a set of integral values, making code easier to read and maintain.
๐ง Syntax:
enum enum_name {
VALUE1,
VALUE2,
VALUE3,
...
}By default, VALUE1 = 0, VALUE2 = 1, and so on. You can also manually assign values.
๐ก Example 1 โ Default Values:
#include <stdio.h>
enum Weekday {
MONDAY, // 0
TUESDAY, // 1
WEDNESDAY,// 2
THURSDAY, // 3
FRIDAY // 4
};
int main() {
enum Weekday today = WEDNESDAY;
printf("Day number: %d\\n", today); // Output: 2
return 0;
}๐ก Example 2 โ Custom Values:
enum Status {
SUCCESS = 1,
FAILURE = 0,
PENDING = -1
};You can now use SUCCESS, FAILURE, PENDING as named constants in your program.
๐ Behind the Scenes:
An enum is essentially an int, so you can:
Assign
enumvalues tointUse in
switchstatementsCompare using logical operators
๐ฅ Memory Size of Data Types
You can check memory size using the sizeof() function:
#include <stdio.h>
int main() {
printf("Size of int: %lu bytes\\n", sizeof(int));
printf("Size of float: %lu bytes\\n", sizeof(float));
printf("Size of double: %lu bytes\\n", sizeof(double));
printf("Size of char: %lu bytes\\n", sizeof(char));
return 0;
}๐จ๏ธ Sample Output (on 64-bit system):
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte๐ Integer Types (Variants of int)
C allows various integer types depending on memory and range:
Type | Range (approximate) | Size |
|---|---|---|
| -32,768 to 32,767 | 2 bytes |
| 0 to 4,294,967,295 | 4 bytes |
| -2.1 billion to 2.1 billion | 4/8 bytes |
| -9 quintillion to +9 quintillion | 8 bytes |
๐งช Example:
unsigned int age = 30; // Cannot store negative values๐ก Character Type (char)
A
charstores a single character in single quotes like'A'.It is internally stored as an integer (ASCII value).
<div class="infographic-ascii">
<style>
.infographic-ascii {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--code-bg: #1e1e2e;
--code-text: #d4d4e0;
}
.infographic-ascii[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--code-bg: #0d0d14;
--code-text: #d4d4e0;
}
.infographic-ascii {
box-sizing: border-box;
max-width: 920px;
margin: 0 auto;
background: var(--paper);
color: var(--ink);
font-family: 'Inter', -apple-system, sans-serif;
padding: 0 clamp(1.25rem, 5vw, 3rem);
}
.infographic-ascii *, .infographic-ascii *::before, .infographic-ascii *::after {
box-sizing: border-box;
}
.infographic-ascii h1 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 700;
font-size: clamp(1.9rem, 5vw, 2.8rem);
letter-spacing: 0.02em;
margin: 2rem 0 0.25rem;
}
.infographic-ascii .subhead {
color: var(--ink-soft);
font-size: 0.95rem;
margin-bottom: 1.5rem;
}
.infographic-ascii .lookup-row {
display: flex;
align-items: center;
gap: 0.75rem;
flex-wrap: wrap;
margin-bottom: 0.6rem;
padding: 0.9rem 1.1rem;
background: var(--accent-soft);
border-radius: 8px;
}
.infographic-ascii .lookup-row label {
font-size: 0.85rem;
font-weight: 600;
color: var(--ink);
}
.infographic-ascii .lookup-row input {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.95rem;
padding: 0.4rem 0.6rem;
border-radius: 6px;
border: 1px solid var(--line);
background: var(--paper);
color: var(--ink);
width: 8rem;
}
.infographic-ascii .lookup-row input:focus {
outline: 2px solid var(--accent);
outline-offset: 1px;
}
.infographic-ascii .lookup-hint {
font-size: 0.78rem;
color: var(--ink-soft);
font-style: italic;
}
.infographic-ascii .lookup-result {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.85rem;
color: var(--accent);
font-weight: 600;
min-height: 1.3rem;
}
.infographic-ascii .table-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 0 1.25rem;
margin-bottom: 1.5rem;
margin-top: 1.5rem;
}
@media (max-width: 820px) {
.infographic-ascii .table-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 460px) {
.infographic-ascii .table-grid { grid-template-columns: 1fr; }
}
.infographic-ascii .table-group {
border-top: 2px solid var(--ink);
padding-top: 0.4rem;
}
.infographic-ascii .col-headers {
display: grid;
grid-template-columns: 2.4rem 2.4rem 1fr;
gap: 0.4rem;
font-size: 0.68rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.03em;
color: var(--ink-soft);
padding: 0.2rem 0.3rem;
margin-bottom: 0.15rem;
}
.infographic-ascii .ascii-row {
display: grid;
grid-template-columns: 2.4rem 2.4rem 1fr;
gap: 0.4rem;
padding: 0.22rem 0.3rem;
border-radius: 4px;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.78rem;
transition: background 0.2s, transform 0.2s;
}
.infographic-ascii .ascii-row:nth-child(even) {
background: var(--paper-alt);
}
.infographic-ascii .ascii-row .dec { color: var(--ink-soft); }
.infographic-ascii .ascii-row .hex { color: var(--accent); }
.infographic-ascii .ascii-row .char { color: var(--ink); font-weight: 600; }
.infographic-ascii .ascii-row .char.ctrl {
font-style: italic;
font-weight: 400;
color: var(--ink-soft);
font-size: 0.72rem;
}
.infographic-ascii .ascii-row.is-match {
background: var(--accent);
transform: scale(1.03);
}
.infographic-ascii .ascii-row.is-match .dec,
.infographic-ascii .ascii-row.is-match .hex,
.infographic-ascii .ascii-row.is-match .char {
color: #fff;
}
.infographic-ascii .takeaway {
border-top: 1px solid var(--line);
padding: 1.25rem 0 2rem;
font-size: 0.95rem;
color: var(--ink-soft);
}
.infographic-ascii .takeaway strong { color: var(--ink); }
@media (prefers-reduced-motion: reduce) {
.infographic-ascii * { transition: none !important; }
}
</style>
<h1>ASCII TABLE</h1>
<p class="subhead">All 128 characters โ look one up, or scan the full set below.</p>
<div class="lookup-row">
<label for="ascii-lookup">Look up:</label>
<input type="text" id="ascii-lookup" placeholder="A, 65, or 0x41">
<span class="lookup-hint">character, decimal, or hex</span>
<span class="lookup-result" id="ascii-lookup-result"></span>
</div>
<div class="table-grid" id="ascii-grid"></div>
<p class="takeaway"><strong>0โ31 and 127</strong> are non-printable control codes; <strong>32</strong> is the space; everything else is a printable character.</p>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-ascii')
|| document.querySelector('.infographic-ascii');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
const CONTROL_NAMES = {
0: 'NULL', 1: 'START OF HEADING', 2: 'START OF TEXT', 3: 'END OF TEXT',
4: 'END OF TRANSMISSION', 5: 'ENQUIRY', 6: 'ACKNOWLEDGE', 7: 'BELL',
8: 'BACKSPACE', 9: 'HORIZONTAL TAB', 10: 'LINE FEED', 11: 'VERTICAL TAB',
12: 'FORM FEED', 13: 'CARRIAGE RETURN', 14: 'SHIFT OUT', 15: 'SHIFT IN',
16: 'DATA LINK ESCAPE', 17: 'DEVICE CONTROL 1', 18: 'DEVICE CONTROL 2',
19: 'DEVICE CONTROL 3', 20: 'DEVICE CONTROL 4', 21: 'NEGATIVE ACKNOWLEDGE',
22: 'SYNCHRONOUS IDLE', 23: 'END OF TRANS. BLOCK', 24: 'CANCEL',
25: 'END OF MEDIUM', 26: 'SUBSTITUTE', 27: 'ESCAPE', 28: 'FILE SEPARATOR',
29: 'GROUP SEPARATOR', 30: 'RECORD SEPARATOR', 31: 'UNIT SEPARATOR',
32: 'SPACE', 127: 'DEL'
};
const grid = wrapper.querySelector('#ascii-grid');
const rows = [];
for (let g = 0; g < 4; g++) {
const group = document.createElement('div');
group.className = 'table-group';
const headers = document.createElement('div');
headers.className = 'col-headers';
headers.innerHTML = '<span>Dec</span><span>Hex</span><span>Char</span>';
group.appendChild(headers);
for (let i = g * 32; i < g * 32 + 32; i++) {
const row = document.createElement('div');
row.className = 'ascii-row';
row.dataset.dec = i;
const hex = i.toString(16).toUpperCase().padStart(2, '0');
const isCtrl = CONTROL_NAMES.hasOwnProperty(i);
const charDisplay = isCtrl ? '[' + CONTROL_NAMES[i] + ']' : String.fromCharCode(i);
row.innerHTML = '<span class="dec">' + i + '</span>' +
'<span class="hex">' + hex + '</span>' +
'<span class="char' + (isCtrl ? ' ctrl' : '') + '">' + charDisplay + '</span>';
group.appendChild(row);
rows.push(row);
}
grid.appendChild(group);
}
const lookupInput = wrapper.querySelector('#ascii-lookup');
const lookupResult = wrapper.querySelector('#ascii-lookup-result');
function clearHighlight() {
rows.forEach(r => r.classList.remove('is-match'));
}
function doLookup() {
const raw = lookupInput.value.trim();
clearHighlight();
if (!raw) { lookupResult.textContent = ''; return; }
let code = null;
if (/^0x[0-9a-f]+$/i.test(raw)) {
code = parseInt(raw, 16);
} else if (/^\d+$/.test(raw)) {
code = parseInt(raw, 10);
} else {
code = raw.charCodeAt(0);
}
if (code === null || isNaN(code) || code < 0 || code > 127) {
lookupResult.textContent = 'Out of ASCII range (0โ127)';
return;
}
const row = rows[code];
row.classList.add('is-match');
row.scrollIntoView({ block: 'center', behavior: 'smooth' });
const hex = code.toString(16).toUpperCase().padStart(2, '0');
const label = CONTROL_NAMES.hasOwnProperty(code) ? '[' + CONTROL_NAMES[code] + ']' : "'" + String.fromCharCode(code) + "'";
lookupResult.textContent = 'Decimal ' + code + ' ยท Hex ' + hex + ' ยท ' + label;
}
lookupInput.addEventListener('input', doLookup);
})();
</script>Example:
char grade = 'A'; // 'A' = 65 in ASCII
printf("%d", grade); // Output: 65๐งฎ Floating-Point Types (float, double)
Used for storing decimal values.
float pi = 3.14;
double price = 99.999999;Type | Precision | Size |
|---|---|---|
| 6 digits | 4 bytes |
| 15 digits | 8 bytes |
๐งช Mixing Data Types
If you mix types in operations, type promotion happens automatically:
int a = 5;
float b = 2.0;
float result = a + b; // a is promoted to floatโ ๏ธ Common Mistakes
Mistake | Correction |
|---|---|
Declaring without data type | โ |
Wrong type assignment | โ |
Using a variable without declaring it | โ |