Possible Duplicate:
const in C vs const in C++
I have following code
In C
int main()
{
const int k;//allowed but garbage and later we can't modify
printf("%d",k);
}
o/p=Garbage
In C++
int main()
{
const int k; //not allowed from here itself
printf("%d",k);
}
o/p-compile time error
I having doubt what is the use of the const in C if it it is allowed to declare it with out initialization but after it declaration we can’t initialize it.
But is c++ it is good that we can’t declare a const value without initialization.
Is there any use of the variable k in C or it is useless,if we only declare it as later modification is not possible.
It has no use by itself.
However, there are compiler specific extensions where this becomes useful again.
C Compilers for embedded platforms, for example, often have extensions that allow to give a variable a fixed address, or as an alias for an memory mapped I/O port.The
constwould indicate / enforce that youonly readfrom that address, for example amemory mapped input port.