What is the output of this program?#include <stdio.h> int main() { int arr[5] = {1, 2, 3,4,5}; int p, q, r; p = ++arr[1]; q = arr[1]++; r = arr[p++]; printf("%d, %d, %d", p, q, r); return 0; }
Question
What is the output of this program?#include <stdio.h> int main() { int arr[5] = {1, 2, 3,4,5}; int p, q, r; p = ++arr[1]; q = arr[1]++; r = arr[p++]; printf("%d, %d, %d", p, q, r); return 0; }
🧐 Not the exact question you are looking for?Go ask a question
Solution
The output of this program is "3, 2, 4".
Here is the step-by-step explanation:
- The program declares an integer array
arrwith 5 elements and initializes it with the values {1, 2, 3, 4, 5}. - Three integer variables
p,q, andrare declared. pis assigned the value of++arr[1]. This means that the value ofarr[1]is incremented by 1 before being assigned top. So,pbecomes 3.qis assigned the value ofarr[1]++. This means that the value ofarr[1]is assigned toqand then incremented by 1. So,qbecomes 2.ris assigned the value ofarr[p++]. This means that the value ofarr[p]is assigned torand thenpis incremented by 1. Sincepis 3,arr[3]is 4, sorbecomes 4.- The program prints the values of
p,q, andrusing theprintffunction. So, the output is "3, 2, 4".
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.