Constants in C are fixed values that cannot be changed during the execution of a program. They are also known as literals. Constants can be of various data types such as integers, floating-point numbers, characters, and strings. In C, constants are used to assign fixed values to variables and also to initialize arrays and structures.
Contents
Constaints in C
There are two types of constants in C:
1. Primary constants
These are the basic constants that can be directly assigned to a variable. They include integer constants, character constants, and floating-point constants.
Examples:
Integer constants: 10, 20, 30
Character constants: ‘A”https://dheeraj-patidar.blogspot.com/2023/01/,”B”https://dheeraj-patidar.blogspot.com/2023/01/,”C’
Floating-point constants: 10.5, 20.5, 30.5
2. Secondary constants
These are derived constants that are created by combining primary constants. They include string literals, array literals, and enumeration constants.
Examples:
String literals: “Hello”https://dheeraj-patidar.blogspot.com/2023/01/,”World”
Array literals: {1, 2, 3}, {‘A”https://dheeraj-patidar.blogspot.com/2023/01/,”B”https://dheeraj-patidar.blogspot.com/2023/01/,”C’}
3. Enumeration constants
enum {RED, GREEN, BLUE}
In C, constants are also known as literals. A literal is a notation for representing a fixed value in source code. There are different types of literals in C such as integer literals, floating-point literals, character literals, and string literals.
4. Integer literals:
Integer literals are used to represent integer values. They can be represented in decimal, octal or hexadecimal notation. Decimal notation is the most common and uses the digits 0-9. Octal notation uses the digits 0-7 and is prefixed with a 0. Hexadecimal notation uses the digits 0-9 and the letters A-F and is prefixed with 0x.
Examples:
Decimal notation: 10, 100, 1000
Octal notation: 012, 064, 0100
Hexadecimal notation: 0xA, 0x64, 0x3E8
5. Floating-point literals
Floating-point literals are used to represent real numbers. They are represented in decimal notation and can be in the form of a fraction or a decimal. They can also include an exponent.
Examples:
Fraction: 3.14, 0.01
Decimal: 3., .14
Exponent: 1.23e2, 3.14E-2
6. Character literals
Character literals are used to represent individual characters. They are enclosed in single quotes.
Example: ‘A”https://dheeraj-patidar.blogspot.com/2023/01/,”B”https://dheeraj-patidar.blogspot.com/2023/01/,”C’
7. String literals
String literals are used to represent a sequence of characters. They are enclosed in double quotes.
Example: “Hello”https://dheeraj-patidar.blogspot.com/2023/01/,”World”
Conclusion
In C, constants play an important role in the program as they are used to initialize variables and arrays, and also to perform calculations. They help to make the code more readable and maintainable as the values do not change during the execution of the program. Using constants in C can also improve the performance of a program as the compiler can optimize the code when it knows that the value of a variable will not change.
Constants in C are fixed values that cannot be changed during the execution of a program. They are also known as literals and can be of various data types such as integers, floating-point numbers, characters, and strings. There are two types of constants in C: primary constants and secondary constants. Understanding how to use constants in C can make your code.