C Tutorials
- What is C?
- History Of C?
- Feature of C?
- How to Install C?
- How to write c program?
- First c program
- Flow of C program
- printf() and scanf() in C
- C Program Structure
- Variables in C
- Data Types in C
- Keywords in C
- C Operators
- Comments in C
- Escape Sequence in C
- Escape Sequence in C
- Constants in C
- C - Storage Classes
- C - Decision Making
- Switch statement in C
- C Loops
- Loop Control Statements
- Type Casting in C
- functions in C
- call by value Or call by reference
- Recursion in C
- Storage Classes in C
- Array
- String
- Pointer
- Pointer And Array
- Pointer with function
- Structure
- Union
- File Handling
- Preprocessor Directives
Important Links
Home » Constants in C »
Constants in C
A constant is a fixed value that can't be change during the execution of the program. its value always fix.
There are two ways to define constant in C programming.
C const keyword :-
The const keyword is used to define constant in C programming.
#include
int main(){
const float PI=3.14;
printf("The value of PI is: %f",PI);
return 0;
}
int main(){
const float PI=3.14;
printf("The value of PI is: %f",PI);
return 0;
}
If you try to change the the value of PI, it will render compile time error.
#include <stdio.h>
int main(){
const float PI=3.14;
PI=4.5;
printf("The value of PI is: %f",PI);
return 0;
}
The #define Preprocessor :-
#define identifier value
#include <stdio.h>#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main() {
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}