C के #define और Macros
In this page:
#define NAME value
#define MACRO(param) ((param) expression)
Object-like Macros
#define PI 3.14159 classic example है -- आपके code में PI का हर occurrence compilation से पहले textually 3.14159 से replace हो जाता है, अगर कभी value बदलनी हो तो इसे update करने के लिए आपको एक जगह देते हुए।
उदाहरण: Object-like Macros
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro PI (replaced by the preprocessor)
#define PI 3.14159
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%.5f", PI);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function-like Macros
#define SQUARE(x) ((x) * (x)) इस्तेमाल के point पर एक function call जैसा दिखता है, लेकिन preprocessor literally argument के text को macro body में substitute करता है जहाँ भी x आता है, runtime पर कोई actual function call overhead नहीं होता।
उदाहरण: Function-like Macros
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro SQUARE (replaced by the preprocessor)
#define SQUARE(x) ((x) * (x))
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", SQUARE(5));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
कई Arguments वाले Macros
#define MAX(a, b) ((a) > (b) ? (a) : (b)) एक two-argument macro दिखाता है -- macro body में हर parameter नाम macro इस्तेमाल होने पर matching argument के literal text से replace हो जाता है।
उदाहरण: Macros with Multiple Arguments
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro MAX (replaced by the preprocessor)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", MAX(3, 7));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Parentheses क्यों Critical हैं
इनके बिना, x * x की तरह defined SQUARE(x), SQUARE(1+2) को 1+2 * 1+2 में expand करेगा, जो operator precedence की वजह से गलत evaluate होता है -- हर parameter और पूरे expression को parentheses में wrap करना bugs की इस पूरी class को avoid करता है।
उदाहरण: Why Parentheses are Critical
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro SQUARE (replaced by the preprocessor)
#define SQUARE(x) ((x) * (x))
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", SQUARE(1 + 2));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
#undef से Macros Undefine करना
यह तब useful है जब एक macro सिर्फ आपकी file के एक हिस्से के लिए active होना चाहिए, या जब आप इसे code के एक अलग section के लिए एक अलग value में redefine करना चाहें बिना एक 'macro redefined' warning trigger किए।
उदाहरण: Undefining Macros with #undef
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro VALUE (replaced by the preprocessor)
#define VALUE 10
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d ", VALUE);
#undef VALUE
// Define the macro VALUE (replaced by the preprocessor)
#define VALUE 20
// Print formatted text to the screen
printf("%d", VALUE);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- macro arguments के आस-पास parentheses भूल जाना, ताकि
SQUARE(a + 1)गलत expression में expand हो। - एक
#defineको semicolon से खत्म करना, जो replacement text का हिस्सा बन जाता है। MAX(i++, j)जैसी side effects वाली एक expression एक macro को pass करना, जो इसे दो बार evaluate कर सकता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: