Turn Desktop View Off
You are here:   HomeC Programming (Theory)Basic Of C Prog.Write a C program to define a constant value.
by Dinesh Thakur Category: Basic Of C Programming

The modifier const causes the variable can not be changed in the program. As the name suggests it is useful to declare constants. We could have, for example:

const float PI = 3.141;
We can see from the example that the variables with the const modifier can be initialized. But PI may not be modified in any other part of the program. If the programmer attempt to modify PI the compiler generates a compile error.
The most important use of const is not declare variables constant in the program. Its most common use is to prevent a parameter of a function is changed by the function. This is very useful in case of a pointer, because the contents of a pointer can be changed by a function. To do so, simply declare the parameter as const. See the example:

#include <stdio.h>
#include<conio.h>
void main()
{
const int i = 10;
printf("%d", i);
getch();
}
 
OUTPUT:
10


Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article