π§ What is an "Error"?
An error is any mistake in the program that prevents it from running correctly or giving the desired output.
Errors can happen:
While writing the code (e.g., forgetting a semicolon),
When the code is being compiled,
When the program is being linked,
Or even during execution (after the program starts running).
π§ͺ Why Are Errors Important?
Errors are the signals from the compiler or runtime system telling you something is wrong:
They protect your program from crashing
They help you spot mistakes early
They force good coding habits like checking inputs, handling exceptions, etc.
π Classification of Errors in C
Letβs break them down one by one with simple examples.
1οΈβ£ Syntax Errors (Compile-Time Errors)
π These are grammar mistakes in your C code β violations of the syntax rules defined by the C language.
β The compiler will not compile your code until you fix them.
β Examples:
int main() {
printf("Hello, world!") // β Missing semicolon
}βοΈ Fix:
printf("Hello, world!");π§ Other common syntax errors:
Misspelled keywords (e.g.,
pritnfinstead ofprintf)Mismatched parentheses or braces
Using undeclared variables
2οΈβ£ Semantic Errors
π§ The code is syntactically correct, but it doesn't make logical sense according to the rules of C.
β Compiler may not complain, but your program behaves incorrectly.
β Example:
int x = "hello"; // β Assigning string to an integerβοΈ Fix:
char *x = "hello";3οΈβ£ Logical Errors
π‘ These are thinking mistakes β your logic is flawed.
β The code compiles and runs, but gives wrong output.
β Example:
int a = 5, b = 10;
printf("Average: %d", a + b / 2); // β wrong precedenceExpected: Average of 5 and 10 = 7
Actual Output: 5 + (10 / 2) = 5 + 5 = 10
βοΈ Fix:
printf("Average: %d", (a + b) / 2);4οΈβ£ Runtime Errors (During Execution)
π£ These happen after the program starts running.
β Common with memory problems, invalid input, or math errors.
β Examples:
Divide by zero:
int x = 10 / 0; // βInvalid memory access:
int *p = NULL; *p = 100; // β segmentation faultArray out-of-bound:
int arr[3]; arr[10] = 5; // β Invalid index
5οΈβ£ Linker Errors
π§± After compilation, the linker tries to connect your code with required libraries and symbols.
β These errors occur when the linker canβt find a function, variable, or symbol thatβs used in your code.
β Example:
void greet(); // Declared but never defined
int main() {
greet(); // β Undefined reference to greet
}βοΈ Fix:
void greet() {
printf("Hello!");
}6οΈβ£ Compilation Warnings
β οΈ These are not errors β they don't stop your program from compiling, but they warn you about potential issues that may cause bugs or crashes.
β Examples:
Variable declared but never used
Possible loss of data in type conversion
Uninitialized variable usage
int x;
printf("%d", x); // β οΈ 'x' is used uninitialized<div class="infographic-cerrors">
<style>
.infographic-cerrors {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--code-bg: #1e1e2e;
--code-text: #d4d4e0;
--syn-keyword: #c586c0;
--syn-func: #dcdcaa;
--syn-string: #ce9178;
--warn: #b45309;
--warn-soft: #fdf0dc;
--ok: #1a7f4e;
--ok-soft: #e3f5ec;
--err: #b0281f;
--err-soft: #fbe4e2;
}
.infographic-cerrors[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--warn: #e0a458;
--warn-soft: #3a2c14;
--ok: #5cd6a0;
--ok-soft: #16342a;
--err: #e2695f;
--err-soft: #3a1f1d;
}
.infographic-cerrors {
box-sizing: border-box;
max-width: 880px;
margin: 0 auto;
background: var(--paper);
color: var(--ink);
font-family: 'Inter', -apple-system, sans-serif;
padding: 0 clamp(1rem, 4vw, 2.25rem) 1.25rem;
}
.infographic-cerrors *, .infographic-cerrors *::before, .infographic-cerrors *::after {
box-sizing: border-box;
}
.infographic-cerrors h1 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 600;
font-size: clamp(1.3rem, 3.5vw, 1.65rem);
margin: 1.25rem 0 0.2rem;
text-align: center;
}
.infographic-cerrors .subhead { color: var(--ink-soft); font-size: 0.85rem; margin-bottom: 1.1rem; text-align: center; }
.infographic-cerrors .chip-row { display: flex; justify-content: center; gap: 0.4rem; margin-bottom: 1rem; flex-wrap: wrap; }
.infographic-cerrors .chip {
all: unset;
cursor: pointer;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.74rem;
font-weight: 600;
padding: 0.38rem 0.7rem;
border-radius: 20px;
border: 1px solid var(--line);
color: var(--ink-soft);
transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.infographic-cerrors .chip:hover { border-color: var(--accent); }
.infographic-cerrors .chip.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.infographic-cerrors .tab-panel { display: none; }
.infographic-cerrors .tab-panel.is-active { display: block; }
.infographic-cerrors .code-panel {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 0.6rem 0.9rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.8rem;
line-height: 1.6;
margin-bottom: 0.9rem;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
text-align: center;
}
.infographic-cerrors .code-panel .code-line { display: block; white-space: pre; }
.infographic-cerrors .syn-keyword { color: var(--syn-keyword); }
.infographic-cerrors .syn-func { color: var(--syn-func); }
.infographic-cerrors .syn-string { color: var(--syn-string); }
.infographic-cerrors .sub-chip-row { display: flex; justify-content: center; gap: 0.4rem; margin-bottom: 0.9rem; flex-wrap: wrap; }
.infographic-cerrors .sub-chip {
all: unset;
cursor: pointer;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.76rem;
font-weight: 600;
padding: 0.35rem 0.7rem;
border-radius: 8px;
border: 1px solid var(--line);
color: var(--ink-soft);
}
.infographic-cerrors .sub-chip.is-active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.infographic-cerrors .field-row { display: flex; align-items: center; justify-content: center; gap: 0.5rem; margin-bottom: 0.9rem; flex-wrap: wrap; }
.infographic-cerrors .field-row label { font-family: 'IBM Plex Mono', monospace; font-size: 0.8rem; color: var(--ink-soft); }
.infographic-cerrors .field-row input {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.85rem;
padding: 0.3rem 0.5rem;
border-radius: 6px;
border: 1px solid var(--line);
background: var(--paper);
color: var(--ink);
width: 4rem;
text-align: center;
}
.infographic-cerrors .act-btn {
all: unset; cursor: pointer;
display: block;
margin: 0 auto 0.9rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.8rem; font-weight: 600;
padding: 0.45rem 1rem;
border-radius: 999px;
background: var(--accent);
color: #fff;
text-align: center;
}
.infographic-cerrors .act-btn:hover { opacity: 0.88; }
.infographic-cerrors .result-panel {
padding: 0.65rem 0.9rem;
border-radius: 8px;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.8rem;
line-height: 1.5;
white-space: pre-wrap;
background: var(--paper-alt);
color: var(--ink-soft);
min-height: 2.4rem;
}
.infographic-cerrors .result-panel.is-ok { background: var(--ok-soft); color: var(--ok); }
.infographic-cerrors .result-panel.is-err { background: var(--err-soft); color: var(--err); }
.infographic-cerrors .result-panel.is-warn { background: var(--warn-soft); color: var(--warn); }
.infographic-cerrors .takeaway {
font-size: 0.82rem; color: var(--ink-soft);
margin-top: 1.3rem; border-top: 1px solid var(--line); padding-top: 0.9rem;
}
.infographic-cerrors .takeaway strong { color: var(--ink); }
</style>
<h1>π Six Kinds of C Errors</h1>
<p class="subhead">Toggle each one and see exactly what breaks.</p>
<div class="chip-row" id="err-tabs">
<button class="chip is-active" data-tab="syntax">Syntax</button>
<button class="chip" data-tab="semantic">Semantic</button>
<button class="chip" data-tab="logical">Logical</button>
<button class="chip" data-tab="runtime">Runtime</button>
<button class="chip" data-tab="linker">Linker</button>
<button class="chip" data-tab="warning">Warning</button>
</div>
<!-- 1 Syntax -->
<div class="tab-panel is-active" data-tab="syntax">
<div class="code-panel" id="syn-code"></div>
<div class="sub-chip-row" id="syn-toggle">
<button class="sub-chip" data-val="missing">no semicolon</button>
<button class="sub-chip is-active" data-val="ok">with semicolon</button>
</div>
<button class="act-btn" id="syn-run">βΆ Compile</button>
<div class="result-panel" id="syn-result"></div>
</div>
<!-- 2 Semantic -->
<div class="tab-panel" data-tab="semantic">
<div class="code-panel" id="sem-code"></div>
<div class="sub-chip-row" id="sem-toggle">
<button class="sub-chip" data-val="bad">int x = "hello";</button>
<button class="sub-chip is-active" data-val="ok">int x = 5;</button>
</div>
<button class="act-btn" id="sem-run">βΆ Compile</button>
<div class="result-panel" id="sem-result"></div>
</div>
<!-- 3 Logical -->
<div class="tab-panel" data-tab="logical">
<div class="code-panel" id="log-code"></div>
<div class="sub-chip-row" id="log-toggle">
<button class="sub-chip is-active" data-val="wrong">length + width</button>
<button class="sub-chip" data-val="right">length * width</button>
</div>
<button class="act-btn" id="log-run">βΆ Run</button>
<div class="result-panel" id="log-result"></div>
</div>
<!-- 4 Runtime -->
<div class="tab-panel" data-tab="runtime">
<div class="code-panel">int result = a / b;</div>
<div class="field-row">
<label>a</label><input type="number" id="run-a" value="10">
<label>b</label><input type="number" id="run-b" value="0">
</div>
<button class="act-btn" id="run-run">βΆ Run</button>
<div class="result-panel" id="run-result"></div>
</div>
<!-- 5 Linker -->
<div class="tab-panel" data-tab="linker">
<div class="code-panel" id="link-code"></div>
<div class="sub-chip-row" id="link-toggle">
<button class="sub-chip" data-val="typo">display() misspelled</button>
<button class="sub-chip is-active" data-val="ok">display() correct</button>
</div>
<button class="act-btn" id="link-run">βΆ Link</button>
<div class="result-panel" id="link-result"></div>
</div>
<!-- 6 Warning -->
<div class="tab-panel" data-tab="warning">
<div class="code-panel" id="warn-code"></div>
<div class="sub-chip-row" id="warn-toggle">
<button class="sub-chip is-active" data-val="bad">printf("%d", pi)</button>
<button class="sub-chip" data-val="ok">printf("%f", pi)</button>
</div>
<button class="act-btn" id="warn-run">βΆ Compile</button>
<div class="result-panel" id="warn-result"></div>
</div>
<p class="takeaway"><strong>Only one of these six lets your program run and look fine:</strong> a logical error β the compiler and linker have nothing to complain about, it just gives you the wrong answer.</p>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-cerrors')
|| document.querySelector('.infographic-cerrors');
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');
function $(sel) { return wrapper.querySelector(sel); }
// ---- main tabs ----
const tabs = wrapper.querySelectorAll('#err-tabs .chip');
const panels = wrapper.querySelectorAll('.tab-panel');
tabs.forEach(tab => {
tab.addEventListener('click', () => {
tabs.forEach(t => t.classList.remove('is-active'));
tab.classList.add('is-active');
panels.forEach(p => p.classList.toggle('is-active', p.dataset.tab === tab.dataset.tab));
});
});
function setupSubChips(rowId, onChange) {
const chips = wrapper.querySelectorAll(rowId + ' .sub-chip');
chips.forEach(chip => {
chip.addEventListener('click', () => {
chips.forEach(c => c.classList.remove('is-active'));
chip.classList.add('is-active');
onChange(chip.dataset.val);
});
});
}
// ---- 1 Syntax ----
let synVal = 'ok';
const synCode = $('#syn-code');
function renderSynCode() {
synCode.innerHTML = '<span class="code-line"><span class="syn-keyword">int</span> x = 5' + (synVal === 'missing' ? '' : ';') + '</span>';
}
setupSubChips('#syn-toggle', v => { synVal = v; renderSynCode(); });
renderSynCode();
$('#syn-run').addEventListener('click', () => {
const r = $('#syn-result');
if (synVal === 'missing') {
r.className = 'result-panel is-err';
r.textContent = "error: expected ';' before '}' token";
} else {
r.className = 'result-panel is-ok';
r.textContent = 'β
Compilation successful.';
}
});
// ---- 2 Semantic ----
let semVal = 'ok';
const semCode = $('#sem-code');
function renderSemCode() {
semCode.innerHTML = semVal === 'bad'
? '<span class="code-line"><span class="syn-keyword">int</span> x = <span class="syn-string">"hello"</span>;</span>'
: '<span class="code-line"><span class="syn-keyword">int</span> x = 5;</span>';
}
setupSubChips('#sem-toggle', v => { semVal = v; renderSemCode(); });
renderSemCode();
$('#sem-run').addEventListener('click', () => {
const r = $('#sem-result');
if (semVal === 'bad') {
r.className = 'result-panel is-err';
r.textContent = "error: incompatible types when initializing type 'int' using type 'char *'";
} else {
r.className = 'result-panel is-ok';
r.textContent = 'β
Compilation successful β types line up.';
}
});
// ---- 3 Logical ----
let logVal = 'wrong';
const logCode = $('#log-code');
function renderLogCode() {
logCode.innerHTML = '<span class="code-line"><span class="syn-keyword">int</span> area = length ' + (logVal === 'wrong' ? '+' : '*') + ' width;</span>';
}
setupSubChips('#log-toggle', v => { logVal = v; renderLogCode(); });
renderLogCode();
$('#log-run').addEventListener('click', () => {
const length = 5, width = 4;
const r = $('#log-result');
if (logVal === 'wrong') {
r.className = 'result-panel is-warn';
r.textContent = 'Compiles and runs fine.\narea = ' + (length + width) + ' \u26a0\ufe0f wrong \u2014 that\u2019s not the area of a 5\u00d74 rectangle.';
} else {
r.className = 'result-panel is-ok';
r.textContent = 'Compiles and runs fine.\narea = ' + (length * width) + ' \u2705 correct.';
}
});
// ---- 4 Runtime ----
$('#run-run').addEventListener('click', () => {
const a = parseInt($('#run-a').value, 10) || 0;
const b = parseInt($('#run-b').value, 10) || 0;
const r = $('#run-result');
if (b === 0) {
r.className = 'result-panel is-err';
r.textContent = 'π₯ Floating point exception (core dumped)\nThe program compiled fine β it crashed while running.';
} else {
r.className = 'result-panel is-ok';
r.textContent = 'β
result = ' + Math.trunc(a / b);
}
});
// ---- 5 Linker ----
let linkVal = 'ok';
const linkCode = $('#link-code');
function renderLinkCode() {
linkCode.innerHTML =
'<span class="code-line"><span class="syn-keyword">void</span> <span class="syn-func">display</span>() { ... }</span>' +
'<span class="code-line">' + (linkVal === 'typo' ? 'dispaly' : '<span class="syn-func">display</span>') + '(); <span style="color:#6a9955;">// call</span></span>';
}
setupSubChips('#link-toggle', v => { linkVal = v; renderLinkCode(); });
renderLinkCode();
$('#link-run').addEventListener('click', () => {
const r = $('#link-result');
if (linkVal === 'typo') {
r.className = 'result-panel is-err';
r.textContent = "undefined reference to `dispaly'\ncollect2: error: ld returned 1 exit status";
} else {
r.className = 'result-panel is-ok';
r.textContent = 'β
Linked successfully β every call found its function.';
}
});
// ---- 6 Warning ----
let warnVal = 'bad';
const warnCode = $('#warn-code');
function renderWarnCode() {
warnCode.innerHTML =
'<span class="code-line"><span class="syn-keyword">double</span> pi = 3.14;</span>' +
'<span class="code-line"><span class="syn-func">printf</span>(<span class="syn-string">"' + (warnVal === 'bad' ? '%d' : '%f') + '"</span>, pi);</span>';
}
setupSubChips('#warn-toggle', v => { warnVal = v; renderWarnCode(); });
renderWarnCode();
$('#warn-run').addEventListener('click', () => {
const r = $('#warn-result');
if (warnVal === 'bad') {
r.className = 'result-panel is-warn';
r.textContent = "\u26a0\ufe0f warning: format '%d' expects 'int', but argument has type 'double'\n(still compiles and runs \u2014 output will be garbage)";
} else {
r.className = 'result-panel is-ok';
r.textContent = 'β
Compiles cleanly, no warnings. Prints 3.140000';
}
});
})();
</script>π Summary Table
Error Type | When It Occurs | Example | Impact |
|---|---|---|---|
Syntax Error | Compile-time | Missing semicolon | Stops compilation |
Semantic Error | Compile-time | Wrong type assignment | Compilation fails |
Logical Error | Runtime/Logic | Wrong formula, condition | Wrong output |
Runtime Error | Execution | Divide by zero, invalid pointer | Program crashes |
Linking Error | Linking phase | Function not defined | Program won't build |
Warnings | Compile-time | Type mismatch, uninitialized vars | May lead to errors later |
π§ Tips to Handle Errors as a Beginner
πΉ Don't panic when you see red text β errors are helpful.
πΉ Read the full error message, especially the line number.
πΉ Fix one error at a time.
πΉ Use tools like gcc -Wall to enable more warnings.
πΉ Use print statements or gdb to track down bugs.