{"id":23269,"date":"2023-12-17T12:12:46","date_gmt":"2023-12-17T12:12:46","guid":{"rendered":"https:\/\/linuxways.net\/?p=23269"},"modified":"2024-03-25T02:10:27","modified_gmt":"2024-03-25T02:10:27","slug":"preprocessor-directives-c","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/preprocessor-directives-c\/","title":{"rendered":"Preprocessor Directives in C"},"content":{"rendered":"<p>The preprocessor of a compiler acts in the first instance of compilation. In this instance, for example, the compiler inserts the additional code of the functions that is defined in the headers that we insert and call in the code, the defined constants that they will use, and so on.<\/p>\n<p>The preprocessor configures the compilation based on the requirements of the code that we want to compile using certain directives. Although we generally make little use of the preprocessor directives in simple programs, many of them configure the compilation through the headers that we insert. These directives are a very useful resource to ensure the portability and reduce the size of the final program code by eliminating unnecessary fragments during compilation.<\/p>\n<p>In this Linux Ways article, you&#8217;ll learn what preprocessor directives in C are all about and how to use them. We&#8217;ll look at each of the directives available in C, explain their usefulness in detail, and include a practical example with code and pictures where you can learn how they work and quickly put them into practice.<\/p>\n<h2><a id=\"post-23269-_asg3jcvy9jtq\"><\/a>#Include Directive in the C Language<\/h2>\n<p>The #include directive includes the headers in the compilation where the functions that we use, their variables, structures, directives, etc. are declared. When the precompiler encounters this directive, it replaces it with the code that is contained in the specified file. Let\u2019s see the #include syntax:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#define &lt;file.extension&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#define &quot;file.extension&quot;<\/span><\/div><\/div>\n<p>If the name of the header is between the <strong><em>&lt; &gt; <\/em><\/strong>symbols, the precompiler looks for the files in the \u201cincludes\u201d of the libraries. While if it is between the quotation marks, it looks in the directory where the main file of \u201cc\u201d that is to be compiled is located. The #include directives must be in the first lines of the code.<\/p>\n<h2><a id=\"post-23269-_hy31otaezd0b\"><\/a>How to Use the #Include Directive to Include the Headers in the C Language<\/h2>\n<p>In this example, we will see how to use the #include directive to include the \u201cstdio.h\u201d header which defines the printf() function that we will use to print the <em>\u201chello World\u201d<\/em> message in the command console. Let\u2019s see the code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Hello World<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The following image shows the compilation and execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23273\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-1.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-1-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-1-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><a id=\"post-23269-_539yupea9vd\"><\/a>Had we not used this directive to include the \u201cstdio.h\u201d header, the compiler would not have found the declaration of the printf() function which leads to the following error, one of the classic errors when compiling with GCC if we fail to include a header:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23276\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-2.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-2-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-2-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23269-_ba9c8dr25gjy\"><\/a>#Define and #Undef Directives in the C Language<\/h2>\n<p>The #define directive is very useful in C and we can find it in the various headers that define the functions. The usefulness of this directive is wide and it is used to:<\/p>\n<ol>\n<li>Define the constants and flags<\/li>\n<li>Define the macro functions<\/li>\n<li>Include the code snippets to the compilation using the \u201cthis\u201d and \u201cconditionals\u201d directives<\/li>\n<\/ol>\n<p>Let\u2019s see the syntax that is used by this directive to declare a constant and assign it with a value:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#define &lt;CONSTANT&gt; &lt;VALUE&gt;<\/span><\/div><\/div>\n<p>To define a macro function with this directive, we have to use the following syntax:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#define &lt;function prototype&gt; &lt;expression&gt;<\/span><\/div><\/div>\n<p>The #undef directive has the opposite effect of the #define and is used to remove the definitions that have already been created. Let\u2019s see the syntax of this declaration to undefine a constant:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#undef &lt;CONSTANT&gt;<\/span><\/div><\/div>\n<h2><a id=\"post-23269-_knlics5c9qc1\"><\/a><br \/>\nHow to Define a Constant with the #Define Directive in the C Language<\/h2>\n<p>In this example, we define a constant with the CONST_1 identifier and assign it with the value of 15. Then, we print its value with the printf() function using the constant as an input argument.<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#define CONST_1 15<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%i&quot;<\/span><span class=\"sy0\">,<\/span> CONST_1<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Let\u2019s see the following image with the compilation and execution of this code that shows the value that is defined to the CONST_1 constant by the #define directive:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23279\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-3.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-3-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-3-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Let&#8217;s see what happens when we remove the definition of the CONST_1 constant with the #undef directive:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#define CONST_1 15<\/span><br \/>\n<br \/>\n<span class=\"co2\">#undef CONST_1<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%i&quot;<\/span><span class=\"sy0\">,<\/span> CONST_1<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>As the following figure shows, the CONST_1 constant was removed by the #undef directive, so the compiler could not find it and issued an error.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23282\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-4.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-4-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-4-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23269-_jk6ffov0wyfi\"><\/a>How to Define a Macro Function with the #Define Directive in the C Language<\/h2>\n<p>Let\u2019s see an example where we define the conv() macro function with the #define directive which converts its input argument from inches to centimeters:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#define conv(b) a * 2.54<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">float<\/span> a <span class=\"sy0\">=<\/span><span class=\"nu16\">2.5<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%f inches equal to %f centimeters<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"sy0\">,<\/span> conv<span class=\"br0\">&#40;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Let\u2019s see the following image with the compilation and execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23285\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-5.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-5-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-5-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23269-_7go6jqhjsoq5\"><\/a>Conditional Preprocessor Directives<\/h2>\n<p>These conditionals include or exclude the code fragments from the compilation based on a previous definition or the result of a relational operation.<\/p>\n<p>These directives allow us to include only the headers, libraries, and fragments that are necessary for a particular compilation which significantly reduces the final size of the program by not including the unnecessary code. Also, the code becomes portable and we can specify the different compilation modes which we can easily define with the #define directive.<\/p>\n<p>In the following, you will find a list of conditional preprocessor directives available in C:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Directive <\/strong><\/td>\n<td><strong>Entry condition<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>#ifdef &lt;condition&gt;<\/strong><\/td>\n<td>If the condition was previously defined<\/td>\n<\/tr>\n<tr>\n<td><strong>#ifndef &lt;condition&gt;<\/strong><\/td>\n<td>If the condition was not previously defined<\/td>\n<\/tr>\n<tr>\n<td><strong>#if &lt;condition&gt;<\/strong><\/td>\n<td>If the condition is true<\/td>\n<\/tr>\n<tr>\n<td><strong>#elif &lt;condition&gt;<\/strong><\/td>\n<td>If the condition is true and false for #if<\/td>\n<\/tr>\n<tr>\n<td><strong>#else<\/strong><\/td>\n<td>If the condition result false for #if and #elif<\/td>\n<\/tr>\n<tr>\n<td><strong>#endif (Delimiter)<\/strong><\/td>\n<td>End of conditional<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Whenever we open one or a series of conditional directives, we must mark their end with #endif.<\/p>\n<h2><a id=\"post-23269-_b7oqewf26h3x\"><\/a>The #Ifdef and #Ifndef Conditional Directives in the C Language<\/h2>\n<p>The #ifdef and #ifndef directives are conditional directives that depend on a previous definition. Let\u2019s see how the #ifdef directive works:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#define US<\/span><br \/>\n<br \/>\n<span class=\"co2\">#ifdef US<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/If US is defined, the compiler will include this code in the compilation.<\/span><\/div><\/div>\n<p>The #ifndef directive works in the opposite way. It includes the assigned code if the constant of its condition is not defined which, in this case, is US.<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#ifndef US<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/If US is NOT defined, the compiler will include this code in the compilation.<\/span><\/div><\/div>\n<h2><a id=\"post-23269-_kvbiwe652rcu\"><\/a><br \/>\nHow to Use the #Ifdef and #Ifndef Directives in C Language<\/h2>\n<p>In this example, we will see how we can use the #ifdef and #ifndef conditional directives to make our code portable while creating a lightweight and readable program. To do this, we create a simple application that calculates the circumference of a circle based on a radius that we enter through the console. The interesting thing is that it supports two languages, English (US) and Spanish for Latin America (LATAM) and uses the unit of measurement corresponding to the zone chosen. Let\u2019s see the C code for this program:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/Here we define which zone we are going to use. To use the LATAM zone uncomment the following line.<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/#define LATAM<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &quot;myheader.h&quot;<\/span><br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">float<\/span> a<span class=\"sy0\">,<\/span> b<span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%s&quot;<\/span><span class=\"sy0\">,<\/span> in<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/scanf.html\"><span class=\"kw3\">scanf<\/span><\/a> &nbsp;<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%f&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"sy0\">&amp;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%f (%f %s)<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"sy0\">,<\/span> conv<span class=\"br0\">&#40;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">,<\/span> unit_conv<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nb<span class=\"sy0\">=<\/span><span class=\"br0\">&#40;<\/span>a<span class=\"sy0\">*<\/span><span class=\"nu0\">2<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">*<\/span><span class=\"nu16\">3.1416<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span>out<span class=\"sy0\">,<\/span> b<span class=\"sy0\">,<\/span> conv<span class=\"br0\">&#40;<\/span>b<span class=\"br0\">&#41;<\/span><span class=\"sy0\">,<\/span> unit_conv<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>As we can see, in this very simple code, we add the \u201cmyheader.h\u201d header which we save in the same directory where we have the \u201c.c\u201d file.<\/p>\n<p>In the \u201cmyheader.h\u201d header, we use the #ifdef directive and write the code for LATAM in it. We do the same with the #ifndef directive in which we write the code for US. In each of these two directives, we define a macro function to convert the unit of measurement used in that area, as well as the constant strings that correspond to the language of the selected area. Let\u2019s see the code for this header:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#define MYHEADER_H<\/span><br \/>\n<br \/>\n<span class=\"co2\">#ifdef LATAM<\/span><br \/>\n<span class=\"co1\">\/\/If LATAM is defined, the compiler will include this code in the compilation.<\/span><br \/>\n<br \/>\n<span class=\"co2\">#define conv(b) b \/ 2.54<\/span><br \/>\n<span class=\"kw4\">char<\/span> in<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;Zona Latino America<span class=\"es1\">\\n<\/span>Ingresar radio en cent\u00edmetros <span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> unit_conv<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;pulgadas&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> out<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;La circunferencia en pulgadas es %f (%f %s) <span class=\"es1\">\\n<\/span>Una pulgada equivale a 2.54cm<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"co2\">#endif<\/span><br \/>\n<br \/>\n<span class=\"co2\">#ifndef LATAM<\/span><br \/>\n<span class=\"co1\">\/\/If LATAM is NOT defined, the compiler will include this code in the compilation.<\/span><br \/>\n<span class=\"co2\">#define conv(b) b \/ 0.393701<\/span><br \/>\n<span class=\"kw4\">char<\/span> in<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;Zone US<span class=\"es1\">\\n<\/span>Enter ratio in inches<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> unit_conv<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;cm&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> out<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;The circumference in inches is %f (%f %s) <span class=\"es1\">\\n<\/span>One inch is equal to 2.54cm<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"co2\">#endif<\/span><\/div><\/div>\n<p>If LATAM is not defined in our code, the compiler defaults to compiling what is in the #ifndef directive, that is, the English-language strings. If LATAM is defined, it compiles the code corresponding to the #ifdef directive in which the strings are in Spanish. We insert this definition in the C code before the line that contains the \u201cmyheader.h\u201d header. Let\u2019s see how this code is compiled and executed by default:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23286\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-6.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-6-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-6-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Now, let&#8217;s uncomment the LATAM definition of our \u201cmain.c\u201d code and compile and run it again:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23289\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-7.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-7-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-7-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>As we can see, using the conditional directives, we can develop a single code to compile different versions of the same program, omitting unnecessary code that slows down the process and takes up the memory.<\/p>\n<h2><a id=\"post-23269-_52k744ktngiq\"><\/a>The #If, #Elif, and #Else Directives in the C language<\/h2>\n<p>These conditional directives include the code fragments in the compilation depending on whether the result of a relational operation between values of predefined constants is true or false. Then, we will see how these directives are applied:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#define LATAM 1<\/span><br \/>\n<span class=\"co2\">#define FR &nbsp; &nbsp;2<\/span><br \/>\n<br \/>\n<span class=\"co2\">#define ZONE FR<\/span><br \/>\n<br \/>\n<span class=\"co2\">#if ZONE == LATAM<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/If LATAM is defined, the compiler will include this code in the compilation.<\/span><br \/>\n<br \/>\n<span class=\"co2\">#elif ZONE == FR<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/If FR is defined, the compiler will include this code in the compilation.<\/span><br \/>\n<br \/>\n<span class=\"co2\">#else<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/If LATAM ot FR is not defined, the compiler will include this code in the compilation.<\/span><br \/>\n<br \/>\n<span class=\"co2\">#endif<\/span><\/div><\/div>\n<p>The preprocessor includes the code that is contained in the conditional statement whose result of the relational operation with the ZONE constant equals to true which, in this case, is the #elif statement with the FR constant.<\/p>\n<h2><a id=\"post-23269-_e8aqsgpeaw5w\"><\/a>How to Use the #If, #Elif, and #Else Conditional Directives in the C Language<\/h2>\n<p>Now, let&#8217;s look at an example where we use these three directives that take the code from the previous example and make some changes to the \u201cmyheader.h\u201d file to add the French language to the program.<\/p>\n<p>First, we define the FR, LATAM, and US constants and assign a value to each. Also, in the C code, before inserting the \u201cmyheader.h\u201d header, we define the ZONE constant which we will use to select the language of the program by assigning the value of one of the three constants that we just defined. The ZONE constant will be the condition for the conditional directives.<\/p>\n<p>Next, we add the #if, #elif, and #else directives in the header. The code for the FR language is inserted under the #if directive, the LATAM code under #elif, and the US code under the #else directive. This way, we have three compilation versions. But if you want to add more, you can insert as many #elif statements as you want, as long as each has a different constant value in the relational operation.<\/p>\n<p>The #else directive does not perform any relational operations, so US is the default language and can be entered with any value except FR or LATAM. Let\u2019s see the code for the \u201cmyheader.h\u201d header:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;height:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#ifndef MYHEADER_H<\/span><br \/>\n<span class=\"co2\">#define MYHEADER_H<\/span><br \/>\n<span class=\"co1\">\/\/Here we define the constants with the options that we are going to use.<\/span><br \/>\n<span class=\"co2\">#define US &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; 0<\/span><br \/>\n<span class=\"co2\">#define FR &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; 1<\/span><br \/>\n<span class=\"co2\">#define LATAM &nbsp; &nbsp; &nbsp; 2<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/Here we define the compilation option for FR.<\/span><br \/>\n<span class=\"co2\">#if ZONE == FR<\/span><br \/>\n<span class=\"co2\">#define conv(b) b \/ 2.54<\/span><br \/>\n<span class=\"kw4\">char<\/span> in<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;Zone France<span class=\"es1\">\\n<\/span>Entrez le rayon en pouces<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> unit_conv<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;cm&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> out<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;La circonf\u00e9rence en pouces est %f (%f %s) <span class=\"es1\">\\n<\/span>Un pouce \u00e9quivaut \u00e0 2,54 cm&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/Here we define the compilation option for LATAM.<\/span><br \/>\n<span class=\"co2\">#elif ZONE == LATAM<\/span><br \/>\n<span class=\"co2\">#define conv(b) b \/ 2.54<\/span><br \/>\n<span class=\"kw4\">char<\/span> in<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;Zona Latino America<span class=\"es1\">\\n<\/span>Ingresar radio en cent\u00edmetros <span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> unit_conv<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;pulgadas&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> out<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;La circunferencia en pulgadas es %f (%f %s) <span class=\"es1\">\\n<\/span>Una pulgada equivale a 2.54cm&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/Here we define the compilation option for the USA.<\/span><br \/>\n<span class=\"co2\">#else<\/span><br \/>\n<span class=\"co2\">#define conv(b) b \/ 0.393701<\/span><br \/>\n<span class=\"kw4\">char<\/span> in<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;Zone US<span class=\"es1\">\\n<\/span>Enter ratio in inches<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> unit_conv<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;cm&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> out<span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;The circumference in inches is %f (%f %s)<span class=\"es1\">\\n<\/span>One inch is equal to 2.54cm&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#endif<\/span><br \/>\n<span class=\"co2\">#endif<\/span><\/div><\/div>\n<p>To choose in which language we want to compile our program, we just need to define the ZONE constant in the code and assign it with one of the three values (US, FR or LATAM) that are defined in the header. Let\u2019s see the code with the language options that are commented out so that the default zone, which is US, is compiled:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/Here we define which zone we are going to use.<\/span><br \/>\n<span class=\"co2\">#define ZONE US <\/span><br \/>\n<span class=\"co1\">\/\/#define ZONE LATAM<\/span><br \/>\n<span class=\"co1\">\/\/#define ZONE FR<\/span><br \/>\n<span class=\"co2\">#include &quot;myheader.h&quot;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">float<\/span> a<span class=\"sy0\">,<\/span> b<span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%s&quot;<\/span><span class=\"sy0\">,<\/span> in<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/scanf.html\"><span class=\"kw3\">scanf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%f&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"sy0\">&amp;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%f (%f %s)<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"sy0\">,<\/span> conv<span class=\"br0\">&#40;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">,<\/span> unit_conv<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nb<span class=\"sy0\">=<\/span><span class=\"br0\">&#40;<\/span>a<span class=\"sy0\">*<\/span><span class=\"nu0\">2<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">*<\/span><span class=\"nu16\">3.1416<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span>out<span class=\"sy0\">,<\/span> b<span class=\"sy0\">,<\/span> conv<span class=\"br0\">&#40;<\/span>b<span class=\"br0\">&#41;<\/span><span class=\"sy0\">,<\/span> unit_conv<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Let\u2019s see the execution of this code with its default US compilation:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23293\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-8.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-8-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-8-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><a id=\"post-23269-_zhtr1mkera8u\"><\/a>Now, we comment out the ZONE US definition and uncomment the ZONE LATAM definition, compile them, and run them again.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23294\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-9.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-9-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-9-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><a id=\"post-23269-_14rcrkdn4ck8\"><\/a>Now, we comment out the ZONE definition and uncomment the ZONE FR definition, compile them, and run them again.<\/p>\n<p><a id=\"post-23269-_e66kvmqg56fz\"><\/a>In this way, we can use the #if, #elif, and #else conditional directives to specify the various compilation options by simply specifying the value of a constant.<\/p>\n<p>&nbsp;<\/p>\n<h2><a id=\"post-23269-_qih3v1q7o3xm\"><\/a>#Error and #Warning Directives in the C Language<\/h2>\n<p>These directives are used to send error or warning messages to the compiler output. These directives are usually used in headers within error detection conditionals. The message to be displayed in the compiler output must be in the text form and not a string. Let\u2019s see an example of the use of these two directives:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#error This is a test of the error directive.<\/span><br \/>\n<br \/>\n<span class=\"co2\">#warning This is a test of the warning directive.<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>In the following figure, we will see the compilation of this code with the error and warning messages it contains:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23295\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-11.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-11-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23269-11-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2><a id=\"post-23269-_8epxktdgrlka\"><\/a>Conclusion<\/h2>\n<p>In this Linux Ways article, we explained to you what the preprocessor directives are all about and what their function is when compiling the code. In this article, we looked at each of these directives and showed you their syntax and usage. To help you quickly implement the use of preprocessor directives, we included a practical example for each of them, with code fragments and images, so that you can see the potential of these directives for code portability and optimization of the final program.<\/p>","protected":false},"excerpt":{"rendered":"<p>Guide on what preprocessor directives in C are, how to use them, the directives available in C, their usefulness, and the practical examples on how they work.<\/p>","protected":false},"author":110,"featured_media":23305,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[],"class_list":["post-23269","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=23269"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23269\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/23305"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=23269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=23269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=23269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}