Learn C
#include "preprocessor/predefined_macros.h"

Predefined Macros (__FILE__, __LINE__) in C

#concept

The compiler predefines macros carrying compile context: __FILE__ (current filename), __LINE__ (current line), __func__ (enclosing function name, C99), __DATE__/__TIME__ (build timestamp), plus __STDC_VERSION__ for the standard in force. Logging and assertion macros stamp these into every message, pinpointing failures without manual labels.

#error aborts compilation with a message (for impossible configurations), #pragma packs pragmas like packWarning controls, and #line renumbers (mostly for generated code). Together they make diagnostics self-locating.

#how it works
1

Location macros

fprintf(stderr, "%s:%d: fail\n", __FILE__, __LINE__); β€” every message carries its own address.

2

__func__ names context

Inside any function, __func__ expands to its name string β€” log helpers include it automatically.

3

#error for impossible states

#if !defined(HAVE_X)\n#error "need X"\n#endif β€” unsupported configurations fail loudly at compile time.

4

Version detection

#if __STDC_VERSION__ >= 201112L gates C11 features β€” portable code adapts to its compiler.

#example
#include <stdio.h>
Β 
#define LOG(msg) fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, msg)
Β 
int main(void) {
LOG("starting");
return 0;
}
Output
app.c:7 starting
1LOG pastes a fprintf stamped with the call site's file and line β€” no manual labels.
2At line 7, __FILE__ is app.c and __LINE__ is 7, producing the self-locating message.
3Every LOG call site reports its own position β€” diagnostics that navigate themselves.
#use cases
Self-locating error and log macros via __FILE__/__LINE__/__func__
Compile-time configuration errors with #error
Version-gated features via __STDC_VERSION__ checks
Build-stamped binaries with __DATE__/__TIME__

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself β€” building beats watching.