-4
#include <stdio.h>

int main(void) {    
    int i= 1, j = 1;
    for (; j; printf("%d  %d",i, j))
        j = i++ <= 5;
    return 0;
}

output-

2  13  14  15  16  17  0

Can anyone explain what is happening inside the loop? This question was asked in the interview process.

Basti
  • 373
  • 2
  • 6
  • 12
Ruth-The Glix
  • 523
  • 1
  • 4
  • 7
  • 8
    *"This question is asked by Zoho Cooperation in the interview process"* - To be frank, if you need help answering it, you better find a good source of learning C in a structured manner. Asking for a crash course about *this code* from strangers on SO won't make you a good candidate to pass the interview. They'll just weed you out with something else, possibly harder. – StoryTeller - Unslander Monica Mar 05 '18 at 10:14
  • See this link : https://wandbox.org/permlink/kusAhEDsuivV8O0N – msc Mar 05 '18 at 10:15
  • 4
    Oh, and if that's what they use to screen candidates, you don't want to work there. – StoryTeller - Unslander Monica Mar 05 '18 at 10:15
  • 1
    @StoryTeller Well it is pretty clever to use questions like these during interview to weed out the quacks. I'd like to include a question about `i=i++;` during interviews, just to tell if the candidate knows that such code is fishy, or at least provoke a reaction "who would write such crap code". Anyone who starts to ponder the inner workings of `i=++` instead of questioning the specification, is a code monkey, not an engineer. Though of course if a company uses questions about obfuscated code and actually expect you to tell what it does, then that's a bad company indeed. – Lundin Mar 05 '18 at 10:34
  • @Lundin - I agree with you on all counts. And that's the beauty of it. You can weed out the quacks with a single statement. No need for monstrosities like this. Proves that both the company and the candidate are worth their salt. – StoryTeller - Unslander Monica Mar 05 '18 at 10:35
  • There's a proposal in C++ to define the behaviour of `i=i++;` I hope all interviewers are well-versed on that. – Bathsheba Mar 05 '18 at 10:36
  • @Bathsheba The order of evaluation of the = operator was already specified in C++11 (unlike C11). And since the two operands of the = are therefore sequenced, I believe that code is well-defined from C++11 and beyond? – Lundin Mar 05 '18 at 10:40
  • I don't think it is, but this is the kind of thing that @StoryTeller knows. Methinks it's C++20. – Bathsheba Mar 05 '18 at 10:41
  • It isn't well defined prior to C++17. A single sentence, *"The right operand is sequenced before the left operand."*, is present in [C++17](https://timsong-cpp.github.io/cppwp/n4659/expr.ass#1) and absent in [C++14](https://timsong-cpp.github.io/cppwp/n4140/expr.ass#1) – StoryTeller - Unslander Monica Mar 05 '18 at 10:44
  • Uh right, it isn't well-defined but I believe `i=++i;` is. https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points/4183735#4183735 – Lundin Mar 05 '18 at 10:48
  • Yeah, pre-increment was on surer footing since C++11. Another interesting thing to poke candidates with – StoryTeller - Unslander Monica Mar 05 '18 at 10:52

5 Answers5

3

The code is equivalent to:

int main(void) {    
  int i=1,j=1;
  while (j != 0) {
    j = (i<=5);
    i = i+1;
    printf("%d  %d",i,j);
  }
  return 0;
}

I think that its meaning and the output is evident now. Note that printf does not print any separator after the second number. It means that the two digit "numbers" in the output are printed by two successive invocations of printf each. If you use printf("%d %d; ",i,j); instead of printf("%d %d",i,j); the output will be:

2  1;  3  1;  4  1;  5  1;  6  1;  7  0;
Marian
  • 7,402
  • 2
  • 22
  • 34
1

Your code is the same as

#include <stdio.h>

int main() {
    int i = 1, j = 1;

    while (j > 0) {
        if (i <= 5) {
            j = 1;
        } else {
            j = 0; 
        }

        i = i + 1;
        printf("%d  %d",i, j);
    }
}

It should now be easy to understand.

Basti
  • 373
  • 2
  • 6
  • 12
0

Initially, i and j are 1 so condition for loop will be true and it will enter into the loop. At j = i++<=5; statement first it will execute conditional operation and assign result of conditional operation (true = 1, or, false = 0) to j and increment i by one. After this, it will execute print statement from the loop which will print value for i (which is now incremented by one is 2) and j (will be 1 as the condition is true). This will continue until the i<=5 condition is true. Whenever i becomes greater than 5 j will be 0, breaking the loop.

WedaPashi
  • 3,561
  • 26
  • 42
0

De-obfuscated, the code is equivalent to this:

for(int i=1; i<7; i++)
{
  printf("%d\t%d\n", i+1, (i+1) < 7);
}

What such a loop would be needed for, I have no idea.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

To make the code and its output more clear rewrite the program the following way

#include <stdio.h>

int main(void) 
{
    for ( int i = 1, j = 1; j != 0; )
    {
        j = i++ <= 5;
        printf("%d  %d\n", i, j );
    }

    return 0;
}

The program output is

2  1
3  1
4  1
5  1
6  1
7  0

As the variable j is calculated as a result of this condtion

i++ <= 5

then it will be always equal to 1 except when i will be equal to 6 because for this value of i the condition is evaluted to false that is to 0 and this value is assigned to j.

So the last iteration of the loop is when i is equal to 6. In this case after this statement

j = i++ <= 5;

i will be equal to 7 and j will be equal to 0. These values are outputted in the last iteration of the loop.

7  0
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335