Please enable Javascript to correctly display the contents on Dot Net Tricks!

Constant in C language

  Author : Shailendra Chauhan
Posted On : 25 Oct 2012
Total Views : 9,866   
Updated On : 16 Dec 2012
 

Constants

This tutorial will cover Constants in C. Constants refers to the fixed values that do not change during the execution of a program. A "constant" is a number, character, or character string that can be used as a value in a program. Use constants to represent floating-point, integer, enumeration, or character values that cannot be modified. C supports several types of constants that I am discussing in this article.

There may be a situation in programming that the value of certain variables to remain constant during execution of a program. In doing so we can use a qualifier const at the time of initialization.
For example :

const float pie =3.147;
const int radius =4;
const char c = 'A';
const char name[] = "Samina Kauser";

In C constant can also be used using preprocessor directive
For example :

 #define FIRST_NUMBER 1

Note

  1. const is a new data type qualifier in C defined by ANSI

Types of constant in C Language

  1. Primary Constant

    Primary Constant have following sub categories

    • Integer Constant

    • Real constant

    • Character constant

  2. Secondary Constant

    Secondary Constant have following sub categories

    • Array

    • Pointer Structure

    • Union

    • Enum

Using Constant In Our Program

(a) Constant definitions typically follow the #include directives at the top of C source code:

#include<stdio.h>
#define SPEEDLIMIT 55
#define RATE 15
#define FIRST_TICKET 85
#define SECOND_TICKET 95
#define THIRD_TICKET 100
int main()
 { 
 int total,fine,speeding; puts("Speeding Tickets\n"); 
/* first ticket */ 
speeding = FIRST_TICKET - SPEEDLIMIT; 
 fine = speeding * RATE; 
 total = total + fine;
 printf("For going %d in a %d zone: $%d\n",FIRST_TICKET,SPEEDLIMIT,fine); 
/* second ticket */ 
 speeding = SECOND_TICKET - SPEEDLIMIT; 
 fine = speeding * RATE; 
 total = total + fine;
 printf("For going %d in a %d zone: $%d\n",SECOND_TICKET,SPEEDLIMIT,fine); 
/* third ticket */ 
speeding = THIRD_TICKET - SPEEDLIMIT; 
 fine = speeding * RATE; 
 total = total + fine;
 printf("For going %d in a %d zone: $%d\n",THIRD_TICKET,SPEEDLIMIT,fine); 
/* Display total */
 printf("\nTotal in fines: $%d\n",total);
 return(0);
 } 

(b) Constant using const keyword C programming:

When declaring a const variable, it is possible to put const either before or after the type: that is, both

 int const a = 15; 

Or

 const int x = 15; 

Following is a simple example

 main() 
{
 const float pi = 3.14;
 float area_of_circle;
 area_of_circle = pi*r*r;
 printf("Area of circle is :%f",area_of_circle);
 } 
What do you think?

In this article I try to explain the concept of constants it's usage and it's types in C language. I hope you will be beneficial by this article. Comment and critics are welcome.



Free Interview Books
 
COMMENTS (0)
6 JUL
ASP.NET Core with Angular4 Development (Online)

7:00 AM-9:00AM IST(+5:30 GMT)T/T

Know More
5 JUL
NodeJS with Angular4 Development (Online)

08:00 PM-09:30 PM IST(+5.30 GMT)M/W/F

Know More
1 JUL
ASP.NET MVC with Angular4 Development (Online)

03:30 PM-05:30 PM IST(+5.30 GMT) Weekend

Know More
1 JUL
ASP.NET MVC with Angular4 Development (Classroom)

8:00 AM-10:00 AM Weekend

Know More
1 JUL
ASP.NET Core with Angular4 Development (Classroom)

3:00PM 5:00PM Weekend

Know More
1 JUL
Xamarin Forms : Build Cross-platform Apps (Classroom)

3:00 PM 5:00 PM Weekend

Know More
1 JUL
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

Know More
30 JUN
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5.30 GMT) MON/WED/FRI

Know More
29 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST(+5:30 GMT)T/W/T

Know More
26 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

9:30PM- 11:00PM IST(+5:30GMT) M/W/F

Know More
26 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

9:30PM- 11:00PM IST(+5:30GMT) M/W/F

6 JUN
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)(TT)

3 JUN
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:30 PM-7:00 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+