In C programming, constants are fixed values that do not change during the execution of a program. They are used to represent values that remain constant throughout the program's lifecycle. Constants are categorized into several types, including numeric constants, character constants, string constants, and symbolic constants. Here's an overview of these types of constants in C.
Numeric Constants:
- Numeric constants represent fixed numerical values and can be of various types, such as integers, floating-point numbers, or characters.
- Integer Constants: Whole numbers without decimal points.
- Example:
42
,-10
,0
,255
- Example:
- Floating-Point Constants: Numbers with decimal points.
- Example:
3.14
,-0.001
,1.0e5
(scientific notation)
- Example:
- Character Constants: Single characters enclosed in single quotes ('').
- Example:
'A'
,'5'
,'?'
- Example:
Character Constants:
- Character constants represent individual characters and are enclosed in single quotes ('').
- Example:
'A'
,'b'
,'1'
String Constants:
- String constants represent sequences of characters and are enclosed in double quotes ("").
- Example:
"Hello, World!"
,"C Programming"
Symbolic Constants (Named Constants):
- Symbolic constants are user-defined constants with names and values that remain fixed throughout the program.
- They are typically defined using the
#define
preprocessor directive. - Example:c
#define PI 3.14159265359 #define MAX_LENGTH 100
Enumeration Constants:
- Enumeration constants (enums) are user-defined data types that consist of a set of named integer constants.
- They are defined using the
enum
keyword. - Example:c
enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
Hexadecimal Constants:
- Numeric constants can also be represented in hexadecimal notation by prefixing them with
0x
or0X
. - Example:
0x1A
,0xFF
,0x10
- Numeric constants can also be represented in hexadecimal notation by prefixing them with
Octal Constants:
- Numeric constants can be represented in octal notation by prefixing them with
0
. - Example:
043
,077
,012
- Numeric constants can be represented in octal notation by prefixing them with
Binary Constants:
- Binary constants are represented in binary notation by prefixing them with
0b
or0B
. - This notation is available in C99 and later standards.
- Example:
0b1010
,0B1101
- Binary constants are represented in binary notation by prefixing them with
Constants play a crucial role in C programming, as they provide a way to work with fixed values, make code more readable, and facilitate easy modifications when constants need to be changed globally in a program.
Follow us