{"id":19807,"date":"2020-07-20T22:44:06","date_gmt":"2020-07-21T05:44:06","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19807"},"modified":"2020-12-29T11:05:18","modified_gmt":"2020-12-29T19:05:18","slug":"c-boolean","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/c-boolean\/","title":{"rendered":"C \u2013 Booleans"},"content":{"rendered":"\n<p><em>Originally, booleans did not have built-in support in C. With the advent of the C99 standard about ten years ago, C added a _Bool type. In this article, we\u2019ll go over several ways that you can represent boolean values in C.&nbsp;<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">&lt;stdheader.h&gt;<\/h2>\n\n\n\n<p>Boolean logic can be implemented if you add the <code>&lt;stdheader.h&gt;<\/code> header at the top of your file.&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>#include &lt;stdio.h&gt;\n#include &lt;stdbool.h&gt;\n \nint main() {\n bool value = 1;\n if(value){\n   printf(&quot;value is true.&quot;);\n }\n else{\n   printf(&quot;value is false.&quot;);\n }\n return 0;\n \n}<\/pre><\/div>\n\n\n\n<p>Using a header in C is analogous to creating a file of constants or functions in JavaScript that can be used across your project. The <code>&lt;stdbool.h&gt;<\/code> header contains macros and constants for boolean values that can be used in your file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">typedef<\/h2>\n\n\n\n<p>A <code>typedef<\/code> is a way to reassign a type to a new name:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>typedef int bool;<\/pre><\/div>\n\n\n\n<p>Here the <code>typedef<\/code> takes int and reassigns it to bool. Now we can use bool in our function:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>#include &lt;stdio.h&gt;\n \ntypedef int bool;\nenum { false, true };\n \n \nint main() {\n bool value = 3;\n if(value){\n   printf(&quot;value is true.&quot;);\n \n }\n else{\n   printf(&quot;value is false.&quot;);\n }\n return 0;\n}<\/pre><\/div>\n\n\n\n<p><code>Typedef<\/code> defines types only. It is not used as a way to define constants or aliases. Enum defines the integral constants of true and false here. By default, enum values start at 0 and go up from there. If you\u2019d like you can switch the default, but you don\u2019t need to for this situation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">#define<\/h2>\n\n\n\n<p>The final way we will go over boolean logic is by using the #define keyword to create some boolean variables:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>#include &lt;stdio.h&gt;\n \ntypedef int bool;\n#define false 0\n#define true 1\n \nint main() {\n bool value = 3;\n if(value){\n   printf(&quot;value is true.&quot;);\n }\n else{\n   printf(&quot;value is false.&quot;);\n \n }\n return 0;\n \n}<\/pre><\/div>\n\n\n\n<p>As you can see, instead of using enum here, we are using #define to tell the compiler that false equates to 0 and true equates to 1 (which is in all honesty how the compiler sees these two values anyway).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we went over several ways to define bool values in C. Try your hand at learning something new in C that you already know how to do in another language!<\/p>\n","protected":false},"excerpt":{"rendered":"Originally, booleans did not have built-in support in C. With the advent of the C99 standard about ten years ago, C added a _Bool type. In this article, we\u2019ll go over several ways that you can represent boolean values in C.&nbsp; &lt;stdheader.h&gt; Boolean logic can be implemented if you add the &lt;stdheader.h&gt; header at the&hellip;","protected":false},"author":77,"featured_media":19808,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[18070],"tags":[],"class_list":{"0":"post-19807","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-software-engineering-skills"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Coding","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>C \u2013 Booleans a _Bool type | Career Karma<\/title>\n<meta name=\"description\" content=\"Depending upon the compiler used, C may or may not have built-in support for booleans. In this article, we\u2019ll go over the standard approaches to bools in C.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/c-boolean\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C \u2013 Booleans\" \/>\n<meta property=\"og:description\" content=\"Depending upon the compiler used, C may or may not have built-in support for booleans. In this article, we\u2019ll go over the standard approaches to bools in C.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/c-boolean\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-21T05:44:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-29T19:05:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/nikhil-mitra-OiUDGKHHuN0-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"678\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Christina Kopecky\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Christina Kopecky\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/c-boolean\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/c-boolean\/\"},\"author\":{\"name\":\"Christina Kopecky\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\"},\"headline\":\"C \u2013 Booleans\",\"datePublished\":\"2020-07-21T05:44:06+00:00\",\"dateModified\":\"2020-12-29T19:05:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/c-boolean\/\"},\"wordCount\":283,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/c-boolean\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/nikhil-mitra-OiUDGKHHuN0-unsplash.jpg\",\"articleSection\":[\"Software Engineering\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/c-boolean\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/c-boolean\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/c-boolean\/\",\"name\":\"C \u2013 Booleans a _Bool type | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/c-boolean\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/c-boolean\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/nikhil-mitra-OiUDGKHHuN0-unsplash.jpg\",\"datePublished\":\"2020-07-21T05:44:06+00:00\",\"dateModified\":\"2020-12-29T19:05:18+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\"},\"description\":\"Depending upon the compiler used, C may or may not have built-in support for booleans. In this article, we\u2019ll go over the standard approaches to bools in C.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/c-boolean\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/c-boolean\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/c-boolean\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/nikhil-mitra-OiUDGKHHuN0-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/nikhil-mitra-OiUDGKHHuN0-unsplash.jpg\",\"width\":1020,\"height\":678,\"caption\":\"Brown Wooden Letter C\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/c-boolean\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Coding\",\"item\":\"https:\/\/careerkarma.com\/blog\/code\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"C \u2013 Booleans\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\",\"name\":\"Christina Kopecky\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg\",\"caption\":\"Christina Kopecky\"},\"description\":\"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.\",\"sameAs\":[\"http:\/\/www.linkedin.com\/in\/cmvnk\"],\"url\":\"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"C \u2013 Booleans a _Bool type | Career Karma","description":"Depending upon the compiler used, C may or may not have built-in support for booleans. In this article, we\u2019ll go over the standard approaches to bools in C.","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:\/\/careerkarma.com\/blog\/c-boolean\/","og_locale":"en_US","og_type":"article","og_title":"C \u2013 Booleans","og_description":"Depending upon the compiler used, C may or may not have built-in support for booleans. In this article, we\u2019ll go over the standard approaches to bools in C.","og_url":"https:\/\/careerkarma.com\/blog\/c-boolean\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-07-21T05:44:06+00:00","article_modified_time":"2020-12-29T19:05:18+00:00","og_image":[{"width":1020,"height":678,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/nikhil-mitra-OiUDGKHHuN0-unsplash.jpg","type":"image\/jpeg"}],"author":"Christina Kopecky","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Christina Kopecky","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/c-boolean\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/c-boolean\/"},"author":{"name":"Christina Kopecky","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"headline":"C \u2013 Booleans","datePublished":"2020-07-21T05:44:06+00:00","dateModified":"2020-12-29T19:05:18+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/c-boolean\/"},"wordCount":283,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/c-boolean\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/nikhil-mitra-OiUDGKHHuN0-unsplash.jpg","articleSection":["Software Engineering"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/c-boolean\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/c-boolean\/","url":"https:\/\/careerkarma.com\/blog\/c-boolean\/","name":"C \u2013 Booleans a _Bool type | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/c-boolean\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/c-boolean\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/nikhil-mitra-OiUDGKHHuN0-unsplash.jpg","datePublished":"2020-07-21T05:44:06+00:00","dateModified":"2020-12-29T19:05:18+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"description":"Depending upon the compiler used, C may or may not have built-in support for booleans. In this article, we\u2019ll go over the standard approaches to bools in C.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/c-boolean\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/c-boolean\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/c-boolean\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/nikhil-mitra-OiUDGKHHuN0-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/nikhil-mitra-OiUDGKHHuN0-unsplash.jpg","width":1020,"height":678,"caption":"Brown Wooden Letter C"},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/c-boolean\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Coding","item":"https:\/\/careerkarma.com\/blog\/code\/"},{"@type":"ListItem","position":3,"name":"C \u2013 Booleans"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e","name":"Christina Kopecky","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","caption":"Christina Kopecky"},"description":"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.","sameAs":["http:\/\/www.linkedin.com\/in\/cmvnk"],"url":"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=19807"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19807\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/19808"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}