Constants and Literals in C
So far, we have learned about variables.
Variables are used to store data, and their values can be changed as needed in a program.
However, some values used in a program must remain constant.
For example, PI = 3.14. The value of PI never changes and should not be modified.
In this topic, we will learn how to create such fixed values in C.
Constants in C Language
A constant is a symbolic name that represents a fixed value.
Constants make code easier to read and write, and they protect important data
from being changed accidentally.
In C, there are two main ways to define constants.
Here, we will learn how to create constants using the
const keyword.
The second method will be learned later.
Example:
const int MAX = 100;
- Here,
constis a keyword. - Constants are usually written in uppercase letters.
- Once declared, the value of
MAXcannot be changed.
The following program demonstrates creating a constant MAX using the const keyword in C Language
#include<stdio.h>
int main() {
const int MAX = 100;
printf("Maximum:%d", MAX);
return 0;
}
Output :
Maximum:100
Maximum:100
In the above program:
const int MAX = 100;defines a constant namedMAX.- It is of integer (
int) type. - The value is fixed as 100.
- Because of the
constkeyword, the value ofMAXcannot be changed. - If we try to write
MAX = 200;anywhere in the program, it will cause an error. - Therefore,
MAXremains a fixed value.
Literals in C
Literals are fixed data values written directly in the source code.
Constants are fixed values created using a name.
If we use the value directly in the program without assigning it to a name,
it is called a literal.
Mainly, we use four types of literals in C.
1. Integer Literals
These represent whole numbers.
Example:
100, 42
2. Floating-Point Literals
These represent numbers with a decimal point.
Example:
- 3.14 (double literal)
- 0.001f (float literal)
- 3.14e-2 (exponential notation)
-
Note: The suffix
forFis used for float (e.g.,3.14f). If no suffix is used, the value is treated as a double by default.
3. Character Literals
These represent a single character.
They are written using single quotes.
Example:
'A', '7'
4. String Literals
A sequence of characters is called a string.
Strings are written using double quotes.
Example:
"Hello, World!", "C Programming"
The following program demonstrates printing literals in C Language
#include<stdio.h>
int main(){
printf("integer literal:%d \n", 10);
printf("float literal:%.2f \n", 24.45f);
printf("double literal:%lf \n", 15.78);
printf("character literal:%c \n", 'A');
printf("string literal:%s \n", "C Language\0");
return 0;
}
Output :
integer literal:10
float literal:24.45
double literal:15.780000
character literal:A
string literal:C Language
integer literal:10
float literal:24.45
double literal:15.780000
character literal:A
string literal:C Language
In the above program:
-
Integer Literal
- Here,
10is an integer literal. - It represents a whole number.
- Here,
-
Float Literal
24.45fis a float literal.- The suffix
ftells the compiler that it is a float value.
-
Double Literal
15.78is a double literal.- It represents a double-precision floating-point number.
-
Character Literal
'A'is a character literal.- It represents a single character.
-
String Literal
"C Language\0"is a string literal.- It represents a sequence of characters.
\0is the null character that marks the end of a string.
Here, we have learned two additional important concepts.
-
In
printf("float literal:%.2f \n", 24.45f);,%.2fis used to print only two digits after the decimal point. Otherwise, more digits would be printed by default. -
'\0'is called the null character (here, 0 means zero). It is used to indicate the end of a string.
So far, we have learned:
- About constants and literals
- How to write programs using constants and literals
- Scope of Variables will be covered in the next topic.