← Back to C Course | Chapter 11: Preprocessor | Lesson 5 of 5

C के Predefined Macros

Predefined macros ready-made नाम हैं जो C आपके लिए भर देता है, जैसे current file, line, date, और time। ये messages और debugging में मदद करते हैं।
Syntax
c
__FILE__  // current source file name
__LINE__  // current line number
__DATE__  // compile date
__TIME__  // compile time

__FILE__ Macro

यह debugging और logging के लिए invaluable है -- #define LOG(msg) printf("[%s] %s\n", __FILE__, msg) जैसा एक macro automatically हर log message को उस exact source file के साथ tag करता है जहाँ से यह आया, बिना आपको filename manually type करने की ज़रूरत के।

उदाहरण: The FILE Macro

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%s", __FILE__);
	// Return 0 to signal that the program finished successfully
	return 0;
}

__LINE__ Macro

__FILE__ के साथ combined, यह pair custom assertion और error-reporting macros की foundation है जो आपको exactly बताते हैं कि source code में कहाँ कुछ गलत हुआ, specific line तक।

उदाहरण: The LINE Macro

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", __LINE__);
	// Return 0 to signal that the program finished successfully
	return 0;
}

__DATE__ Macro

यह build metadata को सीधे compiled binaries में embed करने के लिए useful है, जैसे बाहरी build scripts की ज़रूरत के बिना इसकी version या about screen में program की build date print करना।

उदाहरण: The DATE Macro

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%s", __DATE__);
	// Return 0 to signal that the program finished successfully
	return 0;
}

__TIME__ Macro

__DATE__ के साथ paired, यह आपको एक complete build timestamp देता है, जो आमतौर पर debug builds में program शुरू होने पर print होने वाली एक single version string में combine होता है।

उदाहरण: The TIME Macro

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%s %s", __DATE__, __TIME__);
	// Return 0 to signal that the program finished successfully
	return 0;
}

__STDC__ Macro

#if __STDC__ == 1 check करना code को इस आधार पर अपना behavior या feature usage adapt करने देता है कि यह एक strictly standards-conformant compiler से compile हो रहा है या vendor-specific extensions enabled वाले किसी से।

उदाहरण: The STDC Macro

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	#if __STDC__ == 1
	// Print formatted text to the screen
	printf("Standards-conformant compiler");
	#endif
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि __DATE__ और __TIME__ दिखाते हैं कि program कब चलता है, जब वे compile time दिखाते हैं।
  2. __LINE__ को %s से print करना, जब यह एक integer है और इसे %d चाहिए।
  3. नाम के दोनों ओर से double underscores छोड़ देना, जैसे FILE।
चैप्टर सारांश
  • Preprocessor compilation से पहले चलता है और # से शुरू होने वाली lines handle करता है।
  • #define constants और macros बनाता है, और #include header files ले आता है।
  • Conditional compilation और predefined macros आपको same source से program के अलग versions बनाने देते हैं।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.