Constants in C

By Anjnee Bhatnagar|Updated : April 27th, 2022

Constants in C: The constants in C are a value assigned to a variable and this value cannot be changed or altered. We assign these constants in C to exhibit the same value throughout the program’s execution. It means that the value assigned once stays fixed throughout the program’s lifetime.

A constant is a name for a variable whose values cannot be updated or altered. A constant is comparable to variables in the C programming language, except it can only hold one variable at a time throughout program execution. It indicates that once we give a value to a constant, we can't modify it throughout the program's execution; it's fixed.

In this article, we will learn about the constants in C, the use of constants in C, and the types of constants in C according to the GATE syllabus for CS. 

Table of Content

What are Literals in C?

Literals are defined as the values that one assigns to the variable that remain constant throughout the execution of the program. Constants and literals are used interchangeably in C programming. The literals also contain memory space but are never referenced as variables. The literals in C are of four types: Integer literals, Float literals, Character literals, and String literals.

Difference Between the Literals and Constants in C

The literals in C are lvalues to which we can refer in the memory. On the other hand constants in C are rvalues to which we cannot refer in the memory. An lvalue is an expression with an address associated with it while an rvalue is an expression that does not contain any memory address associated with it. The literals contain memory so they can be referred easily while constants do not occupy any memory.

What is the Use of Constants in C?

Constants in C are basically defined as a named memory. The constants in C can be of any data type that is a character type, floating type, string, integer, and double type. Constants are used in the program execution to store values that will remain fixed and not vary at any point in time. We can use constants in C using two methods:

  • By using ‘”#define”
  • By using the keyword “const”

Use of #define for creating Constants in C

Constants in C can be created by using preprocessor directives. We must define it in the beginning of the program. The syntax for creating constants in C using #define:

Syntax:

#define CONSTANT_NAME value

 Example:

#define velocity 3.6

 Here “velocity” is the constant name with the value 3.6. This value cannot be changed throughout the program execution.

Use of const keyword for creating Constants in C

We declare the keyword “const“ before the variable to create a constant. Any data type can use this keyword. The syntax for creating constant in C using keyword const:

Syntax:

const data_type variable_name= value;

Example: const int length= 10;

Here keyword const is prefixed to the integer data type which is assigned a value of 10.

Types of Constants in C

Constants in C can be of different types depending on the data types available. There are two types of constants in C:

  • Primary Constants: integer, float, double, and character constants
  • Secondary Constants: Array, structures, Enum, etc.

However, based on data types, constants can be divided into the following along with their ranges:

Type of Constants

Data type

Integer constants

int

23, 738, -1278, etc.

unsigned int

long int, long long int

Floating-point or Real constants

doule

500.987654321

float

Octal constant

int

Hexadecimal constant

int

character constants

char

string constants

char

If you are preparing for ESE/ GATE or other PSU Exams, then avail Online Classroom Program for ESE and GATE: 

Comprehensive Preparation for GATE & ESE

Attempt online mock tests of ESE & GATE 2023 at BYJU'S Exam Prep to improve the exam score in all disciplines.

Online Test Series for ESE and GATE

Thanks

Comments

write a comment

FAQs

  • The constants in C are defined as the values that are assigned to any variable or identifier. The constants remain fixed throughout the execution of the program. However, on the other hand, variables can change their value and may not remain fixed during their lifetime.

  • In C constants can be created using two methods, namely, #define preprocessor directives and using the keyword const. Through preprocessor directives, the constants are declared before the global variables.

  • In C, constants are majorly divided into two types- primitive and secondary constants. The primitive types include int, float, and string whereas, secondary types include array, structure, etc.

  • A literal has the same physical significance as that of a constant in C. Therefore, the word literal is interchangeably used with constants. It is used to specify a significant value in the program.

  • Constants in C are used to define a value that cannot be changed throughout program execution. Constants can be declared using the keyword “const”.

  • The const statement is used to declare and set a constant. You give a value a meaningful name by defining a constant. A constant can't be changed or given a new value after it's been stated.

Follow us for latest updates