EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Constants in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Constants in C

Constants in C

Introduction to Constants in C

In C programming language, a name given to a variable whose values cannot be changed such variables is known as constants. These are also called as literals in C programming language as they are similar to variables but with one condition of which values of these variables cannot be altered or the values are fixed for such variables. There are different basic types of constants provided by C, in turn, they are categorized into two major categories that are primary constants and secondary constants. In primary constants, we have integer constants, real constants, and character constants, etc. In secondary constants, we have Array, structure, union, pointer, etc.

Functions of Constants in C

As discussed above constants are variables with fixed values. In C programming language, constants can be declared or defined in two ways one is using a keyword “const” and the other is using #define preprocessor. Let us see the syntax and its example:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. Use of Const keyword for Defining Constants

Syntax:

const contant_type constant_name = value;

Or

constant_type const const_name = value;

Example #1

Code:

#include<stdio.h>
int main()
{
const float PI=3.14;
int radius = 2;
float area = PI * radius * radius;
printf("The area  of circle is: %f",area);
return 0;
}

Output:

Constants in C-1.1

In the above program, we have declared a “const” keyword for defining pi value and this variable is used to calculate the area of the circle. So using constant “PI” we can use it directly in the formula for calculating the area of the circle with the value declared once using the “const” keyword with value as 3.142 which is not altered during the execution of the code. If we try to change the “PI” value then it will give an error. Let us try changing the value of “PI”.

Example #2

Code:

#include<stdio.h>
int main()
{
const float PI=3.14;
PI = 5.76;
int radius = 2;
float area = PI * radius * radius;
printf("The area  of circle is: %f",area);
return 0;
}

Output:

Constants in C-1.2

Normally constants can be of any data types. Let us see an example for this:

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)
Example #3

Code:

#include <stdio.h>
int main()
{
const int var1 = 70;
printf("Integer constant:%d \n", var1 );
const float var2 = 35.89;
printf("Floating point constant: %.2f\n", var2 );
const char var3 = 'S';
printf("Character constant: %c\n", var3 );
const char var4[10] = "string";
printf("String constant: %s\n", var4);
return 0;
}

Output:

Constants in C-1.3

In the above code, we have used integer constant “int”, real constant as “float”, a character constant as “char” and string constant as “char”.

2. Constant Can be Defined Using #define

In this method, constants can be declared or defined using the #define preprocessor directive. In C programming, this method is explained using macros in C. Let us see how this preprocessor directive will be used to define constants which are used to declare constant values for the entire code.

Syntax:

#define constant_name value

Example #1

Code:

#include <stdio.h>
#define LENGTH 20
#define WIDTH  30
int main() {
int area_r, area_s;
area_r = LENGTH * WIDTH;
printf("Area of rectange is l * b =  %d\n", area_r);
area_s = LENGTH * LENGTH;
printf("Area of square is l * b =  %d", area_s);
return 0;
}

Output:

Constants in C-1.4

In the above code, we can see that we are declaring “LENGTH” and “WIDTH” as two constants using the #define preprocessor directive. These constants can be used in the entire program. In the above code, we are calculating the area of rectangle and square, where these constants are declared or defined only once at the starting and we can use these constants in the entire program any number of times.

Points to Remember for Constants in C

Generally, in C programming language, constants are assigned with some value and this value is known as literal. Hence in C programming language constants are also known as literals.

Example:

const int max =100;

Here “100” is constant integer literal in the above constant expression.

  • We should be careful while defining the constants, as just using the “const” keyword and declaring is as simple as declaring the variables in C. But declaration should be done as

int a = 5;

The below two declarations are not valid for defining the constants:

const int a;
const int a;
= 5;

  • For the declaration of constants, it is always the best practice for writing constant names in capital letters or uppercase letters. This helps in understanding and readable program to others as well as for us.
  • We cannot change the value of the constant once assigned using “const” keyword or #define directive, because it will give an error if try to assign some other value to the constant that is declared.

Conclusion

In this article, we have discussed the constants in the C programming language. The constants are divided into two categories primary and secondary constants in which primary constants consist of numerical constants like integer, decimal, real, etc and character constants like character, string, backslash, etc. In secondary constants, we have an array, pointer, structure, union, etc. Constants in C are those whose values cannot be altered once declared and these constants are also known as literals.

Recommended Articles

This is a guide to Constants in C. Here we discuss the Introduction and working of constants in c along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. C Literals
  2. Java Literals
  3. Error Handling in C
  4. Function Prototype in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More