Constants in C

Last Updated Nov 1, 2015, 07:00:14 PM





Constants in computer programming refer to a fixed value that can not be changed during program execution. These fixed values are called as Literals

In C programming, constants can be of any type. For example, a constant can be of integer type, float type, a character type, or a string type. In general, both variables and constants are same but technically the only difference is, constants can be changed during the program execution while the variables can be changed at any time.

Syntax

Different types of Constants or Literals in C

There are four different types Constants such as:

  • Integer Constants
  • Floating-point Constants
  • Character Constants
  • String Constants

Integer Constants

Both decimal, octal, and hexademical numbers are considered as integer constants. Integer constants must have atleast one digit.

Example
  • Integer constats must not contain any decimal point
  • It can be either positive or negative value
  • Integer literals does not accept any commas or blank spaces within the integer
  • The acceptable range values for integer literals are -32768 to 32768

Floating-point Constants

Floating-point constants can be mixed of both integer and decimal values. It also accepts fractional part and exponential part

Example

Character Constants

Character constant is a one single letter or symbol enclosed within single quotes. The lenght of character constant is only one character or letter

The character constant can be a single letter like 'x' or an escape sequence like ' \n' or a universal character like '\u02C0'

Example Try It Now

Other Escape Sequence Characters

Backslash_character Meaning
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\” Double quote
\’ Single quote
\\ Backslash
\v Vertical tab
\a Alert or bell
\? Question mark
\N Octal constant (N is an octal constant)
\XN Hexadecimal constant (N – hex.dcml cnst)
String Constants

String constants are similar to character constants but with double quotes. String characters takes multiple characters like a complete word or a phrase.

String literals can be seperated with whitespace if we want to put them in seperate or multiple lines.

Example

What is Const Keyword in C