arrow
arrow
arrow
Consider the following C program: #include <stdio.h> int main() { int i = 5, j = 10, k = 15; printf("%d", sizeof(k /= i + j));
Question



Consider the following C program:
#include <stdio.h>
int main()
{
int i = 5, j = 10, k = 15;
printf("%d", sizeof(k /= i + j));
printf("%d", k);
return 0;
}
Assume the size of an integer is 4 bytes. What is the output of the above program?

A.

2 1

B.

4 1

C.

4 15

D.

More than one of the above

E.

None of the above

Correct option is C


To determine the output of the given C program, let’s analyze it step by step.
Step 1: Understanding sizeof(k /= i + j)
1. Expression Evaluation:
· i = 5, j = 10, k = 15
· The expression inside sizeof:
k /= i + j
· First, evaluate i + j:
5 + 10 = 15
· Now, k /= 15:
k = k / 15 = 15 / 15 = 1
2. Behavior of sizeof Operator:
· sizeof(k /= i + j) evaluates the type of the expression, not the result.
· Since k is an int and integer division results in an int, sizeof(int) = 4 bytes.
Step 2: Execution of printf Statements
1. First printf Statement:
printf("%d", sizeof(k /= i + j));
· Outputs 4 because sizeof(int) = 4 (does not execute k /= 15).
2. Second printf Statement:
printf("%d", k);
· Since sizeof does not evaluate k /= i + j, the value of k remains 15.
· Outputs 15.
Final Output:
4 15
Thus, option (c) is correct.
Important Key Points:
1. sizeof Operator Behavior:
· sizeof only evaluates the data type, not the expression itself.
· sizeof(k /= i + j) does not modify k because sizeof is evaluated at compile-time.
2. Integer Division (/=) Does Not Execute in sizeof: The sizeof operator does not execute expressions inside it.
3. Output Justification:
· sizeof(k /= i + j) → sizeof(int) = 4
· k remains 15 since sizeof does not execute k /= i + j.
4. Example of a Correct Execution of k /= i + j:
int main() {
int i = 5, j = 10, k = 15;
k /= i + j; // This line executes
printf("%d", k); // Output: 1
return 0;
}
· In this case, k = 1 because the expression is executed normally.
Knowledge Booster:
· sizeof Operator:
· Evaluates the type, not the value of an expression.
· sizeof(int) = 4 (on most systems).
· Integer Arithmetic in C: 15 / 15 = 1 (integer division).
· Expression Evaluation in sizeof: sizeof(x = 10 + 5) does not modify x.

test-prime-package

Access ‘BPSC TRE (11-12)’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
192k+ students have already unlocked exclusive benefits with Test Prime!

Free Tests

Free
Must Attempt

DSSSB TGT COMPUTER SCIENCE PYP (01 August 2021 Shift-2)

languageIcon English
  • pdpQsnIcon200 Questions
  • pdpsheetsIcon200 Marks
  • timerIcon120 Mins
languageIcon English
Free
Must Attempt

English Subject Test 1

languageIcon English
  • pdpQsnIcon20 Questions
  • pdpsheetsIcon20 Marks
  • timerIcon20 Mins
languageIcon English
Free
Must Attempt

Indian Geography Chapter Test

languageIcon English
  • pdpQsnIcon10 Questions
  • pdpsheetsIcon10 Marks
  • timerIcon10 Mins
languageIcon English