_Generics Keyword In C https://www.skillvertex.com/blog Wed, 06 Mar 2024 11:42:43 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png _Generics Keyword In C https://www.skillvertex.com/blog 32 32 _Generics Keyword In C https://www.skillvertex.com/blog/generics-keyword-in-c/ https://www.skillvertex.com/blog/generics-keyword-in-c/#respond Wed, 06 Mar 2024 11:42:43 +0000 https://www.skillvertex.com/blog/?p=3708 Read more]]>

Table of Contents

_Generics Keyword In C

The drawbacks of _Generic include:

  1. Macro Usage Vulnerability:
  • _Generic used in macros can be error-prone.
  1. Complex Syntax and Debugging Challenges:
  • The syntax is intricate, making it challenging to understand and debug errors in the expanded code.

The _Generic keyword in C allows the implementation of generic code that can execute statements based on the type of provided arguments. It is commonly used with C macros to simulate function overloading. This keyword was introduced in the C11 Standard.

Syntax of _Generic in C

_Generic( (expression),
    type_1: statements,
    type_2: statements,
    .
    .
    default: statements
)

In C, the _Generic keyword helps execute statements based on the type of a given expression. If the type matches one we’ve defined, the corresponding statements run. If there’s no match, the statements under “default” are executed. It’s similar to the switch statement where different actions happen for different case values.

How to use _Generic in C?

_Generic keyword is used in any place inside the code but, the generic code is required for that. The example provided below will illustrate the use of the _Generic keyword.

// C program to illustrate macro function. 
#include <stdio.h> 
int main(void) 
{ 
    // _Generic keyword acts as a switch that chooses 
    // operation based on data type of argument. 
    printf("%d\n", _Generic(1.0L, float : 1, double : 2, 
                            long double : 3, default : 0)); 
    
    printf("%d\n", _Generic(1L, float : 1, double : 2, 
                            long double : 3, default : 0)); 
    
    printf("%d\n", _Generic(1.0L, float : 1, double : 2, 
                            long double : 3)); 
    return 0; 
}

Output

3
0
3

_Generic in Macros

A major disadvantage of Macro in C is arguments lack type checking, i.e. a macro can operate on different types of variables(like char, int, double,..) without type checking. Hence, We can use _Generic to the statements for different argument types in such macros.

Example

// C program to illustrate macro function. 
#include <stdio.h> 
  
// Defining the macro with generic code for 
//different argument types 
#define skill vertex(T) _Generic((T), \ 
        char* : "String", \ 
        int : "Integer", \ 
        long : "Long Integer", \ 
        default : "Others") 
  
int main(void) 
{ 
  
    // Here A is a string. 
    printf("%s\n", skillvertex("A")); 
  
    // floating point value 
    printf("%s\n", skillvertex(5)); 
    
      // float type which is not defined in the macro 
      printf("%s", skillvertex(5.12)); 
  
    return 0; 
}

Output

String
Integer
Others

In C, the behavior of _Generic resembles C++ function overloading, where different expressions are returned for different data types. This allows us to bring some aspects of function overloading into our C programs.

Advantages of _Generic

  • It works to stimulate function overloading when used with macros.
  • _Generic will help the functionality to execute the code based on the type of parameters.

Disadvantages of _Generic

The major disadvantages of _Generic are as follows

The drawbacks of _Generic include:

a.Macro Usage Vulnerability:

  • _Generic used in macros can be error-prone.

b.Syntax and Debugging Challenges:

  • The syntax is intricate, making it challenging to understand and debug errors in the expanded code.

FAQ- _Generics Keyword in C

Q1.What is the _generic keyword in C++?

Ans. The _Generic keyword is basically functions to write code that can choose an expression at compile time based on the type of the argument. It works similarly to overloading in C++ where the type of the argument selects which function to call. Whereas,this type of the argument selects which expression to evaluate.

Q2. What is the advantage of using generic language model?

Ans. Generics in programming help create versatile algorithms that can handle different types of collections. This makes the code customizable, easy to understand, and ensures safety with types, making it simpler for programmers to work with.

Q3. What is generic in C?

Ans. Generics in programming mean allowing types (like Integer, String, and user-defined types) to be parameters for methods, classes, and interfaces. For instance, classes such as arrays and maps can be efficiently used with generics, making them adaptable for any type.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/generics-keyword-in-c/feed/ 0