Translate

Generics Keyword in C क्या है? (Complete Guide with Examples)

क्या आपने कभी ऐसा सोचा है कि एक ही function अलग-अलग data types के लिए काम करे? जैसे एक ही function int, float और double तीनों के लिए इस्तेमाल हो सके।

यही concept Generic Programming का है। कई modern languages जैसे C++, Java और C# में Generics keyword मौजूद होता है। लेकिन सवाल यह है:

क्या C language में Generics keyword होता है?

सीधा जवाब है — नहीं। लेकिन अच्छी खबर यह है कि C language में भी हम कुछ techniques जैसे macros, void pointers और _Generic keyword (C11) की मदद से generic programming कर सकते हैं।

इस detailed guide में हम समझेंगे:

  • Generics Keyword in C क्या होता है
  • C में Generic Programming कैसे implement करते हैं
  • Macros और void pointers का उपयोग
  • C11 का _Generic keyword
  • Real coding examples
  • Best practices for developers

Table of Contents

Generics Keyword का मतलब क्या है?

Generics का मतलब होता है ऐसा code लिखना जो किसी एक specific data type पर depend न करे।

उदाहरण के लिए:

अगर आपको दो numbers का maximum निकालना है तो आप अलग-अलग functions लिख सकते हैं:

  • maxInt()
  • maxFloat()
  • maxDouble()

लेकिन Generic Programming में आप एक ही function लिखते हैं जो हर data type के साथ काम कर सके।

Generics Keyword in C क्या है? C में Generic Programming समझें (Examples सहित)

Generic Programming के फायदे

  • Code reuse बढ़ता है
  • Code duplication कम होता है
  • Maintain करना आसान होता है
  • Performance बेहतर होती है

यही वजह है कि Generics Keyword modern programming languages में बहुत popular है।

क्या C Language में Generics Keyword होता है?

कई beginners यह मान लेते हैं कि C में भी Generics keyword होता है, लेकिन technically ऐसा नहीं है।

Traditional C language में generics support नहीं था। लेकिन developers ने कुछ smart techniques बनाई जिससे generic behaviour achieve किया जा सके।

C में Generic Programming के तरीके

  • Macros
  • Void pointers
  • C11 का _Generic keyword

इन techniques की मदद से आप Generics keyword in C जैसा behaviour बना सकते हैं।

Generic Programming in C

जब हम C language में generic code लिखते हैं तो हमारा goal होता है:

"एक ही function या logic कई data types के साथ काम करे"

इस approach को Generic Programming in C कहा जाता है।

Example scenario:

  • Sorting algorithms
  • Data structures (Stack, Queue, Linked List)
  • Utility functions

इनमें अक्सर हमें different data types handle करने पड़ते हैं।

Macros से Generic Functions बनाना

C में Macros generic programming का सबसे आसान तरीका है।

Example: Generic Max Function

#define MAX(a,b) ((a) > (b) ? (a) : (b))

अब यह macro कई data types के साथ काम करेगा:

int a = 10, b = 20;
float x = 3.5, y = 7.2;

printf("%d", MAX(a,b));
printf("%f", MAX(x,y));

Macros के फायदे

  • Simple syntax
  • Multiple data types support
  • Compile time expansion

Macros की limitations

  • Type safety नहीं होती
  • Debugging मुश्किल हो सकती है

Void Pointer से Generic Programming

C में void pointer का मतलब होता है ऐसा pointer जो किसी भी data type को point कर सकता है।

इसका उपयोग करके हम generic functions बना सकते हैं।

Example

#include <stdio.h>

void printValue(void *ptr, char type)
{
    if(type == 'i')
        printf("%d", *(int*)ptr);

    else if(type == 'f')
        printf("%f", *(float*)ptr);
}

Usage:

int a = 10;
float b = 3.14;

printValue(&a,'i');
printValue(&b,'f');

इस technique से हम Generic Programming in C implement कर सकते हैं।

C11 का _Generic Keyword

C11 standard में एक नया feature आया — _Generic

यह feature compile time पर type checking करने में मदद करता है।

Example

#define type(x) _Generic((x), \
    int: "Integer", \
    float: "Float", \
    double: "Double", \
    default: "Unknown")

Usage:

printf("%s", type(10));
printf("%s", type(3.14));

यह approach Generics keyword in C का सबसे modern implementation माना जाता है।

Real-Life Use Cases of Generic Programming in C

1. Generic Sorting Algorithms

Sorting algorithms जैसे:

  • Quick Sort
  • Merge Sort
  • Bubble Sort

इनमें अक्सर void pointers का उपयोग किया जाता है ताकि algorithm किसी भी data type पर काम कर सके।

2. Data Structures Libraries

Professional C libraries में generic data structures बनाए जाते हैं:

  • Generic Stack
  • Generic Queue
  • Generic Linked List

3. Standard Library Example

C की qsort() function भी internally generic programming concept का उपयोग करती है।

Developers के लिए Practical Tips

1. Small utilities के लिए Macros उपयोग करें

अगर function छोटा है जैसे max, min, swap तो macros सबसे आसान solution है।

2. Libraries में Void Pointers उपयोग करें

Data structures या reusable libraries के लिए void pointer बेहतर होता है।

3. Modern code में _Generic अपनाएँ

अगर आप C11 या newer compiler उपयोग कर रहे हैं तो _Generic keyword सबसे अच्छा विकल्प है।

4. Type Safety का ध्यान रखें

Generic code लिखते समय type errors avoid करने के लिए proper documentation और testing जरूरी है।

C11 _Generic Keyword Deep Dive

C11 standard में introduced feature _Generic वास्तव में एक compile-time selection expression है।

यह runtime polymorphism नहीं बल्कि compile-time type dispatch करता है।

Syntax

_Generic(expression,
        type1: result1,
        type2: result2,
        default: result)

Compiler expression के type को check करता है और matching result choose करता है।

Example

#define printType(x) _Generic((x), \
    int: "Integer", \
    float: "Float", \
    double: "Double", \
    char*: "String", \
    default: "Unknown")

यह compile time पर determine करता है कि expression किस type का है।

Type Safe Generic MAX Implementation

Macros powerful होते हैं लेकिन उनमें type safety नहीं होती। C11 का _Generic इस problem को solve कर सकता है।

#define max(a,b) _Generic((a)+(b), \
    int: max_int, \
    float: max_float, \
    double: max_double \
)(a,b)
int max_int(int a, int b){
 return a>b?a:b;
}

float max_float(float a, float b){
 return a>b?a:b;
}

यह pattern compile-time dispatch का example है।

Generic Swap Macro

Production level C code में अक्सर generic swap macro का उपयोग किया जाता है।

#define SWAP(type,a,b) \
do { \
 type temp = a; \
 a = b; \
 b = temp; \
} while(0)
int x = 5;
int y = 10;

SWAP(int,x,y);

यह pattern Linux kernel सहित कई C projects में common है।

qsort() – Standard Library Generic Algorithm

C standard library का qsort() function generic programming का सबसे अच्छा practical example है।

void qsort(void *base,
           size_t num,
           size_t size,
           int (*compare)(const void *, const void *));

Example

int compare(const void *a, const void *b)
{
 return (*(int*)a - *(int*)b);
}

यह function void pointers का उपयोग करके किसी भी data type को sort कर सकता है।

Macros के Hidden Pitfalls

Generic macros powerful होते हैं लेकिन इनके साथ कुछ risks भी होते हैं।

1. Multiple Evaluation Problem

#define SQUARE(x) (x*x)
SQUARE(i++)

यह unexpected behavior दे सकता है।

2. Debugging Difficult

Macros compile time expand होते हैं इसलिए debugging कठिन होती है।

3. No Type Safety

Macros compiler type checking bypass कर सकते हैं।

Performance Comparison

Technique Type Safety Performance Flexibility
Macros Low Very Fast Medium
Void Pointers Medium Fast High
_Generic High Fast Medium

Advanced Developer Checklist

  • ✔ Prefer _Generic when using C11 or newer
  • ✔ Use void pointers for reusable libraries
  • ✔ Avoid complex macros unless necessary
  • ✔ Document type assumptions clearly
  • ✔ Benchmark generic algorithms for performance
  • ✔ Always test generic code with multiple data types

_Generic कैसे Type-Based Switch की तरह काम करता है

C11 का _Generic keyword एक तरह से type-based switch statement जैसा काम करता है।

जैसे switch value के आधार पर case चुनता है, वैसे ही _Generic expression के data type के आधार पर result चुनता है

Syntax Structure

_Generic(
    expression,
    type1 : result1,
    type2 : result2,
    default : result
)
  • expression – जिस variable या value का type check होगा
  • type1, type2 – supported types
  • default – fallback result

Compiler expression के type को evaluate करता है और उसी के अनुसार matching result return करता है।

_Generic Compile-Time पर Evaluate होता है

एक महत्वपूर्ण बात जो कई developers नहीं जानते:

_Generic runtime पर execute नहीं होता।

यह compile-time selection करता है।

इसका मतलब

  • Compiler program compile करते समय type check करता है
  • Runtime performance पर कोई extra overhead नहीं होता
  • यह technique function overloading simulate कर सकती है

इसी कारण _Generic selection को अक्सर compile-time type dispatch कहा जाता है।

C में Function Overloading Simulation

C language में real function overloading नहीं होता, लेकिन _Generic का उपयोग करके हम इसे simulate कर सकते हैं।

#include <stdio.h>

void print_int(int x){
    printf("Integer: %d\n", x);
}

void print_double(double x){
    printf("Double: %f\n", x);
}

#define print(X) _Generic((X), \
    int: print_int, \
    double: print_double \
)(X)

int main(){
    print(10);
    print(3.14);
}

यह macro argument के type के अनुसार अलग function call करता है।

_Generic में Default Case Optional होता है

अगर कोई matching type नहीं मिलता और default case define नहीं है तो compiler error generate करेगा।

Example

printf("%d",
 _Generic(1.0L,
 float: 1,
 double: 2,
 long double: 3
));

यह expression long double match करेगा।

लेकिन अगर expression किसी undefined type का हो, तो compiler error आएगा।

Macros में Type Safety जोड़ने के लिए _Generic

Traditional C macros में एक बड़ी problem होती है:

Macros type checking नहीं करते।

इसका मतलब macro किसी भी data type पर apply हो सकता है।

C11 का _Generic इस problem को solve करने में मदद करता है।

Example

#define datatype(x) _Generic((x), \
    int: "Integer", \
    long: "Long Integer", \
    char*: "String", \
    default: "Other Type")
printf("%s", datatype("Hello"));
printf("%s", datatype(10));

यह macro argument के type के आधार पर अलग output देगा।

Advantages of _Generic

  • Compile-time type selection
  • Function overloading simulate कर सकता है
  • Macros को type-safe बनाने में मदद करता है
  • Generic utilities बनाने में उपयोगी

Disadvantages of _Generic

  • Syntax complex हो सकता है
  • Debugging मुश्किल हो सकती है
  • Limited number of supported types
  • Large generic macros maintain करना कठिन हो सकता है

Type Generic Macros in Standard Library

C standard library में कई functions internally type-generic macros का उपयोग करते हैं।

Example:

  • tgmath.h
  • mathematical functions (sin, cos, sqrt)

यह macros automatically correct function select करते हैं जैसे:

  • sin()
  • sinf()
  • sinl()

यह selection internally _Generic mechanism का उपयोग करता है।

FAQs: Generics Keyword in C

1. क्या C language में Generics keyword होता है?

नहीं, traditional C language में Generics keyword मौजूद नहीं होता। लेकिन macros, void pointers और C11 के _Generic feature से generic programming की जा सकती है।

2. Generic Programming in C क्या होती है?

जब हम ऐसा code लिखते हैं जो कई data types के साथ काम करे तो उसे Generic Programming कहा जाता है।

3. C में generics implement करने के तरीके क्या हैं?

मुख्य तरीके हैं:

  • Macros
  • Void pointers
  • C11 का _Generic

4. क्या _Generic keyword C++ templates जैसा है?

नहीं। C++ templates ज्यादा powerful होते हैं। C का _Generic केवल compile time type selection करता है।

5. क्या professional C libraries generic programming उपयोग करती हैं?

हाँ। कई libraries और algorithms जैसे qsort() generic programming concept का उपयोग करते हैं।

📌 Further reading

Next
This is the most recent post.
Previous
Older Post

Post a Comment

Blogger

Your Comment Will be Show after Approval , Thanks

Support Our Content

Pay via UPI
Works with: GPay | PhonePe | Paytm | BHIM
anurajk.com@ptyes


More Payment Options / Scan QR →

Ads

 
↑ Top