सी में जेनेरिक कीवर्ड का उपयोग विभिन्न डेटा प्रकारों के लिए मैक्रो को परिभाषित करने के लिए किया जाता है। यह नया कीवर्ड 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है