- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Constants
![]() Share with a Friend |
C Programming - C Constants
C Constants
In C, constants are fixed values that cannot be modified during the execution of a program. Constants are used to make programs more readable, maintainable, and error-proof by replacing hardcoded values with meaningful names.
Types of Constants in C
- Integer Constants
- Represent whole numbers.
- Can be written in decimal, octal, or hexadecimal format.
Examples:
C
int decimal = 42; // Decimal constant
int octal = 052; // Octal constant (starts with 0)
int hexadecimal = 0x2A; // Hexadecimal constant (starts with 0x)
| Type | Example |
|---|---|
|
Decimal |
42 |
|
Octal |
052 |
|
Hexadecimal |
0x2A |
- Floating-Point Constants
- Represent numbers with fractional parts.
- Can be written in decimal or scientific/exponential notation.
Examples:
C
float pi = 3.14159; // Decimal constant
float scientific = 2.5e3; // Scientific notation (2.5 × 10³)
- Character Constants
- Represent a single character enclosed in single quotes (').
- Correspond to their ASCII value.
Examples:
C
char letter = 'A'; // Character constant
char newline = '\n'; // Escape sequence constant
| Character Constant | Description |
|---|---|
|
'A' |
The character A |
|
'\n' |
Newline character |
|
'\t' |
Tab character |
- String Constants
- Represent a sequence of characters enclosed in double quotes (").
- Always terminate with a null character (\0).
Examples:
C
const char *greeting = "Hello, World!";
- Symbolic Constants
- Created using the #define preprocessor directive or the const keyword.
Using #define:
C
#define PI 3.14159
#define MAX 100
Using const :
C
const float PI = 3.14159;
const int MAX = 100;
- Boolean Constants
- Introduced in C99 with <stdbool.h>.
- Represent true and false.
Examples:
C
#include <stdbool.h>
const bool isAvailable = true; // Boolean constant
Declaring Constants
Using const Keyword
- The const keyword creates constants with a specific data type.
Example:
C
const int MAX = 50;
Using #define Preprocessor Directive
- Used to create symbolic constants without a specific data type.
Example:
C
#define MAX 50
| Feature | const | #define |
|---|---|---|
|
Type-checking |
Yes |
No |
|
Scope |
Block-level |
Global |
|
Debugging Support |
Yes |
No |
Rules for Constants
- Integer Constants:
- Cannot have a decimal point.
- Must fit within the range of the data type.
- Floating-Point Constants:
- Must include a decimal point or an exponent.
- Character Constants:
- Enclosed in single quotes.
- Only one character is allowed.
- String Constants:
- Enclosed in double quotes.
- Can represent multiple characters.
Example Program
C
#include <stdio.h>
#define PI 3.14159 // Symbolic constant
int main() {
const int MAX = 100; // Constant using const keyword
const char newline = '\n'; // Character constant
printf("The value of PI is: %.2f%c", PI, newline);
printf("The maximum value is: %d%c", MAX, newline);
return 0;
}
Output:
The value of PI is: 3.14
The maximum value is: 100
Advantages of Using Constants
- Readability:
- Improves program readability by replacing hardcoded values with meaningful names.
- Maintainability:
- Easy to update values in one place without changing the entire code.
- Error Prevention:
- Prevents accidental modification of values.
Key Points
- Use const for type-checked, block-scoped constants.
- Use #define for preprocessor-based constants.
- Always use meaningful names for constants to enhance code readability.
- Use escape sequences for special character constants.
Constants are essential for creating robust and maintainable programs in C.
