Constants in C - Definition, Types, Examples

By Anjnee Bhatnagar|Updated : October 10th, 2022

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

Constants in C PDF

A constant is 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 simultaneously 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.

Download Formulas for GATE Computer Science Engineering - Programming & Data Structures

What are Constants in C?

Constants in C language are used in the program execution to store values that will remain fixed and not vary at any point. A variety of questions is formulated in the GATE exam based on this topic. We can use constants in C using two methods:

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

Download Formulas for GATE Computer Science Engineering - Discrete Mathematics

Constants in C Definition

Constants in C are basically defined as a named memory. The constants in C programming can be of any data type: a character type, floating type, string, integer, and double type. 

Use of #define for Creating Constants in C

Constants in C can be created by using preprocessor directives. We must define it at 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. It is also used in the GATE CSE exam vividly. The syntax for creating constant in C using the keyword const:

Syntax:

const data_type variable_name= value;

Example: const int length= 10;

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

Download Formulas for GATE Computer Science Engineering - Algorithms

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

What are Literals in C?

Literals are the values one assigns to the variable that remains constant throughout the program's execution. 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 "values" we cannot refer to 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 associated memory address. The literals contain memory, so they can be referred easily, while constants do not occupy any memory.

Important Topics for Gate Exam
Brittle MaterialCapacitors in Parallel
Capacitors in SeriesCarnot Cycle
Cement TestClamping Circuit
Clipping CircuitCMOS Fabrication
CMOS ConverterColumn Base

Comments

write a comment

FAQs on Constants in C

  • 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