C Programming - Const - Discussion

Discussion Forum : Const - Find Output of Program (Q.No. 1)
1.
What will be the output of the program?
#include<stdio.h>

int main()
{
    int y=128;
    const int x=y;
    printf("%d\n", x);
    return 0;
}
128
Garbage value
Error
0
Answer: Option
Explanation:

Step 1: int y=128; The variable 'y' is declared as an integer type and initialized to value "128".

Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with the variable 'y' value.

Step 3: printf("%d\n", x); It prints the value of variable 'x'.

Hence the output of the program is "128"

Discussion:
4 comments Page 1 of 1.

Surekha said:   3 years ago
Yes, I agree with @Mithun Yadav.

How it is working here? Please explain.

Rohan said:   6 years ago
According to me, It is;

#include<stdio.h>

int main()
{
int y=128; /* value can change */
const int x=y;
/* once it declare as a const int , the value of x doesn't change */
printf("%d\n", x);
return 0;
}

MITHUN YADAV said:   1 decade ago
But const int only initialized by const literals. Then how it will print 128 ?

Manorama said:   1 decade ago
But it is cont int and it is different from int.

Post your comments here:

Your comments will be displayed after verification.