Poll added!
I request all members to Participate in the pole and Rate this thread
Here is another unknow fact
We all know that we can declare a constant by using #define but do you know that there is still other method to do so.
const data type
This lesser known datatype is used to declare constants.The syntax is
const type name=value;
ex:
is equivalent to
now consider this program
Code:
#include<stdio.h>
void main()
{
const int a=10;
printf("%d",a);
a=14;
printf("%d",a);
}
what do you think the ouput will be???
First off all the program will not compile itself because it is not allowed to change the value of 'a'
statically(within the program)
now change the program as
Code:
#include<stdio.h>
void main()
{
const int a=10;
printf("%d",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d",a);
}
will change the value of a
That means we can change the value of the constant declared by using const in run time mode(Dynamically) but not Statically(within the program).
now consider this program
Code:
#include<stdio.h>
#define a 10
void main()
{
printf("%d",a);
printf("Enter new value\n");
scanf("%d",&a);
printf("The value is now %d",a);
}
what do you think the output will be???
the program will not be compiled,that means that we can access the value of 'a' but not able to use scanf.
The use of const is not restricted to int only.
ex:
Code:
const char a='y';/*char*/
const char a[10]={"adithya"};/*String*/
const float a=4.67;/*float*/
const long double a=9.78843434;/*long double*/
now you can say that all these can be done using #define also.
But there are several things for which we cannot use #define
The excellent example is array.
Can anyone tell me how to declare an static constant array using #define???
But i can tell you how to do so using const
Code:
const float a[10]={1,3,5};