

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Declare variable as constant in C
Variables can be declared as constant using the const keyword or the #define preprocessor directive. Details about these are given as follows.
The const keyword
Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.
A program that demonstrates the declaration of constant variables in C using const keyword is given as follows.
Example
#include <stdio.h> int main() { const int a; const int b = 12; printf("The default value of variable a : %d", a); printf("\nThe value of variable b : %d", b); return 0; }
The output of the above program is as follows.
The default value of variable a : 0 The value of variable b : 12
The #define preprocessor directive
Variables can be declared as constants by using the #define preprocessor directive as it declares an alias for any value.
A program that demonstrates the declaration of constant variables in C using #define preprocessor directive is given as follows.
Example
#include <stdio.h> #define num 25 int main() { printf("The value of num is: %d", num); return 0; }
Output
The output of the above program is as follows.
The value of num is: 25
- Related Questions & Answers
- Different ways to declare variable as constant in C and C++
- How to declare and initialize constant strings in C#?
- How to declare a variable in C++?
- How to declare a global variable in C++
- What is different between constant and variable in C++?
- How to declare a variable in MySQL?
- How do we declare variable in Python?
- Using DECLARE to create variable in MySQL?
- How to declare a variable in Python?
- MySQL how to declare a datetime variable?
- How to declare a local variable in Java?
- How to declare a global variable in PHP?
- How to declare a global variable in Python?
- How to declare a variable correctly in a MySQLProcedure?
- Can we declare constructor as final in java?