Please enable Javascript to correctly display the contents on Dot Net Tricks!

Constants and Literals

Posted On : 03 Jun 2016
Updated On : 03 Jun 2016
Total Views : 2038

A constant is a value or an identifier whose value cannot be change during program execution. In other words, you can say variable is fixed value.

For example: 1, 2.5, "C programming is easy" etc.

Value of variable are called literals.

 const double PI = 3.14

Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program.

Integer constants

A integer constant is a numeric constant (associated with number) without any fractional or exponential part.

There are mainly three types of integer constants available in C programming

  1. decimal constant(base 10) : 0, -7, 8 etc.

  2. octal constant(base 8) : 031, 066, 044 etc.

  3. hexadecimal constant(base 16) - 0x4f, 0x5a, 0x321 etc.

In C programming, octal constant starts with a 0 and hexadecimal constant starts with a 0x.

Floating-point constants

A floating point constant is a numeric constant that has either a fractional form or an exponent form. For example

Character constants

A character constant use in single quotes. For example: 'a', 'l', 'm', 'F'

Escape Sequences

Sometimes, it is necessary to use characters which cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. In order to use these characters, escape sequence is used.

For example: \n is used for newline. The backslash ( \ ) causes "escape" from the normal way the characters are interpreted by the compiler.

List of Backslash Character Constants in C
Audible Alert
Back Space
Form Feed
New Line
Carriage Return
Horizontal Tab
Vertical Tab
Single Quote
Double Quote
Question Mark

String constants

String constants are the constants which are enclosed in a pair of double-quote marks

String constant
Null String Constant
" "
String constant of Five white space
Null String Constant
"Coding is Rhyme\n"
prints string with newline

Enumeration constants

Keyword enum is used to define enumeration types that means user can define their own data type. For example:

enum subject {C, Html, Java, Sql};

Here, subject is name of variable and C, Html, Java and Sql are the values (enumeration) constants having value 0, 1, 2 and 3 accordingly.