1
#include <stdio.h>

int main() {
    int d[][3][2] = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    int i = -1;
    int j;
    j = d[i++][++i][++i];
    printf("%d", j);
    return 0;
}

How does i not equal to -1 in this code?

j = d[i++][++i][++i];
      ^^^^

When you running this code, you may be able to see the result as 4.

I just wondered about how i does not equal -1 in the above case. Thanks.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 2
    C or C++? Choose one, they have different rules. – HolyBlackCat Apr 17 '22 at 08:09
  • Did you overlook the warnings `"missing braces around initializer"` and `"operation on ‘i’ may be undefined"`?? – David C. Rankin Apr 17 '22 at 08:14
  • @PaulHankin It looks same but, I think there's one difference: To get '4' in this array, we need `d[0][0][0]`, right? In this case, how does `[i++][++i][++i]` equals `[0][0][0]`? Is C parses the operators from right to left or anything else? Thanks. – lambdacalculusser Apr 17 '22 at 08:19
  • The behavior is **undefined**. Anything can happen. C parses the expression from left to right and applies the precedence and associativity rules to determine how the expression should be evaluated, but this does not define the order of evaluation of subexpressions, nor the order of application of side effects, if any. – chqrlie Apr 17 '22 at 08:24

1 Answers1

0

The behavior is undefined because i is modified multiple times in the same expression without sequence points. The side effect can occur in any order or worse... nothing can be predicted regarding the value of the expression nor the final value of i.

Note that this answer applies to c++ too, but not to java nor javascript or some other languages with the increment operator ++, as they have different semantics regarding the order of evaluation and the application of side effects. Even in languages where the behavior is defined, it is bad practice to use such confusing expressions.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • This code comes from Turkish olympiad which has C questions that you need to interpret the code by hand. Thank you for answering by the way! – lambdacalculusser Apr 17 '22 at 08:22
  • Unfortunately questions has not UB option but runtime and compile error has. – lambdacalculusser Apr 17 '22 at 08:29
  • @lambdacalculusser: the code can produce warnings at compile time depending on compiler quality and warning level, but not errors unless you make warnings errors by using `-Wall -Werror`. Undefined behavior is not the same as runtime error. UB may cause runtime errors or produce surprising behavior as you observe. If the question is about the C language, it should allow you to check undefined behavior, otherwise the whole competition has undefined behavior :) – chqrlie Apr 17 '22 at 08:36
  • Year of 2016, 24th TUBITAK Computer Olympiad. Q42. – lambdacalculusser Apr 17 '22 at 09:51
  • Hmm. Check this one for past exams. This one is 24th: https://nd.nl.tab.digital/s/RWXwtAFkmd35yt6?dir=undefined&path=%2F%C3%87%C4%B1km%C4%B1%C5%9F%201.%20a%C5%9Fama%20sorular%C4%B1%2FLise%2F2016-2019&openfile=81275313 – lambdacalculusser Apr 17 '22 at 19:55
  • The question on page 24 (problem 41) is *Yukarıdaki program için aşağıdakilerden hangisi doğrudur ?* (Which of the following is true for the above program?) The answer **E) Hiçbiri** (None) seems appropriate. The posted program is actually page 25 (problem 42) and the answer to the same question is also **E) Hiçbiri** (None). – chqrlie Apr 17 '22 at 20:12
  • The next problem also has undefined behavior, commanding answer E) Hiçbiri (None). – chqrlie Apr 17 '22 at 20:18
  • There's so much UBs. You might have seen but there's also a solution PDF for 24th: https://nd.nl.tab.digital/s/RWXwtAFkmd35yt6?dir=undefined&path=%2F%C3%87%C4%B1km%C4%B1%C5%9F%201.%20a%C5%9Fama%20sorular%C4%B1%2FLise%2F%C3%87%C3%B6z%C3%BCmler%2F2014-2019&openfile=84017969 – lambdacalculusser Apr 17 '22 at 20:26
  • Well this is a total failure: answers to 41, 42 and 43 are ludicrously incorrect. The C questions focus mostly on operator precedence, something most programmers do not fully master because there are too many levels and some of them are counter-intuitive, but mastering them without the classic cheat sheet is not a necessary feat to be a great programmer. Spotting and avoiding undefined behavior is more important. Are these the official solutions? Is there an English version? – chqrlie Apr 17 '22 at 20:46
  • These solutions are official whereas taken from https://bilimolimpiyatlari.tubitak.gov.tr/. – lambdacalculusser Apr 17 '22 at 20:54
  • Another note regarding these C problems: the solutions for other problems seem correct, albeit not always explained correctly, but except for the one that produces a compiler error (taking the address of a `register` variable), they actually all have undefined behavior when calling `printf` without a proper declaration of this function (they do not include ``). – chqrlie Apr 17 '22 at 21:07
  • Okay, also a note: They assumes as required libraries are preincluded, "Gerekli tüm başlık (header) dosyalarının verilen programa dahil edildiğini varsayınız.". – lambdacalculusser Apr 17 '22 at 21:19