{"id":82235,"date":"2021-07-23T09:00:02","date_gmt":"2021-07-23T03:30:02","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=82235"},"modified":"2021-07-23T09:00:02","modified_gmt":"2021-07-23T03:30:02","slug":"constants-literals-in-c","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/","title":{"rendered":"Constants in C | Literals in C"},"content":{"rendered":"<p>The C programming language offers many features and functionalities to the programmers. It has a ton of built-in functions that will help in simple and efficient coding. In C, you also use a fixed value that won\u2019t be altered during the execution of the program.<\/p>\n<p>These fixed values will help you in many ways in your programming. A programmer should learn the C programming language because it has many methods and functionalities to offer.<\/p>\n<h3>Literals in C<\/h3>\n<p>The main purpose of literals is to represent fixed values. These fixed values cannot be changed during the execution of the program. Also, you cannot change the values after their definition. You can use the literals directly in your program code.<\/p>\n<p>For example:- <strong>1, 3.2, \u2018T\u2019<\/strong> etc.<\/p>\n<h3>Types of\u00a0 C Literals<\/h3>\n<p>Below are the types of literals present in C:-<\/p>\n<ul>\n<li>Integer literals<\/li>\n<li>Floating literals<\/li>\n<li>Character literals<\/li>\n<li>String literals<\/li>\n<\/ul>\n<h4>1. Integer Literals in C:-<\/h4>\n<p>There is no fractional or exponential part in integer literals. There are 3 types of integer literals present in C:-<\/p>\n<ul>\n<li>Decimal<\/li>\n<li>Hexadecimal<\/li>\n<li>Octal<\/li>\n<\/ul>\n<h4>Prefixes:-<\/h4>\n<p>It is used for indicating the base of the number system in which the data is stored.<\/p>\n<ul>\n<li><strong>0x or 0X :- hexadecimal(base-16)<\/strong><\/li>\n<li><strong>0 :- Octal(base-8)<\/strong><\/li>\n<li><strong>No prefix :- Decimal(base-10)<\/strong><\/li>\n<\/ul>\n<h4>Suffixes:-<\/h4>\n<p>It is used for indicating the type of the value which is stored in an integer constant. You can use U(unsigned) and L(long).<\/p>\n<p><strong>Example:- Integer Literals<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Decimal:- 0,6,58 etc\nHexadecimal:- 0x6f, 0x4a, 0x675 etc\nOctal:- 033, 077, 015 etc\n26l     \t\/\/ long\n56ul    \t\/\/ unsigned long\n<\/pre>\n<h4>2. Floating Literals in C<\/h4>\n<p>A floating point consists of 4 things:-<\/p>\n<ul>\n<li>An Integer part<\/li>\n<li>Decimal point<\/li>\n<li>Fractional part<\/li>\n<li>Exponential Part<\/li>\n<\/ul>\n<p>A floating point literal can be either in fractional or in exponential form.<\/p>\n<p><strong>Example:- Floating Literals<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">-89.2\n0.0001258\n-0.12E-5<\/pre>\n<p><strong>NOTE:-<\/strong> E-5 = 105<\/p>\n<h4>3. Character Literal in C<\/h4>\n<p>The main purpose of a character literal is to store a single character which is enclosed within single quotes(\u2018\u2019). You can also store multiple characters with the help of an array of character types.<\/p>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">char s = 'T';<\/pre>\n<p><strong>Example:- Character Literal<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">'A';\n'h';\n'(';<\/pre>\n<h5>Escape Sequences in C<\/h5>\n<p>There are some special types of characters like \u2018\\n\u2019 and \u2018\\t\u2019 present in C. These are known as escape sequences. It has special meaning in C programming. For example, \u2018\\n\u2019 means newline.<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Escape Sequences<\/strong><\/td>\n<td><strong>What it does<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\b<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for backspace.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\f<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for form feed.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\n<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for a new line.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\r<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for a return.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\t<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for the horizontal tab.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\v<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for the vertical tab.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\\\<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for backslash.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\\u2019<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for a single quotation mark.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\\u201d<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for a double quotation mark.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\?<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for a question mark.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for Null character.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The compiler will handle the characters differently when it executes the <strong>backslash(\\).<\/strong><\/p>\n<h4>4. String Laterals in C<\/h4>\n<p>You can use string laterals to store multiple characters together. It is enclosed within double quotes(\u201c\u201d).<\/p>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">char s[]=\"TechVidvan\";<\/pre>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n \nint main() {\n  char s[] = \"TechVidvan Tutorial: String Lateral!\";\n  printf(\"%s\", s);\n  return 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><br \/>\nTechVidvan Tutorial: String Lateral!<\/p>\n<h3>Constants in C<\/h3>\n<p>In C, you can use const keyword to define a variable as constant. After defining it as constant, you cannot change its value.<\/p>\n<h4>Defining constants in C:-<\/h4>\n<p>In C, there are two ways to define a constant such as:-<\/p>\n<ul>\n<li>By Using <strong>const<\/strong> keyword<\/li>\n<li>By Using #define preprocessor directive<\/li>\n<\/ul>\n<h4>1. Using const keyword in C<\/h4>\n<p>You can use the <strong>const<\/strong> keyword to define a constant variable in C.<\/p>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const dataType variable_name = value;<\/pre>\n<p><strong>Example:- Using const keyword<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n \nint main() {\n  const int val = 4;\n  val = 5; \/\/ changing the value 4 to 5!\n  printf(\"%d\", val);\n  return 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">main.c: In function 'main':\nmain.c:13:7: error: assignment of read-only variable 'val'\n   val = 5; \/\/ changing the value 4 to 5!<\/pre>\n<p>From the above output, you can clearly understand that you cannot change the value of a constant variable.<\/p>\n<h4>2. Using #define preprocessor directive in C<\/h4>\n<p>It is used for creating and defining macros in C. With the help of macros, you can declare a constant value which you can use throughout the program.<\/p>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#define name_of_the_constant value<\/pre>\n<p><strong>Example:- #define directive<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n \n#define SIDE 7\nint main() {\n  int area;\n  area = SIDE*SIDE;\n  printf(\"TechVidvan Tutorial: #define directive!\\n\\n\");\n  printf(\"Area of square: %d\", area);\n  return 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><br \/>\nTechVidvan Tutorial: #define directive!<\/p>\n<p>Area of square: 49<\/p>\n<h3>Summary<\/h3>\n<p>In this tutorial, we learnt about constants and literals in C. We also discussed the types of literals. We discussed 2 ways to define constants in C. Constants and Literals help programmers in simple and easy coding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C programming language offers many features and functionalities to the programmers. It has a ton of built-in functions that will help in simple and efficient coding. In C, you also use a fixed&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":82900,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3510],"tags":[3768,3769,3846],"class_list":["post-82235","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-constants-in-c","tag-literals-in-c","tag-types-of-literals-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Constants in C | Literals in C - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about constants &amp; literals in C. These help programmers in simple &amp; easy coding. See types of literals &amp; ways to define constants 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:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constants in C | Literals in C - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about constants &amp; literals in C. These help programmers in simple &amp; easy coding. See types of literals &amp; ways to define constants in C\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/constants-literals-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-23T03:30:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-in-C-1.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":"Constants in C | Literals in C - TechVidvan","description":"Learn about constants & literals in C. These help programmers in simple & easy coding. See types of literals & ways to define constants 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:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Constants in C | Literals in C - TechVidvan","og_description":"Learn about constants & literals in C. These help programmers in simple & easy coding. See types of literals & ways to define constants in C","og_url":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-23T03:30:02+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-in-C-1.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\/constants-literals-in-c\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Constants in C | Literals in C","datePublished":"2021-07-23T03:30:02+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/"},"wordCount":651,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-in-C-1.jpg","keywords":["Constants in C++","Literals in C","Types of Literals in C"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/","url":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/","name":"Constants in C | Literals in C - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-in-C-1.jpg","datePublished":"2021-07-23T03:30:02+00:00","description":"Learn about constants & literals in C. These help programmers in simple & easy coding. See types of literals & ways to define constants in C","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-in-C-1.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-in-C-1.jpg","width":1200,"height":628,"caption":"Constants and Literals in C"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/constants-literals-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Constants in C | Literals in C"}]},{"@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\/82235","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=82235"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82235\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/82900"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=82235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=82235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=82235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}