Type of C Contants- part 6
Type of C Contants- part 6
Contents [hide]
- 0.0.1 Types of Constants in C (Part 6)
- 0.0.2 1. Integer Constants
- 0.0.3 2. Floating-Point (Real) Constants
- 0.0.4 3. Character Constants
- 0.0.5 4. String Constants
- 0.0.6 5. Symbolic Constants
- 0.0.7 6. Enumeration Constants
- 0.0.8 CHAPTER -4 CONSTANTS ,VARIABLES AND DATA TYPES
- 0.0.9 Type of C Contants- part 6
- 0.0.10 Unit-I Chapter 2 C Tokens: Character set, Identifiers, …
- 1
C Programming – Types of Constants (Part 6)
Types of Constants in C (Part 6)
In the C programming language, constants are fixed values that do not change during program execution. Constants can be classified into different types based on their data type and usage.
1. Integer Constants
Integer constants are whole numbers (without decimal points). They can be:
- Decimal (Base 10):
10, 25, -100
- Octal (Base 8, starts with 0):
012 (equivalent to 10 in decimal)
- Hexadecimal (Base 16, starts with 0x):
0xA (equivalent to 10 in decimal)
2. Floating-Point (Real) Constants
Floating-point constants represent decimal numbers or numbers in exponential form.
- Example:
3.14, -0.001, 1.2E3 (equivalent to 1.2 × 10³ = 1200)
3. Character Constants
A single character enclosed in single quotes (‘ ‘).
- Example:
'A'
,'5'
,'@'
- Each character constant has an ASCII value (e.g.,
'A'
= 65).
4. String Constants
A sequence of characters enclosed in double quotes (” “).
- Example:
"Hello"
,"C Programming"
- Stored as an array of characters with an implicit
\0
(null terminator).
5. Symbolic Constants
Defined using the #define
preprocessor directive.
- Example:
- These are replaced by their values during compilation.
6. Enumeration Constants
Created using enum
for defining a set of named integer constants.
- Example:
Would you like a detailed explanation of any specific
CHAPTER -4 CONSTANTS ,VARIABLES AND DATA TYPES
Type of C Contants- part 6
Unit-I Chapter 2 C Tokens: Character set, Identifiers, …
Here’s a structured and easy-to-understand explanation of:
C Programming – Types of Constants (Part 6)
Suitable for BCA, B.Tech, Diploma, or beginner programmers.
What are Constants in C?
A constant is a fixed value that does not change during the execution of a program.
Constants are used in programs to make them more readable and maintainable.
Types of Constants in C
C supports several types of constants, broadly categorized as:
Integer Constants
- Whole numbers without decimal.
- Example:
10
,-5
,0
,2000
Types:
- Decimal (Base 10):
25
- Octal (Base 8): starts with
0
, e.g.037
- Hexadecimal (Base 16): starts with
0x
, e.g.0x1F
Floating-point Constants
- Numbers with decimal point or in exponential (scientific) notation.
- Example:
3.14
,-0.001
,2.5e3
Character Constants
- A single character enclosed in single quotes
' '
. - Example:
'A'
,'z'
,'5'
Each character constant has an ASCII value internally.
char ch = 'A'; // ASCII value = 65
String Constants
- A sequence of characters enclosed in double quotes
" "
. - Example:
"Hello"
,"1234"
,"C Programming"
char str[] = "Hello";
Enumeration Constants (enum)
- Used to define named integer constants.
- Improves readability of code.
enum Days { MON, TUE, WED, THU, FRI };
Here, MON
is 0, TUE
is 1, etc.
Defined Constants (#define
)
- Use the preprocessor directive
#define
to create constants.
#define PI 3.14
#define MAX 100
Once defined, PI
will always be replaced with 3.14
.
Summary Table
Constant Type | Example |
---|---|
Integer | 100 , -23 |
Floating-point | 3.14 , 2.0e3 |
Character | 'A' , '7' |
String | "Hello" |
Enumeration | enum Colors {RED, BLUE} |
Defined (#define ) |
#define PI 3.14 |
Tips
- Constants make code easier to read and maintain.
- Use
const
keyword to declare variables as constants:
const int age = 18; // age cannot be changed
Would you like:
Practice questions on constants?
A PDF revision sheet?
A quiz to test your understanding?
Let me know, and I’ll provide it!