The Gateway to Computer Science Excellence
+2 votes
155 views

Consider the following program:

The value printed by the above program is:

1. 20

2. 30

3. 40

4. 50

I think it will be undefined behaviour because of:
arr[count++] = incr();

But I am not sure. need to confirm.

in Programming by Boss (17.8k points)
edited by | 155 views
0
i also thought so but it works fine
0
I dont think so i think it will be evaluated as arr[count++] evaluated after incr() evaluated.
0

@Anu see Example 2 at the end of this selected answer: https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points

0
Rishabh a[count++] = ++count ; has confusion which to acsess 1st , so undefined behaviour.
But in given program we force ++count to evaluate 1st .
0
So, does that mean if we have a function call it will be evaluated first?

Do you have any source to read this?
0
Wouldn't it be 30?
0
@kiran Yes answer will be 30.
And arr[0] = 2
0
finally what will be the ans ??coz i am also confused
0

@Rishabh Gupta  shouldn't it be arr[0] = 1 as it will store the value returned by the function call incr() which is 1 and then count++ makes value of count as 2 to be used in the next printf statment?

Please log in or register to answer this question.

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,324 answers
198,405 comments
105,169 users