What will be the output of the following program : void main() { int val=5; void ptr; *(int *)ptr=5; val=ptr; printf("%d %d",(int )val,(int *)ptr); }

  1. Compile-Time Error

  2. Unpredictable

  3. 5 5

  4. None of the above


Correct Option: C

AI Explanation

To answer this question, let's go through the given program step by step:

  1. The program starts by declaring a variable val and initializing it with the value 5.
  2. Then, a void pointer ptr is declared.
  3. The next line *(int *)ptr=5; tries to assign the value 5 to the memory location pointed by ptr. However, since ptr is not pointing to any valid memory location, this line will result in undefined behavior. This is because ptr is uninitialized and pointing to an unknown memory location.
  4. Next, the line val=ptr; tries to assign the value of ptr to val. Since ptr is uninitialized, this will also result in undefined behavior.
  5. Finally, the line printf("%d %d",*(int *)val,*(int *)ptr); tries to print the values at the memory locations pointed by val and ptr. However, since both val and ptr are uninitialized and not pointing to valid memory locations, this will also result in undefined behavior.

Therefore, the correct answer is option A) Compile-Time Error.

Find more quizzes: