{"id":3708,"date":"2024-03-06T11:42:43","date_gmt":"2024-03-06T11:42:43","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3708"},"modified":"2024-03-06T11:42:43","modified_gmt":"2024-03-06T11:42:43","slug":"generics-keyword-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/generics-keyword-in-c\/","title":{"rendered":"_Generics Keyword In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#generics-keyword-in-c\">_Generics Keyword In C<\/a><\/li><li ><a href=\"#syntax-of-generic-in-c\">Syntax of _Generic in C<\/a><\/li><li ><a href=\"#how-to-use-generic-in-c\">How to use _Generic in C? <\/a><\/li><li ><a href=\"#generic-in-macros\">_Generic in Macros <\/a><\/li><li ><a href=\"#advantages-of-generic\">Advantages of _Generic<\/a><\/li><li ><a href=\"#disadvantages-of-generic\">Disadvantages of _Generic<\/a><\/li><li ><a href=\"#faq-generics-keyword-in-c\">FAQ- _Generics Keyword in C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"generics-keyword-in-c\">_Generics Keyword In C<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The drawbacks of <code>_Generic<\/code> include:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Macro Usage Vulnerability:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>_Generic<\/code> used in macros can be error-prone.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Complex Syntax and Debugging Challenges:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The syntax is intricate, making it challenging to understand and debug errors in the expanded code.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>_Generic<\/code> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-generic-in-c\">Syntax of _Generic in C<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>_Generic( (expression),\n    type_1: statements,\n    type_2: statements,\n    .\n    .\n    default: statements\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In C, the <code>_Generic<\/code> keyword helps execute statements based on the type of a given expression. If the type matches one we&#8217;ve defined, the corresponding statements run. If there&#8217;s no match, the statements under &#8220;default&#8221; are executed. It&#8217;s similar to the <code>switch<\/code> statement where different actions happen for different case values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-generic-in-c\">How to use _Generic in C?<br><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">_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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate macro function. \n#include &lt;stdio.h&gt; \nint main(void) \n{ \n    \/\/ _Generic keyword acts as a switch that chooses \n    \/\/ operation based on data type of argument. \n    printf(\"%d\\n\", _Generic(1.0L, float : 1, double : 2, \n                            long double : 3, default : 0)); \n    \n    printf(\"%d\\n\", _Generic(1L, float : 1, double : 2, \n                            long double : 3, default : 0)); \n    \n    printf(\"%d\\n\", _Generic(1.0L, float : 1, double : 2, \n                            long double : 3)); \n    return 0; \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3\n0\n3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"generic-in-macros\">_Generic in Macros<br><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A major disadvantage of&nbsp;<a href=\"https:\/\/www.geeksforgeeks.org\/interesting-facts-preprocessors-c\/\" rel=\"nofollow noopener\" target=\"_blank\">Macro in C<\/a>&nbsp;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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate macro function. \n#include &lt;stdio.h&gt; \n  \n\/\/ Defining the macro with generic code for \n\/\/different argument types \n#define skill vertex(T) _Generic((T), \\ \n        char* : \"String\", \\ \n        int : \"Integer\", \\ \n        long : \"Long Integer\", \\ \n        default : \"Others\") \n  \nint main(void) \n{ \n  \n    \/\/ Here A is a string. \n    printf(\"%s\\n\", skillvertex(\"A\")); \n  \n    \/\/ floating point value \n    printf(\"%s\\n\", skillvertex(5)); \n    \n      \/\/ float type which is not defined in the macro \n      printf(\"%s\", skillvertex(5.12)); \n  \n    return 0; \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String\nInteger\nOthers<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In C, the behavior of <code>_Generic<\/code> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages-of-generic\">Advantages of _Generic<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It works to stimulate function overloading when used with macros.<\/li>\n\n\n\n<li>_Generic will help the functionality to execute the code based on the type of parameters.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"disadvantages-of-generic\">Disadvantages of _Generic<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The major disadvantages of _Generic are as follows<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The drawbacks of <code>_Generic<\/code> include:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a.<strong>Macro Usage Vulnerability:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>_Generic<\/code> used in macros can be error-prone.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">b.<strong>Syntax and Debugging Challenges:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The syntax is intricate, making it challenging to understand and debug errors in the expanded code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-generics-keyword-in-c\">FAQ- _Generics Keyword in C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1700724408052\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.What is the _generic keyword in C++?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>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.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700724412567\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the advantage of using generic language model?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>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.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700724416615\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is generic in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>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.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>_Generics Keyword In C The drawbacks of _Generic include: 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 &#8230; <a title=\"_Generics Keyword In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/generics-keyword-in-c\/\" aria-label=\"More on _Generics Keyword In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5459,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[613],"class_list":["post-3708","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-_generics-keyword-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3708"}],"version-history":[{"count":7,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3708\/revisions"}],"predecessor-version":[{"id":7958,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3708\/revisions\/7958"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5459"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}