C provides a set of built-in (predefined) macros that are automatically available in your program. These are mostly used for debugging, logging, or version control, and can help you know more about the code's environment at compile time.
These macros are defined by the compiler and cannot be changed or undefined.
📋 What are Predefined Macros?
These are special macros that provide useful information such as:
Macro | Description |
|---|---|
| Current date of compilation (e.g. |
| Current time of compilation (e.g. |
| Name of the current source file |
| Current line number in the source file |
| Defined as |
🔍 List of All Predefined Macros
Macro | Example Output | Purpose |
|---|---|---|
|
| Date when file was compiled |
|
| Time of compilation |
|
| Name of the current file being compiled |
|
| Line number in current file |
|
| Indicates compiler follows ANSI C standard |
|
| Shows the C version (if defined) |
|
| Indicates whether the implementation is hosted or freestanding (usually 1) |
🧪 Practical Example Using Predefined Macros
<div class="infographic-cmacros">
<style>
.infographic-cmacros {
--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;
--syn-comment: #6a9955;
}
.infographic-cmacros[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-cmacros {
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-cmacros *, .infographic-cmacros *::before, .infographic-cmacros *::after {
box-sizing: border-box;
}
.infographic-cmacros h1 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 600;
font-size: clamp(1.3rem, 3.5vw, 1.7rem);
margin: 1.25rem 0 0.2rem;
text-align: center;
}
.infographic-cmacros .subhead { color: var(--ink-soft); font-size: 0.85rem; margin-bottom: 1.1rem; text-align: center; }
.infographic-cmacros .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.76rem;
line-height: 1.55;
margin-bottom: 1rem;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
.infographic-cmacros .code-panel .code-line { display: block; white-space: pre; }
.infographic-cmacros .code-panel .code-line.is-highlight {
background: rgba(91, 141, 239, 0.22);
box-shadow: inset 3px 0 0 var(--accent);
}
.infographic-cmacros .syn-keyword { color: var(--syn-keyword); }
.infographic-cmacros .syn-func { color: var(--syn-func); }
.infographic-cmacros .syn-string { color: var(--syn-string); }
.infographic-cmacros .syn-comment { color: var(--syn-comment); }
.infographic-cmacros .field-row { display: flex; align-items: center; justify-content: center; gap: 0.6rem; margin-bottom: 0.8rem; flex-wrap: wrap; }
.infographic-cmacros .field-row label { font-family: 'IBM Plex Mono', monospace; font-size: 0.8rem; color: var(--ink-soft); }
.infographic-cmacros .field-row input[type="text"] {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.85rem;
padding: 0.32rem 0.5rem;
border-radius: 6px;
border: 1px solid var(--line);
background: var(--paper);
color: var(--ink);
width: 7rem;
}
.infographic-cmacros .chip-row { display: flex; justify-content: center; gap: 0.4rem; margin-bottom: 1rem; }
.infographic-cmacros .chip {
all: unset;
cursor: pointer;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.8rem;
font-weight: 600;
padding: 0.4rem 0.85rem;
border-radius: 20px;
border: 1px solid var(--line);
color: var(--ink-soft);
transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.infographic-cmacros .chip:hover { border-color: var(--accent); }
.infographic-cmacros .chip.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.infographic-cmacros .run-btn {
all: unset; cursor: pointer;
display: block;
margin: 0 auto 1rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.8rem; font-weight: 600;
padding: 0.45rem 1.1rem;
border-radius: 999px;
background: var(--accent);
color: #fff;
}
.infographic-cmacros .run-btn:hover { opacity: 0.88; }
.infographic-cmacros .run-btn:disabled { opacity: 0.5; cursor: default; }
.infographic-cmacros .console {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 0.7rem 0.9rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.8rem;
line-height: 1.6;
white-space: pre;
min-height: 6.5rem;
}
.infographic-cmacros .takeaway {
font-size: 0.82rem; color: var(--ink-soft);
margin-top: 1.3rem; border-top: 1px solid var(--line); padding-top: 0.9rem;
}
.infographic-cmacros .takeaway strong { color: var(--ink); }
@media (prefers-reduced-motion: reduce) {
.infographic-cmacros * { transition: none !important; }
}
</style>
<h1>🏷️ Predefined Macros</h1>
<p class="subhead">__FILE__, __LINE__, __DATE__, __TIME__ — filled in by the compiler, not you.</p>
<div class="code-panel" id="mac-code">
<span class="code-line">#include <stdio.h></span>
<span class="code-line"><span class="syn-keyword">int</span> <span class="syn-func">main</span>() {</span>
<span class="code-line" data-line="file"> <span class="syn-func">printf</span>(<span class="syn-string">"File Name: %s\n"</span>, __FILE__);</span>
<span class="code-line" data-line="line"> <span class="syn-func">printf</span>(<span class="syn-string">"Line Number: %d\n"</span>, __LINE__);</span>
<span class="code-line" data-line="date"> <span class="syn-func">printf</span>(<span class="syn-string">"Compilation Date: %s\n"</span>, __DATE__);</span>
<span class="code-line" data-line="time"> <span class="syn-func">printf</span>(<span class="syn-string">"Compilation Time: %s\n"</span>, __TIME__);</span>
<span class="code-line" data-line="ifdef"> <span class="syn-keyword">#ifdef</span> __STDC__</span>
<span class="code-line" data-line="yes"> <span class="syn-func">printf</span>(<span class="syn-string">"ANSI Standard C Compiler Used: Yes\n"</span>);</span>
<span class="code-line" data-line="else"><span class="syn-keyword"> #else</span></span>
<span class="code-line" data-line="no"> <span class="syn-func">printf</span>(<span class="syn-string">"ANSI Standard C Compiler Used: No\n"</span>);</span>
<span class="code-line" data-line="endif"><span class="syn-keyword"> #endif</span></span>
<span class="code-line" data-line="ret"> <span class="syn-keyword">return</span> 0;</span>
<span class="code-line">}</span>
</div>
<div class="field-row">
<label>__FILE__ =</label><input type="text" id="mac-filename" value="demo.c">
</div>
<div class="chip-row" id="stdc-tabs">
<button class="chip is-active" data-val="yes">__STDC__ defined</button>
<button class="chip" data-val="no">__STDC__ not defined</button>
</div>
<button class="run-btn" id="mac-run">▶ Run</button>
<div class="console" id="mac-console"></div>
<p class="takeaway"><strong>#ifdef / #else / #endif</strong> is decided before compilation even starts — only one of those two printf lines actually makes it into your program.</p>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-cmacros')
|| document.querySelector('.infographic-cmacros');
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); }
const codePanel = $('#mac-code');
const codeLines = codePanel.querySelectorAll('.code-line[data-line]');
const filenameInput = $('#mac-filename');
const stdcChips = wrapper.querySelectorAll('#stdc-tabs .chip');
const runBtn = $('#mac-run');
const consoleEl = $('#mac-console');
let stdcDefined = true;
stdcChips.forEach(chip => {
chip.addEventListener('click', () => {
stdcChips.forEach(c => c.classList.remove('is-active'));
chip.classList.add('is-active');
stdcDefined = chip.dataset.val === 'yes';
});
});
function highlight(id) {
codeLines.forEach(l => l.classList.toggle('is-highlight', l.dataset.line === id));
}
function realDate() {
const MONTHS = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
const d = new Date();
const mon = MONTHS[d.getMonth()];
const day = String(d.getDate()).padStart(2, ' ');
return mon + ' ' + day + ' ' + d.getFullYear();
}
function realTime() {
const d = new Date();
const pad = n => String(n).padStart(2, '0');
return pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds());
}
runBtn.addEventListener('click', () => {
runBtn.disabled = true;
consoleEl.textContent = '';
highlight(null);
const filename = filenameInput.value || 'demo.c';
const steps = [
() => { highlight('file'); consoleEl.textContent = 'File Name: ' + filename; },
() => { highlight('line'); consoleEl.textContent += '\nLine Number: 4'; },
() => { highlight('date'); consoleEl.textContent += '\nCompilation Date: ' + realDate(); },
() => { highlight('time'); consoleEl.textContent += '\nCompilation Time: ' + realTime(); },
() => { highlight('ifdef'); },
() => {
if (stdcDefined) {
highlight('yes');
consoleEl.textContent += '\nANSI Standard C Compiler Used: Yes';
} else {
highlight('no');
consoleEl.textContent += '\nANSI Standard C Compiler Used: No';
}
},
() => { highlight('ret'); },
() => { highlight(null); runBtn.disabled = false; }
];
let i = 0;
(function next() { if (i >= steps.length) return; steps[i](); i++; setTimeout(next, 420); })();
});
})();
</script>🔎 LINE will print the actual line number in the source file where it is used. If you move the statement, the number will change accordingly.
🧠 Why and Where to Use Predefined Macros?
Use Case | Description |
|---|---|
✅ Debugging | Show which file and line caused an issue |
🧪 Logging | Print compilation time to confirm code freshness |
🚧 Error Handling | Combine with |
📂 File tracking | Automatically know the current source file |
📐 Version Control | Detect compiler version and behavior |
📌 Using __LINE__ & __FILE__ for Custom Error Messages
#define error(msg) printf("Error: %s in %s at line %d\\n", msg, __FILE__, __LINE__)
int main() {
error("Something went wrong!");
return 0;
}Error: Something went wrong! in main.c at line 6🧰 This is a manual version of assert(), great for early-stage debugging without using libraries.