The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

C Storage Classes

In C programming language, storage classes are used to define things like storage location (whether RAM or REGISTER), scope, life time and default value of a variable.

In C programmig language, there are FOUR storage classes and they are as follows...

  1. auto storage class
  2. extern storage class
  3. static storage class
  4. register storage class

auto storage class

The default storage class of all local varibles (variables declared inside block or function) is auto storage class. .

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