{"id":4746,"date":"2021-05-08T00:01:08","date_gmt":"2021-05-08T07:01:08","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4746"},"modified":"2021-05-01T10:58:51","modified_gmt":"2021-05-01T17:58:51","slug":"the-_generic-keyword","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4746","title":{"rendered":"The <em>_Generic<\/em> Keyword"},"content":{"rendered":"<p>The C11 standard added the &#8220;underscore&#8221; bunch to the C language&#8217;s traditional keywords:<\/p>\n<p><em>_Alignas<br \/>\n_Alingof<br \/>\n_Atomic<br \/>\n_Bool<br \/>\n_Complex<br \/>\n_Generic<br \/>\n_Imaginary<br \/>\n_Noreturn<br \/>\n_Static_assert<br \/>\n_Thread_local<\/em><\/p>\n<p>I don&#8217;t routinely use any of these in my programs, beyond trying a few out to see how they work. The <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2953\"><em>_Bool<\/em><\/a> keyword comes in handy. The rest? Well, they&#8217;re worth exploring from a curiosity standpoint. For this week&#8217;s Lesson, I reveal the mysteries of the <em>_Generic<\/em> keyword.<br \/>\n<!--more--><br \/>\nIt would be neat if computer scientists just threw in these new keywords for fun. They&#8217;d say, &#8220;This new underscore word will really drive the coders nuts!&#8221; But no, these keywords have their roots in problems that were originally solved in other ways. This tale tells true for the <em>_Generic<\/em> keyword, which has its roots in C&#8217;s various high-level math functions.<\/p>\n<p>For example, different math functions are required for specific data types. You may not be aware of these differences because the math function is really a macro. Internally, the macro calls a specific function based on the data type used as an argument. This process explains how the <em>_Generic<\/em> keyword came about; its job is to help determine a data type.<\/p>\n<p>Here is the format for <em>_Generic<\/em>, which behaves more like a function than a keyword:<\/p>\n<p><code><em>r<\/em> = _Generic( <em>expression<\/em>, <em>list<\/em>... );<\/code><\/p>\n<p><em>r<\/em> is an <em>int<\/em> value generated by the <em>_Generic<\/em> keyword.<\/p>\n<p><em>expression<\/em> is the variable or constant to be evaluated.<\/p>\n<p><em>list<\/em> is a menu of options. It works similar to <em>case<\/em> statements in a <em>switch-case<\/em> structure. The <em>list<\/em> presents a series of data types followed by a colon and an expression. These items are separated by commas, and a <em>default<\/em> item is present to handle no-matches:<\/p>\n<p><code>_Generic( var, char: 1, int: 2, long: 3, default: 0 );<\/code><\/p>\n<p>Above, assume the data type for <code>var<\/code> is <em>int<\/em>. Therefore, the value 2 is generated by the <em>_Generic<\/em> keyword.<\/p>\n<p>Here&#8217;s another, more practical example from a math function standpoint:<\/p>\n<p><code>#define cbrt(X) _Generic((X), long double: cbrtl, float: cbrtf, default: cbrt )<\/code><\/p>\n<p>In this declaration, the <code>cbrt()<\/code> macro can evaluate to either the <em>cbrtl()<\/em>, <em>cbrtf()<\/em> or <em>cbrt()<\/em> function depending on the data type of argument <code>X<\/code>. In this manner, <em>_Generic<\/em> helps the code to implement the proper cubed root function based on the argument&#8217;s data type.<\/p>\n<p>Because I&#8217;m mathematically disinclined, here is some sample code you can play with:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_05_08-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_05_08-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char a;\r\n    int r;\r\n\r\n    r = _Generic( a, char: 1, int: 2, long: 3, default: 0 );\r\n    printf(\"'a' is \");\r\n    switch(r)\r\n    {\r\n        case 1:\r\n            puts(\"a char\");\r\n            break;\r\n        case 2:\r\n            puts(\"an int\");\r\n            break;\r\n        case 3:\r\n            puts(\"a long\");\r\n            break;\r\n        default:\r\n            puts(\"unknown\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 8, the <em>_Generic<\/em> keyword evaluates the data type of variable <code>a<\/code>. In this case, it&#8217;s a <em>char<\/em>, so <em>int<\/em> value 1 is returned. This value is saved in variable <code>r<\/code> and used in the <em>switch-case<\/em> structure starting at Line 10. Here is the output:<\/p>\n<p><code>'a' is a char<\/code><\/p>\n<p>You can change the declaration of variable <code>a<\/code> at Line 5 to <em>int<\/em>, <em>long<\/em>, and so on to see how output is affected. If you change the data type to <em>float<\/em> or a pointer, the <em>default<\/em> value of 0 is generated.<\/p>\n<p>This Lesson may not convince you to use <em>_Generic<\/em> in your code, but I hope you appreciate its worth. Perhaps you can work it in somehow, but like the most of the underscore keywords, it remains a curiosity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C11 standard formalized introduction of the keyword <em>_Generic<\/em>. Why? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4746\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4746","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4746"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4746\/revisions"}],"predecessor-version":[{"id":4756,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4746\/revisions\/4756"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}