Apply for Zend Framework Certification Training

c



< Scope Rules in C Dynamic memory allocation >



Constants in C
A constant in C is a value that does not change during the execution of a program. Constants are useful when a particular value must remain fixed throughout the program.
In C, constants can be created using the const keyword or the #define preprocessor directive. Constants can represent different types of values, such as integers, floating-point numbers, characters, and strings.
1. Using the const Keyword
The const keyword tells the compiler that the value of a variable should not be modified after it has been initialized.
Example
#include <stdio.h>
int main() {
    const int a = 10;
    printf("%d", a);
    return 0;
}
Output
10
Syntax
const data_type variable_name = value;
For example:
const int age = 20;
const float pi = 3.14159;
const char grade = 'A';
Here, age, pi, and grade cannot be modified after initialization.
Important Properties of const Variables
1. Initialization
A const variable should normally be initialized when it is declared.
#include <stdio.h>
int main() {
    const int a = 10;
    printf("%d", a);
    return 0;
}
If a const variable is declared without initialization:
const int a;
its value is indeterminate. Reading such a variable produces undefined behavior; you should not rely on it containing 0 or any other particular value.
2. Value Cannot Be Modified
Once a const variable has been initialized, its value cannot be changed through that variable.
Example
#include <stdio.h>
int main() {
    const int a = 10;
    a = 20;   // Error
    printf("%d", a);
    return 0;
}
Output
The compiler will generate an error similar to:
error: assignment of read-only variable 'a'
This happens because a has been declared as a read-only variable using const.
Constants Using #define
Another way to create symbolic constants in C is by using the #define preprocessor directive.
Unlike const, a #define constant does not have a data type. The preprocessor simply replaces the constant name with its value before compilation.
Syntax
#define CONSTANT_NAME value
Example
#include <stdio.h>
#define PI 3.14
int main() {
    printf("%.2f", PI);
    return 0;
}
Output
3.14
Here, before compilation, the preprocessor replaces PI with 3.14.
Real-World Examples of Constants
Constants are commonly used for values that should remain fixed throughout a program.
1. ATM – Daily Withdrawal Limit
A banking application may define a fixed daily withdrawal limit:
const int dailyLimit = 20000;
The program can use this value whenever it checks a customer's withdrawal amount.
2. Application – Maximum Login Attempts
An application may allow a user only three login attempts:
const int maxLoginAttempts = 3;
3. Mathematics – Value of Pi
The value of π can be stored as a constant:
const float pi = 3.14159;
4. HR System – Maximum Leave Days
A company may define a fixed maximum number of leave days:
const int maxLeaveDays = 15;
Using constants makes programs easier to maintain because a fixed value can be defined once and reused wherever required.
Constants vs. Literals
Constants and literals are related, but they are not the same thing.
A constant variable is an identifier whose value cannot be modified, while a literal is the actual fixed value written directly in the source code.
Constant Literal
A constant is an identifier whose stored value is not intended to be modified. A literal is a fixed value written directly in the program.
const can be used to create a constant variable. Literals do not require const.
A constant variable has a data type. A literal represents a value of a particular type.
A constant variable can have an address. An ordinary numeric or character literal is not an object with its own address.
Example: const int c = 20; Examples: 20, 15.5, 'A', "Hello"
Example
const int marks = 50;
Here:
marks → constant variable
50 → integer literal
const vs. #define
Both const and #define can be used to represent fixed values, but they work differently.
const #define
Creates a typed object. Creates a preprocessor macro.
Handled by the compiler. Handled by the preprocessor.
Has a data type. Does not have a data type of its own.
Follows normal C scope rules. Macro substitution is controlled by preprocessor directives.
Syntax: const type name = value; Syntax: #define name value
Generally provides better type checking and debugging. Performs text substitution before compilation.
Example using const
const int MAX = 100;
Example using #define
#define MAX 100
Which One Should You Use?
For a value that represents a constant object in C, const is generally preferred because it provides type information and works more naturally with the C type system.
Use #define when you specifically need a preprocessor macro, such as conditional compilation or macro substitution.
Key Point
const creates a read-only object, whereas #define performs textual substitution before compilation.

< Scope Rules in C Dynamic memory allocation >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top