C के Predefined Macros
In this page:
__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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
__LINE__ Macro
__FILE__ के साथ combined, यह pair custom assertion और error-reporting macros की foundation है जो आपको exactly बताते हैं कि source code में कहाँ कुछ गलत हुआ, specific line तक।
उदाहरण: The LINE Macro
// 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;
}
Login to try C/C++/Java/PHP code in the editor
__DATE__ Macro
यह build metadata को सीधे compiled binaries में embed करने के लिए useful है, जैसे बाहरी build scripts की ज़रूरत के बिना इसकी version या about screen में program की build date print करना।
उदाहरण: The DATE Macro
// 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;
}
Login to try C/C++/Java/PHP code in the editor
__TIME__ Macro
__DATE__ के साथ paired, यह आपको एक complete build timestamp देता है, जो आमतौर पर debug builds में program शुरू होने पर print होने वाली एक single version string में combine होता है।
उदाहरण: The TIME Macro
// 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;
}
Login to try C/C++/Java/PHP code in the editor
__STDC__ Macro
#if __STDC__ == 1 check करना code को इस आधार पर अपना behavior या feature usage adapt करने देता है कि यह एक strictly standards-conformant compiler से compile हो रहा है या vendor-specific extensions enabled वाले किसी से।
उदाहरण: The STDC Macro
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
__DATE__और__TIME__दिखाते हैं कि program कब चलता है, जब वे compile time दिखाते हैं। __LINE__को%sसे print करना, जब यह एक integer है और इसे%dचाहिए।- नाम के दोनों ओर से double underscores छोड़ देना, जैसे
FILE।
- Preprocessor compilation से पहले चलता है और
#से शुरू होने वाली lines handle करता है। #defineconstants और macros बनाता है, और#includeheader 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: