Constants in C Language

Preview

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 two categories.

  • Numeric Constant
  • Character Constant/Non Numeric Constant

Numeric Constant

Numeric Constant is that which can contain digits in it. Numeric constant can be categorized into two types:

  • Integer Constant
  • Floating point Constant/Real Constant

1. Integer Constant

Integer Constants are the numeric constants which can contain numbers without decimal point.  There are three types of integer constants:-

  • 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

1014904467

Examples of invalid Decimal Integer Constants

33.6It can’t contain decimal point.
1,447It can’t contain comma in it.
3 343It can’t contain space in between.
23-44-22It can’t have hyphens or any other special character in between.

b. Octal

Octal Integer Constant can contain digits from 0 to 7. So base of octal integer constant is 8. Octal numbers always start with 0 in C.

Examples of valid Octal Integer Constants:

012306663

Examples of invalid Octal Integer Constants

245  It must start with 0.
0393  It can’t contain 9.

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

Base of hexadecimal integers constant is 16. Hexadecimal numbers always start with 0X in C.

Examples of valid Hexadecimal Integer Constants:

0X1230X6663

Examples of invalid Hexadecimal Integer Constants

245It must start with 0X
0X33.6It can’t contain decimal point
0X2HIt can’t contain H

2 Floating point Constant/Real Constant

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

Real constants can contain both negative as well as positive values. Floating point constants can be used to represent very small values and very large quantities. 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.  Digits before or after the decimal point are optional.

Examples of valid Floating Point Constants in standard form:

320.25-65.50+3.55..450

Examples of invalid Floating Point Constants in standard form:

225Decimal point is not present in this value.
134,233.5A floating point number can’t contain comma in it.

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.

The number in exponent form has two parts

  • Mantissa: The digits before the symbol E are known as mantissa.
  • Exponent: The digits after the symbol E are known as Exponent part. The exponent part can contain integers only.

Example

25.355676 E09

25.355676 х 109  can be represented as 12.33E09

135.325 х 102  can be represented as 135.325e02

Examples of valid Floating Point Constants in exponent form:

123.55E332.2e-4-2.25e3

Examples of invalid Floating Point Constants in exponent form:

255.5E5.5Exponent part can’t contain floating point value.
45E4Exponent part can’t contain space between E and 4

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

Single Character Constant is the constant which can contain an alphabet, digit, special character or space within the pair of single quotes (’). It should not contain more than one character within single quotes.

Examples of valid single character constant:

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

Examples of invalid single character constant:

‘A1’It contains more than one character.

Note:   “1 and ‘1’ are not same   because

  • 1 is integer constant whereas ‘1’ is a character constant.
  • 1 consumes 2 bytes whereas ‘1’ consumes 1 byte of memory

b. String Constant

String Constant may be 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 which is represented as ‘\0’.

Examples of valid string constant:

“Lovejot”“2012”“$5000”“lovejot@gmail.com”

Note:   “String constant “Lovejot” contains 8 characters” because

  • String “Lovejot” contains 7 characters.
  • Last character is null character ‘\0’.

Note: ‘A’ and “A” are different because

  • ‘A’ is a character constant and “A” is a string constant.
  • ‘A’ is internally stored as an integer (ASCII Code) whereas “A” is not stored internally as an integer.
  • ‘A’ contains one character whereas “A” contains two characters.

c.  Escape Sequence

Escape sequences are special character constants provided by C which are used to represent those characters which can’t be represented graphically.

Escape sequences start with backward slash \ followed by a character specifically meant for a particular purpose. Following are various escape sequences provided by C.

(i) \n (New Line):

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

Program to demonstrate the use of \n

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

Output:

Great
india
india
great

(ii) \t (Horizontal tab): 

It is used to leave a gap of 5 or 8 spaces depending upon settings.

Program to demonstrate the use of \t

#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.

Program to demonstrate the use of \a

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

Output:

India great

(iv) \b Backspace:

It deletes a character appearing before it.

Program to demonstrate the use of \b

#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.

Program to demonstrate the use of \r

#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 of printf().

Program to demonstrate the use of \\

#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.

Program to demonstrate the use of \’

#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.

Program to demonstrate the use of \”

#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.

Program to demonstrate the use of \?

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

Output:

What is this?

(x) \0 (Null Character):

It is represented as backward slash followed by zero(\0). It is used to terminate a string. Nothing after it will be shown if it appears within a string.

Program to demonstrate the use of \0

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

Output:

What is

 (xi) \x Hexadecimal Number:

It is used to represent a hexadecimal number in the output.

(xii) \O Octal Number:

It is used to represent an octal number in the output.

(xiii) \v Vertical Tab:

It is used to insert five lines between two strings.

(xiv) \f Form feed:

It is used to feed one blank page before starting the printing.

%% Percentage Symbol:

It is used to print % sign in the output.

Program to demonstrate the use of %%

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

Output:

28%

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