C में _Generic कीवर्ड का उपयोग विभिन्न डेटा प्रकारों के लिए मैक्रो परिभाषित करने के लिए किया जाता है। यह नया कीवर्ड C11 मानक रिलीज में C प्रोग्रामिंग भाषा में जोड़ा गया था। _Generic कीवर्ड प्रोग्रामर को मैक्रो का अधिक कुशल तरीके से उपयोग करने में मदद करता है।
यह कीवर्ड वेरिएबल के प्रकार के आधार पर मैक्रो का अनुवाद करता है। उदाहरण:
#define dec(x) _Generic((x), long double: decl, default: Inc, float: incf)(x)
उदाहरण कोड
#include <stdio.h>
#define typecheck(T) _Generic((T), char: 1, int: 2, long: 3, float: 4, default: 0)
int main(void) {
printf("passing a long value to the macro, result is %d\n", typecheck(2353463456356465));
printf("passing a float value to the macro, result is %d\n", typecheck(4.32f));
printf("passing a int value to the macro, result is %d\n", typecheck(324));
printf("passing a string value to the macro, result is %d\n", typecheck("Hello"));
return 0;
}
आउटपुट
passing a long value to the macro, result is 3 passing a float value to the macro, result is 4 passing a int value to the macro, result is 2 passing a string value to the macro, result is 0