{"id":3603,"date":"2014-03-29T06:42:12","date_gmt":"2014-03-29T06:42:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/why-we-are-avoid-magic-numbers-or-constants-from-program-and-use-macro-closed-collection-of-common-programming-errors\/"},"modified":"2014-03-29T06:42:12","modified_gmt":"2014-03-29T06:42:12","slug":"why-we-are-avoid-magic-numbers-or-constants-from-program-and-use-macro-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/why-we-are-avoid-magic-numbers-or-constants-from-program-and-use-macro-closed-collection-of-common-programming-errors\/","title":{"rendered":"Why we are avoid Magic Numbers or Constants from Program and use macro? [closed]-Collection of common programming errors"},"content":{"rendered":"<p>When compiling a C program, the program is first processed by the C preprocessor. It process all the <code>#foo<\/code> stuff in your program and replaces macros. By the time the <em>actual<\/em> compiler sees the code, your macros have been replaced with values.<\/p>\n<p>So if you feed your second code snippet to the compiler, the <em>actual<\/em> C compiler will see the exact same thing as in your first code snippet. This means that using a macro instead of a value <em>does not<\/em> slow down your program.<\/p>\n<p>The idea about using macros (or constants) is to have names for your numbers that may make sense to a human.<\/p>\n<p>Consider your program supports three bicycles (or whatever, doesn&#8217;t matter). You would need to write <code>3<\/code> in a lot of places (like you wrote 255 in your <code>for<\/code> loop). But a month later, you might not remember the <em>meaning<\/em> of a 3 any more when you see it. If you instead have:<\/p>\n<pre><code>#define MAX_BICYCLES 3\n<\/code><\/pre>\n<p>and then use <code>MAX_BICYCLES<\/code> everywhere the intention becomes clearer. Better yet, once your app should support, say, 5 bicycles it might be as easy as changing that one macro. You do not have to remember in which places to change a <code>3<\/code> to <code>5<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When compiling a C program, the program is first processed by the C preprocessor. It process all the #foo stuff in your program and replaces macros. By the time the actual compiler sees the code, your macros have been replaced with values. So if you feed your second code snippet to the compiler, the actual [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3603","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3603"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3603\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}