+ 2

Why the following is error ?

int const x = 10;//correct ------------------------- int const x; x = 10; // error ---------------------------- BESIDES, int const x; scanf ("%d",&x); //the value user will input , the system will except that. Another thing is, What are the major differences between constant and variable? ?

15th May 2020, 4:50 AM
AR Sĕŕïéś
AR Sĕŕïéś - avatar
2 Respuestas
0
The first one is true and that obvious, about the second one why it's not correct as you know integers in declaration time if no value assigned to it, it takes a value by default which is 0, thus "const int x;" means "const int x = 0;" the difference between constant and variable as far as I know is that variables are alive as long as the function which called them is running and when the function quits the variable got destroyed, but constant variables are alive from the beginning of the program to the end of that program.
15th May 2020, 10:52 PM
aissa ultimate
aissa ultimate - avatar
+ 3
A constant should be "assigned a value at the time of its declaration", unlike a variable. So, the first one is only correct syntax. In second one, you have left the constant undeclared in first line, hence the error. EDIT: If anyone is interested in how to take constant input, then do int _x; scanf("%d", &_x); const int x = _x; // that's it
15th May 2020, 4:58 AM
777
777 - avatar