Constants in C language

Constants in C language

27 May 2024
Intermediate
11K Views
5 min read
Learn via Video Course & by Doing Hands-on Labs

Free C Programming Online Course With Certificate

What are Constants in C?

This tutorial will cover Constants in C. Constants refer 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 where the value of certain variables remains constant during the execution of a program. In doing so we can use a qualifier const at the time of initialization.

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 the preprocessor directive

Example


 #define FIRST_NUMBER 1

Note

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

Types of Constants in C Language

  1. Primary Constant

    Primary Constant has the following sub categories

    • Integer Constant

    • Real constant
    • Character constant

  2. Secondary Constant

    Secondary Constant have the following sub categories

    • Array

    • Pointer Structure

    • Union

    • Enum

Read More: Top 50 Mostly Asked C Interview Questions and Answers

Using Constant In Our Program

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


#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);
 } 

Output

Speeding Tickets

For going 85 in a 55 zone: $450
For going 95 in a 55 zone: $600
For going 100 in a 55 zone: $675

Total in fines: $1725

(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 their usage, and their types in C language. I hope you will benefit from this article.

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Azure Developer Certification TrainingSep 14SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
ASP.NET Core Certification TrainingSep 15SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification TrainingSep 15SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
.NET Solution Architect Certification TrainingSep 22SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Software Architecture and Design TrainingSep 22SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification TrainingSep 29SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core Certification TrainingSep 29SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Amit Kumar (Software Engineer And Author)

Experienced Software Engineer with a demonstrated history of working in the professional training & coaching industry. Skilled in Asp.net MVC, Angular, Language Integrated Query (LINQ), SQL, C++, and HTML. Strong engineering professional graduated from Sathyabama University.
Accept cookies & close this