The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

C Constants

In C programming language, a constant is similar to the variable but constant holds only one value during the program execution. That means, once a value is asssigned to the constant, that value can't be changed during the program execution. Once the value is assigned to the constant, it is fixed throughout the program. A constant can be defined as follows...

A constant is a named memory location which holds only one value throughout the program execution.

In C programmig language, constant can be of any datatype like integer, floating point, character, string and double etc.,

Integer constants

An integer constant can be a decimal integer or octal integer or hexa decimal integer. A decimal integer value is specified as direct integer value whereas octal integer value is prefixed with 'o' and hexa decimal value is prefixed with 'OX'.
An integer constant can also be unsigned type of integer constant or long type of integer constant. Unsigned integer constant value is suffixed with 'u' and long integer constant value is suffixed with 'l' whereas unsigned long integer constant value is suffixed with 'ul'.

Example

125 -----> Decimal Integer Constant
O76 -----> Octal Integer Constant
OX3A -----> Hexa Decimal Integer Constant
50u -----> Unsigned Integer Constant
30l -----> Long Integer Constant
100ul -----> Unsigned Long Integer Constant

Floating Point constants

A floating point constant must contain both integer and decimal parts. Some times it may also contain exponent part. When a floating point constant is represented in exponent form, the value must be suffixed with 'e' or 'E'.

Example

The floating point value 3.14 is represented as 3E-14 in exponent form.

Character Constants

A character constant is a symbol enclosed in single quotation. A character constant has a maximum length of one character.

Example

'A'
'2'
'+'

In C programming language, there are some predefined character constants called escape sequences. Every escape sequence has its own special functionality and every escape sequence is prefixed with '\' symbol. These escape sequences are used in output function called 'printf()'.

String Constants

A string constant is a collection of characters, digits, special symbols and escape sequences that are enclosed in double quotations.

We define string constant in a single line as follows...
"This is btechsmartclass"

We can define string constant using multiple lines as follows...
" This\
is\
btechsmartclass "


We can also define string constant by separating it with white space as follows...
"This" "is" "btechsmartclass"

All the above three defines the same string constant.

Creating constants in C

In c programming language, constants can be created using two concepts...

  1. Using 'const' keyword
  2. Using '#define' preprocessor

Using 'const' keyword

We create a constant of any datatype using 'const' keyword. To create a constant, we prefix the variable declaration with 'const' keyword.
The general syntax for creating constant using 'const' keyword is as follows...

const datatype constantName ;

OR

const datatype constantName = value ;

Example

const int x = 10 ;

Here, 'x' is a integer constant with fixed value 10.

Example Program

#include<stdio.h>
#include<conio.h>
void main(){

   int i = 9 ;
   const int x = 10 ;
      
   i = 15 ;
   x = 100 ; // creates an error
   printf("i = %d\nx = %d", i, x ) ;

}

The above program gives an error because we are trying to change the constant variable value (x = 100).

Using '#define' preprocessor

We can also create constants using '#define' preprocessor directive. When we create constant using this preprocessor directive it must be defined at the beginning of the program (because all the preprocessor directives must be written before the gloabal declaration).
We use the following syntax to create constant using '#define' preprocessor directive...

#define CONSTANTNAME value

Example

#define PI 3.14

Here, PI is a constant with value 3.14

Example Program

#include<stdio.h>
#include<conio.h>

#defien  PI  3.14

void main(){
   int r, area ;
   
   printf("Please enter the radius of circle : ") ;
   scanf("%d", &r) ;
   
   area = PI * (r * r) ;
   
   printf("Area of the circle = %d", area) ;
}

Place your ad here
Place your ad here