1. In the given below code, what will be the value of a variable x? #include int main() { int y = 100; const int x = y; printf("%d\n", x); return 0; }
Answers
The value of variable x will be 100
Let's correct the question.
1. In the given below códe, what will be the value of a variable x?
#include <stdio.h>
int main()
{
int y = 100;
const int x = y;
printf("%d\n", x);
return 0;
}
Explanation
A variable 'y' is defined with the data type int which means integer value.
The value which is assigned to the variable 'y' is 100.
Now, another variable 'x' is defined with data types const and int.
'x' is assigned with the variable 'y' value that is 100.
It means that x = 100
Now, the value of the 'x' variable is printed and we get the output as 100.
Extra Information
int
int data type is used when we are assigning an integer value to a variable.
Example
int x = 10;
float
float data type is used when we are assigning a float value or decimal value to a variable.
Example
float x = 5.6;
const
const data type is used when we strictly want that variable's value should not change further in the códe even if the value is assigned again to that variable.
Example
const int x = 100;
If you run this códe
#include <stdio.h>
int main()
{
const int x = 100;
x = 20;
printf("%d\n", x);
return 0;
}
It will give an error because a variable having data type as const can't be assigned a new value.