🧾 What is a Constant?
In C, a constant is a value that cannot be changed once it is defined.
It’s like labeling something as “Do Not Touch” in your code.
Think of it like your date of birth — you cannot change it, even though you can use it anywhere.
🧪 Declaring a Constant
✅ Method 1: Using the const keyword
const int maxMarks = 100;Here,
maxMarksis a constant integer.If you try to change it later like
maxMarks = 90;, you'll get a compile-time error.
✅ Method 2: Using #define preprocessor macro
#define PI 3.14159PIis now a macro constant — this is replaced before compilation.You can’t change it later in the code.
🔎 Difference between const and #define:
Feature |
|
|
|---|---|---|
Type-safe? | ✅ Yes | ❌ No (just text replacement) |
Memory allocated? | ✅ Yes (variable) | ❌ No |
Scope? | Local or Global | Global |
Debugging support | ✅ Better | ❌ Poor |
🔄 Example:
#include <stdio.h>
#define PI 3.14
int main() {
const int MAX = 100;
printf("Value of PI = %f\\n", PI);
printf("Max limit = %d\\n", MAX);
// PI = 3.14159; ❌ Error (with const) — can't modify
// MAX = 200; ❌ Error — can't modify const variable
return 0;
}🔥 Why Use Constants?
🚫 Avoid Accidental Changes
🔄 Make Code Easy to Update (Change value in one place)
💡 Improve Readability & Meaning (e.g.,
MAX_SPEEDis better than using120everywhere)
🔍 What is a Literal?
A literal is a value that appears directly in your code.
It’s the actual number or character or string used in your code.
📘 Examples of literals:
25 // integer literal
3.14 // float literal
'A' // character literal
"Hello" // string literal🧬 Types of Literals in C
Let’s break them down:
🔢 Integer Literals
Values without decimal points:
int x = 100; // decimal (base 10)
int y = 0xFF; // hexadecimal (base 16)
int z = 075; // octal (base 8🌊 Floating-Point Literals
Values with decimal points:
float pi = 3.14;
double radius = 2.718281828;You can also use scientific notation:
float distance = 1.2e5; // = 1.2 × 10^5 = 120000.0🔤 Character Literals
A single character enclosed in single quotes:
char grade = 'A';
char symbol = '$';Every character literal is stored as an integer ASCII value internally.
char c = 'A'; // ASCII of A = 65You can even print:
printf("%d", 'A'); // prints 6🧵 **D. String Literals**A sequence of characters enclosed in double quotes:
char message[] = "Hello, World!";It ends with a special null character
\\0Stored as an array of characters
⚙️ Escape Sequence Literals
Special literals used inside strings/characters:
Literal | Meaning |
|---|---|
| New Line |
| Tab Space |
| Backslash |
| Double Quote |
| Single Quote |
Example:
printf("Hello\\nWorld\\tC"); // Prints on two lines with a tabHello
World C✅ Best Practices
Use
constinstead of#definefor typed constantsUse all UPPERCASE names for constants:
MAX_SIZE,PI,LIMITDo not use magic numbers (e.g., don’t write
if (x > 500), useMAX_LIMITinstead)
🧪 Difference Between Constants and Literals
Feature | Constant | Literal |
|---|---|---|
Can be named? | ✅ Yes ( | ❌ No (just value: |
Modifiable? | ❌ No | ❌ No |
Use in code? | Defined once, used everywhere | Appears directly in code |
Example |
|
|
<div class="infographic-cconst">
<style>
.infographic-cconst {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--code-bg: #1e1e2e;
--code-text: #d4d4e0;
--syn-preproc: #569cd6;
--syn-keyword: #c586c0;
--syn-func: #dcdcaa;
--syn-string: #ce9178;
--syn-comment: #6a9955;
}
.infographic-cconst[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-cconst {
box-sizing: border-box;
max-width: 760px;
margin: 0 auto;
background: var(--paper);
color: var(--ink);
font-family: 'Inter', -apple-system, sans-serif;
padding: 0 clamp(1.25rem, 5vw, 3rem);
}
.infographic-cconst *, .infographic-cconst *::before, .infographic-cconst *::after {
box-sizing: border-box;
}
.infographic-cconst h1 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 600;
font-size: clamp(1.6rem, 4vw, 2.2rem);
margin: 2rem 0 0.25rem;
}
.infographic-cconst .subhead {
color: var(--ink-soft);
font-size: 0.95rem;
margin-bottom: 2rem;
}
.infographic-cconst h2 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 600;
font-size: 1.2rem;
margin: 0 0 0.9rem;
display: flex;
align-items: center;
gap: 0.5rem;
}
.infographic-cconst section {
margin-bottom: 2.5rem;
}
.infographic-cconst section + section {
border-top: 1px solid var(--line);
padding-top: 2rem;
}
/* shared */
.infographic-cconst .code-panel {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 1rem 1.15rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.88rem;
line-height: 1.85;
}
.infographic-cconst .code-panel .code-line { white-space: pre; }
.infographic-cconst .detail-panel {
margin-top: 0.9rem;
padding: 0.85rem 1.05rem;
background: var(--paper-alt);
border-radius: 8px;
font-size: 0.88rem;
color: var(--ink-soft);
line-height: 1.5;
min-height: 2.4rem;
}
.infographic-cconst .detail-panel strong { color: var(--ink); }
.infographic-cconst .chip-row {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
margin-bottom: 1rem;
}
.infographic-cconst .chip {
all: unset;
cursor: pointer;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.85rem;
font-weight: 600;
padding: 0.45rem 0.9rem;
border-radius: 20px;
border: 1px solid var(--line);
color: var(--ink-soft);
transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.infographic-cconst .chip:hover { border-color: var(--accent); }
.infographic-cconst .chip.is-active {
background: var(--accent);
border-color: var(--accent);
color: #fff;
}
/* Section 2: literal tabs */
.infographic-cconst .example { display: none; }
.infographic-cconst .example.is-active {
display: block;
animation: cconst-fade-in 0.3s ease;
}
@keyframes cconst-fade-in {
from { opacity: 0; transform: translateY(4px); }
to { opacity: 1; transform: translateY(0); }
}
.infographic-cconst .tok {
cursor: pointer;
border-radius: 4px;
padding: 0.05rem 0.2rem;
margin: 0 -0.15rem;
transition: background 0.15s, color 0.15s;
}
.infographic-cconst .tok:hover { background: rgba(255,255,255,0.08); }
.infographic-cconst .tok.is-active {
background: var(--accent);
color: #fff;
font-weight: 600;
}
.infographic-cconst .ascii-try {
margin-top: 0.9rem;
display: flex;
align-items: center;
gap: 0.75rem;
font-size: 0.88rem;
color: var(--ink-soft);
}
.infographic-cconst .ascii-try input {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.95rem;
padding: 0.3rem 0.5rem;
border-radius: 6px;
border: 1px solid var(--line);
background: var(--paper);
color: var(--ink);
width: 3.2rem;
text-align: center;
}
.infographic-cconst .ascii-try input:focus {
outline: 2px solid var(--accent);
outline-offset: 1px;
}
.infographic-cconst .ascii-try .ascii-result strong {
color: var(--accent);
font-family: 'IBM Plex Mono', monospace;
}
.infographic-cconst .output-label {
margin-top: 0.75rem;
font-size: 0.72rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.04em;
color: var(--ink-soft);
margin-bottom: 0.35rem;
}
.infographic-cconst .output-panel {
background: var(--paper-alt);
border-left: 3px solid var(--accent);
border-radius: 6px;
padding: 0.75rem 1rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.9rem;
color: var(--ink);
white-space: pre;
tab-size: 4;
-moz-tab-size: 4;
}
.infographic-cconst .takeaway {
font-size: 0.95rem;
color: var(--ink-soft);
}
.infographic-cconst .takeaway strong { color: var(--ink); }
@media (prefers-reduced-motion: reduce) {
.infographic-cconst * { transition: none !important; animation: none !important; }
}
</style>
<h1>🧾 Constants & Literals</h1>
<p class="subhead">Click, toggle, and type your way through how C locks values down.</p>
<section>
<h2>🔎 const vs #define</h2>
<div class="chip-row" id="cconst-toggle">
<button class="chip is-active" data-mode="const">const</button>
<button class="chip" data-mode="define">#define</button>
</div>
<div class="code-panel"><span class="code-line" id="cconst-code"><span class="syn-keyword">const</span> <span class="syn-func">int</span> maxMarks = 100;</span></div>
</section>
<section>
<h2>🔍 Literal Type Identifier</h2>
<div class="chip-row" id="lit-tabs">
<button class="chip is-active" data-ex="0">Integer</button>
<button class="chip" data-ex="1">Float</button>
<button class="chip" data-ex="2">Character</button>
<button class="chip" data-ex="3">String</button>
</div>
<div class="example is-active" data-ex="0">
<div class="code-panel">
<span class="code-line">int x = <span class="tok" data-explain="Decimal integer literal (base 10).">100</span>;</span>
<span class="code-line">int y = <span class="tok" data-explain="Hexadecimal integer literal (base 16) — equals 255.">0xFF</span>;</span>
<span class="code-line">int z = <span class="tok" data-explain="Octal integer literal (base 8) — equals 61.">075</span>;</span>
</div>
</div>
<div class="example" data-ex="1">
<div class="code-panel">
<span class="code-line">float pi = <span class="tok" data-explain="Floating-point literal — a value with a decimal point.">3.14</span>;</span>
<span class="code-line">float distance = <span class="tok" data-explain="Scientific notation literal — 1.2 × 10⁵ = 120000.0.">1.2e5</span>;</span>
</div>
</div>
<div class="example" data-ex="2">
<div class="code-panel">
<span class="code-line">char grade = '<span class="tok" data-kind="char" data-word="A">A</span>';</span>
<span class="code-line">char symbol = '<span class="tok" data-kind="char" data-word="$">$</span>';</span>
</div>
<div class="ascii-try">
<label for="cconst-ascii-input">Try your own:</label>
<input type="text" id="cconst-ascii-input" maxlength="1" value="A">
<span class="ascii-result">ASCII = <strong id="cconst-ascii-out">65</strong></span>
</div>
</div>
<div class="example" data-ex="3">
<div class="code-panel">
<span class="code-line">char message[] = "<span class="tok" data-explain="String literal — stored as an array of characters, ends with a null terminator \0.">Hello, World!</span>";</span>
</div>
</div>
<div class="detail-panel" id="lit-detail">Click any highlighted value to see what kind of literal it is.</div>
</section>
<section>
<h2>⚙️ Escape Sequence Literals</h2>
<div class="chip-row" id="esc-tabs">
<button class="chip is-active" data-ex="0">\n New Line</button>
<button class="chip" data-ex="1">\t Tab</button>
<button class="chip" data-ex="2">\\ Backslash</button>
<button class="chip" data-ex="3">\" Double Quote</button>
<button class="chip" data-ex="4">\' Single Quote</button>
</div>
<div class="example is-active" data-ex="0">
<div class="code-panel"><span class="code-line">printf(<span class="syn-string">"Hello\nWorld"</span>);</span></div>
<div class="output-label">Output</div>
<div class="output-panel" id="esc-out-0"></div>
</div>
<div class="example" data-ex="1">
<div class="code-panel"><span class="code-line">printf(<span class="syn-string">"Name:\tJohn"</span>);</span></div>
<div class="output-label">Output</div>
<div class="output-panel" id="esc-out-1"></div>
</div>
<div class="example" data-ex="2">
<div class="code-panel"><span class="code-line">printf(<span class="syn-string">"Path: C:\\Users"</span>);</span></div>
<div class="output-label">Output</div>
<div class="output-panel" id="esc-out-2"></div>
</div>
<div class="example" data-ex="3">
<div class="code-panel"><span class="code-line">printf(<span class="syn-string">"She said \"Hi\""</span>);</span></div>
<div class="output-label">Output</div>
<div class="output-panel" id="esc-out-3"></div>
</div>
<div class="example" data-ex="4">
<div class="code-panel">
<span class="code-line">char c = <span class="syn-string">'\''</span>;</span>
<span class="code-line">printf(<span class="syn-string">"%c"</span>, c);</span>
</div>
<div class="output-label">Output</div>
<div class="output-panel" id="esc-out-4"></div>
</div>
</section>
<p class="takeaway"><strong>Same idea, three ways:</strong> a literal is the raw value, a constant is that value given a name, and naming it is what makes code readable.</p>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-cconst')
|| document.querySelector('.infographic-cconst');
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');
// Section 1: const vs #define toggle
const modeCode = {
const: '<span class="syn-keyword">const</span> <span class="syn-func">int</span> maxMarks = 100;',
define: '<span class="syn-preproc">#define</span> PI 3.14159'
};
const codeEl = wrapper.querySelector('#cconst-code');
const toggleBtns = wrapper.querySelectorAll('#cconst-toggle .chip');
function renderMode(mode) {
codeEl.innerHTML = modeCode[mode];
}
toggleBtns.forEach(btn => {
btn.addEventListener('click', () => {
toggleBtns.forEach(b => b.classList.remove('is-active'));
btn.classList.add('is-active');
renderMode(btn.dataset.mode);
});
});
renderMode('const');
// Generic tab-group helper (used by Literal Type Identifier and Escape Sequences)
function setupTabs(tabsSelector, exampleSelector, onShow) {
const tabs = Array.from(wrapper.querySelectorAll(tabsSelector + ' .chip'));
const examples = Array.from(wrapper.querySelectorAll(exampleSelector));
function show(index) {
tabs.forEach(t => t.classList.toggle('is-active', t.dataset.ex === String(index)));
examples.forEach(e => e.classList.toggle('is-active', e.dataset.ex === String(index)));
if (onShow) onShow(index);
}
tabs.forEach(tab => tab.addEventListener('click', () => show(tab.dataset.ex)));
}
// Section 2: literal tabs
const litDetail = wrapper.querySelector('#lit-detail');
function resetLitDetail() {
litDetail.innerHTML = "Click any highlighted value to see what kind of literal it is.";
}
setupTabs('#lit-tabs', '#lit-tabs ~ .example', resetLitDetail);
wrapper.querySelectorAll('#lit-tabs ~ .example .tok').forEach(tok => {
tok.addEventListener('click', () => {
const container = tok.closest('.example');
container.querySelectorAll('.tok').forEach(t => t.classList.remove('is-active'));
tok.classList.add('is-active');
if (tok.dataset.kind === 'char') {
const word = tok.dataset.word;
const code = word.charCodeAt(0);
litDetail.innerHTML = "<strong>'" + word + "'</strong> is a character literal — stored internally as its ASCII value: <strong>" + code + '</strong>.';
} else {
litDetail.innerHTML = tok.dataset.explain;
}
});
});
const asciiInput = wrapper.querySelector('#cconst-ascii-input');
const asciiOut = wrapper.querySelector('#cconst-ascii-out');
asciiInput.addEventListener('input', () => {
const ch = asciiInput.value.slice(-1);
asciiInput.value = ch;
asciiOut.textContent = ch ? ch.charCodeAt(0) : '—';
});
// Section 3: escape sequence outputs (computed for real, not hardcoded)
const escapeOutputs = {
'esc-out-0': "Hello\nWorld",
'esc-out-1': "Name:\tJohn",
'esc-out-2': "Path: C:\\Users",
'esc-out-3': 'She said "Hi"',
'esc-out-4': "'"
};
Object.keys(escapeOutputs).forEach(id => {
const el = wrapper.querySelector('#' + id);
if (el) el.textContent = escapeOutputs[id];
});
setupTabs('#esc-tabs', '#esc-tabs ~ .example');
})();
</script>