



{"id":4575,"date":"2018-09-01T20:58:52","date_gmt":"2018-09-01T15:28:52","guid":{"rendered":"http:\/\/computersirkiclass.com\/?p=4575"},"modified":"2026-03-18T18:21:59","modified_gmt":"2026-03-18T12:51:59","slug":"preprocessor-directives-in-c","status":"publish","type":"post","link":"https:\/\/computersirkiclass.com\/cskc\/preprocessor-directives-in-c\/","title":{"rendered":"Preprocessor Directives in C++"},"content":{"rendered":"<div id=\"modal-ready\"><p>As the name signifies, pre-processor directives are instructions given to compiler which it has to run before main code compilation will begin. You must be frequently seeing #include as the top of most C++ program codes. It is nothing but a pre-processor directive to tell compiler that definitions of certain classes, function and objects have to be read before using them in the main program. Overall following major types of pre-processor directives are there.<\/p>\n<ul>\n<li>File Inclusion<\/li>\n<li>Macro Definition<\/li>\n<li>Conditional Compilation<\/li>\n<li>Other types<\/li>\n<\/ul>\n<h3>File Inclusion<\/h3>\n<p>This is the most common directive you come across everyday in most programs. Examples are &#8211;<\/p>\n<pre>#inlude &lt;iostream&gt; \/\/To use things like cout, cin, endl etc.\r\n\r\n#include &lt;string.h&gt;\u00a0 \/\/To include c style string related functions.\r\n\r\n#include &lt;math.h&gt; \/\/To include things like sqrt, pow, sin, cos etc.<\/pre>\n<h3>Macro definition<\/h3>\n<p>This is a way in C++ which allows replacement of some values in program with their pre-defined values. For e.g.<\/p>\n<pre>#define PI 3.1416\r\n\r\ndouble circum=2*PI*r;<\/pre>\n<p>after compilation pass it will become<\/p>\n<pre>double circum=2*3.1416*r;<\/pre>\n<p>So if PI occurs in my formula, it will be replaced by value 3.1416 . For this replacement nature of activity this directive is also called pre-processor macros.<\/p>\n<p>One may have formula style macro definition like &#8211;<\/p>\n<pre>#define velocity(u,a,t)\u00a0 u+a*t\r\n\r\nint v=velocity(5,2,3);<\/pre>\n<p>The above code will apply the velocity formula as given in the define and fill value of 11 in the v named variable.<\/p>\n<h3>Conditions Compilation<\/h3>\n<p>This allows to publish multiple version of executable codes of program based on conditions set using the #define directive. For e.g.<\/p>\n<pre>#define Zone 1\r\n#ifdef Zone\r\n\u00a0\u00a0char msg[]=\"Zone information in use\";\r\n#else\u00a0 \r\n\u00a0 char msg[]=\"Zone information not in use\";\r\n#endif<\/pre>\n<p>In the above example c style string msg will be set to Zone information in use as Zone macro has been defined above it. Based on this setting a program may take a different path of compilation.<\/p>\n<h3>Other directives<\/h3>\n<p>There many other pre-processor directives also for different purposes for e.g.<\/p>\n<p>#undef (For undefining a pre-defined macro)<\/p>\n<p>#pragma (For turning on and off some compilation features)<\/p>\n<p>#error (to show diagnostic error messages while compilation)<\/p>\n<p>The standard documentation for C++ pre-processor directives can be seen<strong> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/preprocessor\" target=\"_blank\" rel=\"noopener\">here<\/a><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>As the name signifies, pre-processor directives are instructions given to compiler which it has to run before main code compilation will begin. You must be frequently seeing #include as the top of most C++ program codes. It is nothing but a pre-processor directive to tell compiler that definitions of certain classes, function and objects have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7766,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[114,99,98],"tags":[234],"model_paper":[],"class_list":["post-4575","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cnr","category-cpp","category-cpp-all","tag-cp330-preprocessor-directives-and-macros","cpp_index-cp9010-preprocessor-directives-and-macros"],"acf":[],"_links":{"self":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts\/4575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/comments?post=4575"}],"version-history":[{"count":3,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts\/4575\/revisions"}],"predecessor-version":[{"id":4579,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts\/4575\/revisions\/4579"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/media\/7766"}],"wp:attachment":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/media?parent=4575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/categories?post=4575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/tags?post=4575"},{"taxonomy":"model_paper","embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/model_paper?post=4575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}