Interview Question


Country: -




Comment hidden because of low score. Click to expand.
1
of 3 vote

i is increased multiple times before the next sequence point, so it is undefined behavior. On a different platform /compiler the result could be totally different

- Anonymous October 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Downvoter, you're wrong. Please read the standard.

- Anonymous October 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correct Ans: Undefined Behaviour
Explanation: Note that a function call f(a,b,c) is not a use of the comma operator and the order of evaluation for a, b, and c is unspecified.
Source: en.wikipedia.org/wiki/Sequence_point

- monish.gupta1 November 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Well this statement is the violation of point rule and hence the behavior is undefined.

Different compilation provide different rules and the output cannot be determined to the accuracy.

- hprem991 November 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

me running print value is 322

- recoil.rahul October 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The right is 3 2 2 i think, the expression is evaluated right to left.
so ++i becomes 2
I++ remains 2
i will be 3.
Then the print statement will print values as 3 2 2.

- compmanic November 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Forgot to mention, printf statements like above are like stack operations so when the compiler sees this expression, it reads each value and pushes into the stack (push operation). When it needs to do the evaluation, it pops out each of the values from the stack,hence the right to left evaluation.

- compmanic November 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

recoil.rahul,
What's your platform?
And don't you all think it has got nothing to do with the precedence of i++ over ++i ?

- DH November 01, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More