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”
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:
#define CONSTANT_NAME value
#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:
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
23, 738, -1278, etc.
long int, long long int
Floating-point or Real constants
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 Material||Capacitors in Parallel|
|Capacitors in Series||Carnot Cycle|
|Cement Test||Clamping Circuit|
|Clipping Circuit||CMOS Fabrication|
|CMOS Converter||Column Base|