Constants


Constants in C refer to fixed values that do not change during the execution of a program. C supports several types of constants as shown in previous tutorials.


Integer Constants

Refers to the order of the constant digits of an integer. There are three types of decimal integers, octal integers, and hexadecimal integers. Decimal integers consist of a set of digits, 0 through 9, preceded by an optional - or + sign. An effective example of a decimal integer constant is:

123   -321   0   654321   +78

C constants

The constant of an octal integer sets any number from 0 to 7 with a top 0.

A sequence of digits before 0x or 0X is considered a hexadecimal integer. They can include the alphabet A through F or complete f. The letter through F represents a number from 10 to 15.

Sample Program

Program
#include <stdio.h>
int main() 
{
  printf("Integer values are \n");
  printf("%d %d",1000,1000+10);
  return 0;
}

Real Constants

The number of integers is insufficient to represent a continuous quantity, such as distance, height, temperature, price, etc. These quantities are represented by fractional numbers such as 18.532. These national numbers are called real constants. Another example of a real constant is:

0.0032   -9.312   231.98   +21.90

These numbers appear in decimal notation after a whole number of decimal points and fractions. Digits before the decimal point or digits after the decimal point can be omitted.

219.   .45   -.12   +.9

Examples of Numeric Constants

Constant Valid? Remarks
789798L Yes Represents long integer
10,000 No Comma is not allowed
+3.6E2 Yes (ANSI C supports unary plus)
3.6E-2 Yes
3.6e 3 No white space not allowed
1.7E+3.5 No Exponent must be an integer
$288 No $ symbol is not permitted
0X9C Yes Hexadecimal integer allowed

Single Character Constants

A single character constant consists of a single character bound in a pair of constant single quotation marks. Examples of character constants are:

'9'   'K'   ';'   ' '

Note that the character constant '9' is not the same as the number 9. The last constant is a blank space.

The character contains integer values known as ASCII values. For example,

printf("%d",'b');
// output 98

printf("%c",'b');
// output b

String Constants

A string constant is a double-quote sequence of characters. Characters can be letters, numbers, special characters, and spaces. Examples are:

"World"   "2000"   "Hello World"   "A"

Note that a character constant (e.g., 'a') is not equivalent to a single-character string constant (e.g., "A"). Furthermore, a single-character string constant does not have an integer value equivalent, but a character constant has an integer value. Character strings are often used in programs to create meaningful programs.


Backslash Character Constants

C supports some special backslash character constants that are used in output methods. For example, the symbol '\b' stands for backspace. All backslash character constants are given in the table below:

Constant Meaning
'\a' audible alert
'\b' back space
'\f' form feed
'\n' new line
'\r' carriage return
'\t' horizontal tab
'\v' vertical tab
'\" single quote
'\"' double quote
'\?' question mark
'\\' backslash
'10' null