Constants in C Language

Preview

Constants in C Language

It is a quantity whose value can’t be changed in a program while executing . Constant is of following types.

1. Numeric Constant

It can contain digits in It is of two types:

  • Integer Constant
  • Real Constant



1. Integer Constant

Integer Constants can contain numbers without a 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

10 908 4776

b. Octal

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

Examples

0357 01754

c.Hexadecimal

It can contain digits from 0 to 9 and alphabets from A to F. where

A is 10, B is 11, C is 12, D is 13, E is 14 and F is 15.

It always start with 0X .

Examples of valid Hexadecimal Integer Constants:

0X39A 0XF630



2 Floating point Constant

Floating point constants can contain numbers with decimal point. They are 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:

5.5 -17.75 +182.8 57.

b.  Exponent Form

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

Example

375.355 х 109  can be represented as 3.75355E09



3 Character Constant

It can  contain any symbol including alphabets, special symbols , digits and blank spaces. Character constant is of three types:

  • Single Character Constant
  • String Constant
  • Escape Sequence

a. Single Character Constant

Single Character Constant contains alphabet, special character , digit and/or space within group of single quotes (’). 

Examples of valid single character constant:

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

 



b. String Constant

It is a group of characters within the pair of  double quotes (“). Alphabets, special characters, digits, and blank spaces can be used in a string constant. It always terminates with a special character called Null character (‘\0’).

Examples of valid string constant:

“LovejotBhardwaj” “2012” “$2500 [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>
int main()
{
printf(“Hello”);
printf(“\nWorld “);
return 0;
}

Output:

Hello
World

(ii) \t (Horizontal tab): 

It is used to leave a gap of 5 spaces.

#include<stdio.h>
int main()
{
printf(“\nHello \t World”);
}

Output:

Hello     World

(iii) \a (Audible Bell):

Sound is produced.

#include<stdio.h>
int main()
{
printf(“Hello\ a World”);return 0;
}

Output:

Hello World

(iv) \b Backspace:

It deletes a character before it.

#include<stdio.h>
int main()
{
printf(“\nHello\b World”);
return 0;
}

Output:

Hello World

(v) \r (Carriage Return):

If we use  \r in a string, all the characters before it will not be shown in output.

#include<stdio.h>
int main()
{
printf(“\nIndia is\r great”);
return 0;
}

Output:

great

 (vi) \\ (Backward Slash \ ):

Shows back slash in the output.

#include<stdio.h>
int main()
{
printf(“\\Hello\\”);
return 0;
}

Output:

\Hello\

(vii) \’ (Single quotes):

Shows single quotes(‘) in the output.

#include<stdio.h>
int main()
{
printf(“\’Hello\’ ”);
return 0;
}

Output:

‘Hello’

(viii) \” (Double Quotes):

Shows double quotes ( “ ) in the output.

#include<stdio.h>
int main()
{
printf(“\”Hello\”“);
return 0;
}

Output:

“Hello”

(ix) \? Question mark:

Shows Question Marks (?) in the output.

#include<stdio.h>
int main()
{
printf(“Hello\?”);
return 0;
}

Output:

Hello?

(x) \0 (Null Character):

It terminates a string value. 

#include<stdio.h>
int main()
{
printf(“Hello \0 Bye”);
return 0;
}

Output:

Hello

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