C Constants

C Constants

In C language, a constant is similar to the variable but constant holds only one value at the time of program execution. That means, once a value is taken to the constant, than it can’t be changed during the program execution time. Once the value is assigned to the constant, it is fixed throughout the program. Therefore a constant can be defined as, it is a named memory location that can holds only one value throughout the execution of program.

In C programmig language, constant can be of any data type 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. Sometimes it may also contain exponent part. When a floating point constant is represented in exponential  form, the value must be end 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.

* A character symbol is enclosed in single quotation.

* A character constant has a maximum length of one character.

            Examples:- ‘A’, ‘2’, ‘+’ , ‘z’  etc.

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 that is 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 bcaguru”

We can define string constant using multiple lines as follows…
” This\
is\
bcaguru “


We can also define string constant by separating it with white space as follows…
“This” “is” “bcaguru”

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

          1)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(“enter the radius of circle :- “) ;

   scanf(“%d”, &r) ;

   area = PI * (r * r) ;

   printf(“Area of the circle = %d”, area) ;

}

Leave a Reply

Your email address will not be published. Required fields are marked *

Contribute
close slider
present

Contribute and Win Exciting Prizes & Gifts



whatsapp

You can also send file via Whatsapp

+91 9155653926

If you have any year ,any subject,any semester question paper and other study materials please send us

अगर आपके पास किसी सेमेस्टर के किसी भी साल और किसी भी विषय का क्वेश्चन पेपर है और कोई अन्य पढाई सामग्री तो उसे हमलोगों को भेजे