Const

Q1:
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;
}

A Garbage value

B Error

C 10

D 5

ANS:A - 5

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;



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.