C Programming - Const - Discussion
Discussion Forum : Const - Find Output of Program (Q.No. 4)
4.
What will be the output of the program?
#include<stdio.h>
int main()
{
const int x=5;
const int *ptrx;
ptrx = &x;
*ptrx = 10;
printf("%d\n", x);
return 0;
}
Answer: Option
Explanation:
Step 1: const int x=5; The constant variable x is declared as an integer data type and initialized with value '5'.
Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer.
Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx.
Step 4: *ptrx = 10; Here we are indirectly trying to change the value of the constant vaiable x. This will result in an error.
To change the value of const variable x we have to use *(int *)&x = 10;
Discussion:
18 comments Page 2 of 2.
Hari said:
1 decade ago
I also don't think step 4 willl work how can it will try to change the value of x answer is 5.
Rishi said:
9 years ago
What is const int x=5;int *ptrx;?
Please explain.
Please explain.
Prudhvi said:
1 decade ago
Any one give me a clear explanation to step 4.
Haritha said:
4 years ago
*(int *)&x = 10;
Please explain this.
Please explain this.
Gaurav said:
8 years ago
*(int *)&x = 10;
Please explain this.
Please explain this.
Bijay said:
1 decade ago
In gcc compiler it will work o/p:10.
Suresh said:
9 years ago
Why cannot print x values of 5?
Martyf said:
1 decade ago
It is very difficult paper.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers