Knowee
Questions
Features
Study Tools

What is the output of the following C program ?void main (){const int x = 5;const int *ptrx;ptrx = &x;*ptrx = 10;printf("%d", x);}A5BCompilation ErrorC10DGarbage ValuePrevious

Question

What is the output of the following C program ?void main (){const int x = 5;const int *ptrx;ptrx = &x;*ptrx = 10;printf("%d", x);}A5BCompilation ErrorC10DGarbage ValuePrevious

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the given C program would be a Compilation Error.

Here's why:

In the C programming language, the keyword 'const' is used to make a variable constant, meaning its value cannot be changed after it has been declared and initialized.

In the given program, 'x' is declared as a constant integer and initialized with the value 5. Then a pointer 'ptrx' is declared as a pointer to a constant integer. The address of 'x' is assigned to 'ptrx', which is perfectly fine.

However, the next line attempts to change the value of 'x' through the pointer 'ptrx' by dereferencing the pointer. This is not allowed because 'x' is a constant.

Therefore, the compiler will throw an error at the line '*ptrx = 10;', and the program will not run. Hence, the correct answer is B: Compilation Error.

This problem has been solved

Similar Questions

What will be the output of the following code?   int main() {int x = 5;int * ptr = &x;*ptr + 1;cout << (*ptr)++;}

What is the output of the following C program ?void main (){const int x = 5;const int *ptrx;ptrx = &x;*ptrx = 10;printf("%d", x);}A5BCompilation ErrorC10DGarbage ValuePrevious

What will be the output of the following code?123456789#include <stdio.h>int main() { int x = 100; int *ptr = &x; *ptr = *ptr / 3 + 5 - *ptr % 2; printf("%d", *ptr); return 0;}

What is the output of the following code?#include <stdio.h>int main() {    int arr[] = {10, 20, 30, 40, 50};    int *ptr = arr;    ptr += 3;    printf("%d\n", *ptr);    return 0;}

What will be the output of the following C code?#include <stdio.h>int x = 0;void main(){int *const ptr = &x;printf("%p\n", ptr);ptr++;printf("%p\n ", ptr);} 0 1Compile time error0xbfd605e8 0xbfd605ec0xbfd605e8 0xbfd605e8Clear ResponseSave & Next

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.