Discussion
Predict the output of the following source code in C language :
int main( )
{
int i, x[5];
for(i=0; i\(\lt\)5; i++) * (x+i) = i;
for(i=0; i\(\lt\)5; i++) printf("%d", x[i]);
return 0;
}
Answer: A
The program prints the values 0 1 2 3 4.
In C, *(x+i) is the same element as x[i], since an array name decays to a pointer to its first element. The first loop stores 0, 1, 2, 3, 4 in the five elements, and the second loop prints them in order.
Because the format string is "%d" with no separator, the actual output is the digits run together, 01234; option A lists those values. The code is valid, so there is no syntax or semantic error, and every element is assigned before it is printed, so no garbage value appears.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development