Constants in C Language

Constants are very important part of any programming language. Constant is a value that can not be changed and remains fixed. constants are also known as Literals. in C Language constants can be classified in different categories like integer Constant, float Constant, character constant or a string.
Constants in C Language

Integer Constants in C Language 

An Integer literal can not have a decimal point. Comma or Blank space is not allowed. integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal. integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.  

Examples : 

146         /* decimal integer literal in c language */

0564       /* octal integer literal in c language */

0x2c       /* hexadecimal integer literal in c language */

89u        /* unsigned int integer literal in c language */
128l        /* long integer literal in c language */
33ul       /* unsigned long integer literal in c language */

Real Constants or Floating-point Literals in C Language

Real constants are also known as Floating Point constants and Can be written in two forms - Fractional form and Exponential form. If the value of a real constant is either too small or too large then real constant can be expressed in exponential form.0.0000794 can be written in 7.94e-5. Part before 'e' is called mantissa and part after 'e' is called exponent.
Examples:
37.89
4.98e-7
+6.74e+6
-158.29

Character Constants in C language

A Character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. character literal can be a plain character (e.g., 'R'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').
Examples :
'R'
'6'
'+'
'$'
There are certain characters in C that represent special meaning when preceded by a backslash for example, newline (\n) or tab (\t)

Escape sequence

To Format Output we want to use newline, tab, quotation mark then escape sequences are used.
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\a Alert
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\0 Null character

String Literals

Strings are enclosed in double quotes. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters. You can break a long line into multiple lines using string literals and separating them using white spaces.
Example:
"Technorials"
"C programming language"

SHARE THIS
Previous Post
Next Post