Constants in C Language

Constants in C Language

A Constant may be defined as a quantity whose value can’t be changed during the execution of a program. Constant can be divided into following categories.

1. Numeric Constant

Numeric Constant can contain digits in It can be categorized into two types:

  • Integer Constant
  • Floating point Constant/Real Constant



1. Integer Constant

Integer Constants can contain numbers without decimal point.  They are of three types:

  • Decimal
  • Octal
  • Hexadecimal

a. Decimal

Decimal Integer Constant can contain digits from 0 to 9. So base of decimal integer constant is 10.

Examples of valid Decimal Integer Constants

101 490 4467

b. Octal

Octal Integer Constant can contain digits from 0 to 7. Octal numbers always start with 0 in C.

Examples of valid Octal Integer Constants:

0123 06663

c.Hexadecimal

Hexadecimal Integer Constant can contain digits from 0 to 9 and alphabets from A to F. where

A = 10

B = 11

C = 12

D = 13

E = 14

F = 15

Hexadecimal numbers always start with 0X in C.

Examples of valid Hexadecimal Integer Constants:

0X123 0X6663



2 Floating point Constant

Floating point constants/Real constants are those constants which can contain numbers with decimal point.

Floating point constants can be categorized into two types

  • Standard Form
  • Exponent Form

a. Standard Form

In standard form, Floating point constant can contain a whole number followed by a decimal point further followed by fractional part.

Examples of valid Floating Point Constants in standard form:

320.25 -65.50 +3.5 5. .450

b.  Exponent Form

Exponent form of floating point constant is also called scientific notation. In this form, a floating point number is represented in terms of power of 10. The power of 10 is represented by E or e where E represents exponent.

Example

25.355676 х 109  can be represented as 12.33E09



3 Character Constant

Character Constants are those constants which can contain any symbol which can include alphabets, digits, special symbols as well as blank spaces. Character constant can be categorized into two types:

  • Single Character Constant
  • String Constant
  • Escape Sequence

a. Single Character Constant

It can contain an alphabet, digit, special character or space within the pair of single quotes (’). It should not contain more than one character in it .

Examples of valid single character constant:

‘A’ ‘1’ ‘#’ ‘:’

 



b. String Constant

It is defined as a sequence of one or more characters enclosed in double quotes (“). We can use alphabets, digits, special characters as well as blank spaces in a string constant.

String constant is always terminated with a special character known as null character (‘\0’).

Examples of valid string constant:

“Lovejot” “2012” “$5000” [email protected]

 



c.  Escape Sequence

Escape sequences start with backward slash \ followed by a character meant for a particular purpose. Various escape sequences are:

(i) \n (New Line):

It is used to insert one blank line where it appears.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Great”);
printf(“\nIndia “);
getch();
}

Output:

Great
india

(ii) \t (Horizontal tab): 

It is used to leave a gap of 5 spaces.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\nIndia \t great”);
getch();
}

Output:

India       great

(iii) \a (Audible Bell):

A small beep(sound) is produced.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\nIndia \a great”);
getch();
}

Output:

India great

(iv) \b Backspace:

It deletes a character before it.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\nIndia is\b great”);
getch();
}

Output:

India i great

(v) \r (Carriage Return):

If \r is used in between a string, all the characters before it will not be shown. If \r is used in the end of string, the cursor will be placed at the very first character of the string in the output.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\nIndia is\r great”);
getch();
}

Output:

great

 (vi) \\ (Backward Slash \ ):

It is used to show back slash in the output.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\\India is great\\”);
getch();
}

Output:

\India is great\

(vii) \’ (Single quotes):

It shows single quotes(‘) in the output.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\’India is great\’”);
getch();
}

Output:

‘India is great’

(viii) \” (Double Quotes):

It shows double quotes ( “ ) in the output.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\”India is great\”“);
getch();
}

Output:

“India is great”

(ix) \? Question mark:

It shows Question Marks (?) in the output.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“What is this\?”);
getch();
}

Output:

What is this?

(x) \0 (Null Character):

It is used to terminate a string. Nothing after it will be shown.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“What is \0 this”);
getch();
}

Output:

What is

Best Books of C

Lesson tags: constants in c, escape sequence in c, exponent form, integer literal in c, literals in c, string in c
Back to: C Programming Language