{"id":82293,"date":"2021-07-29T09:00:22","date_gmt":"2021-07-29T03:30:22","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=82293"},"modified":"2021-07-29T09:00:22","modified_gmt":"2021-07-29T03:30:22","slug":"macros-in-c","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/","title":{"rendered":"Macros in C &#8211; Types and Examples"},"content":{"rendered":"<p>C programming offers various types of functions and methods to the programmers. It helps in turning the large and complex codes into simple and easy. In C, the program code is first compiled and then it is sent to the compiler.<\/p>\n<p>The compiler turns it into machine language and then the compilation finishes and the C program gets executed. A macro in C is a set of program statements that is replaced by the value of the macro throughout the entire program.<\/p>\n<p>Let us learn about macros in C.<\/p>\n<h3>What is a Macro in C?<\/h3>\n<p>You can say that macro is a piece of code that is replaced by the value of the macro throughout the program. You can define a macro with <strong>#define<\/strong> directive. At the end of defining a macro, you don\u2019t have to put a semicolon(;) to terminate it.<\/p>\n<h3>Types of Macros in C<\/h3>\n<p>There are 2 types of macros present in C such as:-<\/p>\n<ul>\n<li>Object-like Macros.<\/li>\n<li>Function-like Macros.<\/li>\n<\/ul>\n<h4>1. Object-like Macros in C<\/h4>\n<p>It is a simple type of macro. In this object-like macro, the macro will be replaced by it\u2019s value. Object-like macros mainly used to represent numeric constants.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#define PI 3.14 \n<\/pre>\n<p><strong>Example:- Object-like Macros<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#define SIDE 4\nint main() {\n  int area;\n  area = SIDE*SIDE;\n  printf(\"TechVidvan Tutorial: Object Like Macros!\\n\");\n  printf(\"Area is: %d\",area);\n  return 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: Object Like Macros!<br \/>\nArea is: 16<\/div>\n<h4>2. Function-like Macros in C<\/h4>\n<p>In C, function-like macros are much similar to a function call. In this type of macro, you can define a function with arguments passed into it.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#define AREA(a) (a*a)<\/pre>\n<p><strong>Example:- Function-like Macros<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#define AREA(s) (s * s) \/\/ macro with argument\nint main()\n{\nint s1 = 10, area_of_square;\narea_of_square = AREA(s1);\nprintf(\"TechVidvan Tutorial: Macros with arguments!\\n\");\nprintf(\"Area of square is: %d\", area_of_square);\nreturn 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: Macros with arguments!<br \/>\nArea of square is: 100<\/div>\n<p>In the above example, the compiler finds the name of the macro <strong>(AREA(a))<\/strong> and replaces it with the statement <strong>(a*a).<\/strong><\/p>\n<h3>Predefined Macros in C<\/h3>\n<p>There are some predefined macros present in C. And it cannot be modified. Below are some predefined macros.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>MACRO\u00a0<\/b><\/td>\n<td><b>What it does<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">__DATE__<\/span><\/td>\n<td><span style=\"font-weight: 400\">Current date as MMM DD YYYY format.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">__TIME__<\/span><\/td>\n<td><span style=\"font-weight: 400\">Current time as HH:MM:SS format.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">__FILE__<\/span><\/td>\n<td><span style=\"font-weight: 400\">Contains current filename.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">__LINE__\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Contains current line number.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">__STDC__<\/span><\/td>\n<td><span style=\"font-weight: 400\">Defined as 1 when the compiler compiles.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example:- Predefined Macros<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\nint main() {\nprintf(\"TechVidvan Tutorial: Predefined Macros!\\n\\n\");\nchar filename[] = __FILE__;\nchar date[] = __DATE__;\nchar time[] = __TIME__;\nint line = __LINE__;\nint ansi = __STDC__;\nprintf(\"File name is: %s\\n\", filename);\nprintf(\"Date is: %s\\n\", date);\nprintf(\"Now time is: %s\\n\", time);\nprintf(\"Current line number: %d\\n\", line);\nprintf(\"Compilation Success: %d\\n\", ansi);\n}<\/pre>\n<p>The above code is saved in a file named <strong>HelloWorld.c.<\/strong><br \/>\n<strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">\n<p>TechVidvan Tutorial: Predefined Macros!<\/p>\n<p>File name is: main.c<br \/>\nDate is: Jun 24 2021<br \/>\nNow time is: 09:47:33<br \/>\nCurrent line number: 15<br \/>\nCompilation Success: 1<\/p>\n<\/div>\n<p><strong>Before using Macros, you have to be careful on following points:-<\/strong><\/p>\n<p>1. In C, when we define a macro then it is replaced by the value of that macro. And it will be set for the entire program. With <strong>#define<\/strong> directive, you can define a macro.<\/p>\n<p><strong>For Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;stdio.h&gt;\n#define A 24\nint main()\n{\nprintf(\"Value of A is %d\", A);\nreturn 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Value of A is 24<\/div>\n<p>2. You can also write multiple lines during defining the macro. You can do that with \u201c\\\u201d. It is known as a Continuation operator in Macro.<\/p>\n<p><strong>For Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#define  message_of_the_day(i, j)  \\\n   printf(#i \" and \" #j \": TechVidvan Tutorial: Macro Continuation!\")<\/pre>\n<p>You can also use macro as a function. And you can pass arguments to it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#define ADD(x,y) x+y\nint main()\n{\nprintf(\"%d\", ADD(3,2));\nreturn 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">5<\/div>\n<p>3. In C, you can also convert a macro parameter into a string. You have to use Stringize operator(#) to do so.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#define statement(i)  \\\n  printf(#i \": Macro Stringize!\\n\")\n \nint main(void) {\n   statement(TechVidvan Tutorial);\n   return 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: Macro Stringize!<\/div>\n<p>While defining, you can also use conditional statements such as if-else directives. This is known as Conditional Compilation.<\/p>\n<h3>Why to use Macros in C?<\/h3>\n<ul>\n<li>It becomes handy when you use it for anything magic number or string related.<\/li>\n<li>You can use macros to create automatic loop unrolling.<\/li>\n<li>With macros, you can do some things which you cannot do with functions like Token Pasting.<\/li>\n<li>You can use a macro for writing debugging messages also.<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In this tutorial, we learnt about macros in C. We also discussed the types of macros in C. You can define a macro with #define directive. In C, A macro is any constant value or variable with its value.<\/p>\n<p>And the macro name will get replaced by its value in the entire program. Macros help in writing less code and also in saving time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C programming offers various types of functions and methods to the programmers. It helps in turning the large and complex codes into simple and easy. In C, the program code is first compiled and&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":82926,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3510],"tags":[3731,3847],"class_list":["post-82293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-macros-in-c","tag-types-of-c-macros"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Macros in C - Types and Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about macros in C. See the types of macros in C and how yo define Macro in C. Check the reasons to use C Macros.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Macros in C - Types and Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about macros in C. See the types of macros in C and how yo define Macro in C. Check the reasons to use C Macros.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-29T03:30:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Macros-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Macros in C - Types and Examples - TechVidvan","description":"Learn about macros in C. See the types of macros in C and how yo define Macro in C. Check the reasons to use C Macros.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Macros in C - Types and Examples - TechVidvan","og_description":"Learn about macros in C. See the types of macros in C and how yo define Macro in C. Check the reasons to use C Macros.","og_url":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-29T03:30:22+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Macros-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Macros in C &#8211; Types and Examples","datePublished":"2021-07-29T03:30:22+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/"},"wordCount":645,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Macros-in-C.jpg","keywords":["Macros in C","Types of C Macros"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/macros-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/","url":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/","name":"Macros in C - Types and Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Macros-in-C.jpg","datePublished":"2021-07-29T03:30:22+00:00","description":"Learn about macros in C. See the types of macros in C and how yo define Macro in C. Check the reasons to use C Macros.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/macros-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Macros-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Macros-in-C.jpg","width":1200,"height":628,"caption":"Macros in C"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/macros-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Macros in C &#8211; Types and Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=82293"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/82926"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=82293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=82293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=82293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}