0

the question Translation: "After running the code below, what is the value of k?'

This question was from my friend's homework(we were senior highs). He didn't know how to deal with this, so he asked me and then I tried to test this on my computer(Debian 10, gcc-10.2.1). The result is (A) , which means the value of k is 6. I reflected on the if statement but still had no idea. It utterly made no sense - especially the syntax(who will even write code in that way?) . That's why I would like to ask this question. Perhaps that's a bug I haven't met before.

The C code:

#include <stdio.h>

int main() {

    int i, j, k = 0;

    for ( i = 0 ; i <= 2 ; ++i ) {
        for ( j = 0 ; j <= 2 ; ++j ) {
            
            if ( i != 1i != 2 && j != 1 ) {
                k = k + j;
            }
        }
    }

    printf("%d\n", k);
        
    return 0;
}

Why can the following if statement work?

if ( i != 1i != 2 && j != 1 )

And what's the value of 1i?

wuj-jay
  • 17
  • 3
  • And please [do not upload images of code](//meta.stackoverflow.com/a/285557/10669875) when asking a question. – wovano Oct 31 '22 at 11:17
  • 4
    I reckon you are surprised that `1i` isn't rejected? – StoryTeller - Unslander Monica Oct 31 '22 at 11:18
  • @chris It is a [GNU extension for writing imaginary literals](https://stackoverflow.com/a/33617143/12002570). See duplicate: [What does the integer suffix J mean?](https://stackoverflow.com/a/33617143/12002570) – Jason Oct 31 '22 at 11:22
  • 3
    @chris I think the homework is about operator precedence; e.g. whether the expression is `(i != 1i) != 2` or `i != (1i != 2)` – ensc Oct 31 '22 at 11:25
  • What is the value of ```1i``` ? – wuj-jay Oct 31 '22 at 11:29
  • 1
    @wuj-jay I've added more dupes in the dupe list. For examole, [Why can't we do three-way comparison in C++?](https://stackoverflow.com/questions/56526264/why-cant-we-do-three-way-comparison-in-c) and [Comparing a variable to a range of values](https://stackoverflow.com/questions/3830644/comparing-a-variable-to-a-range-of-values). – Jason Oct 31 '22 at 11:29
  • @wuj-jay That's what [this duplicate](https://stackoverflow.com/a/33617143/12002570) explains. It is a [GNU extension for writing imaginary literals](https://stackoverflow.com/a/33617143/12002570). Kindly read the duplicates. – Jason Oct 31 '22 at 11:30
  • @wuj-jay 1 as an imaginary number, https://en.wikipedia.org/wiki/Imaginary_number – rdtsc Oct 31 '22 at 11:31
  • 1
    @wuj-jay Alas, since the question was _closed_, there is no very good way to explain everything wrong with the exam’s question. The `1i`’s value is not really relevant except to say it is a non-integer not equal to `i`, producing a boolean value of `true`. (Which is _then_ used in the next `!=` part of the expression.) – Dúthomhas Oct 31 '22 at 11:32
  • @Revolver_Ocelot, I should have taken a closer look at the choices before defaulting to `&&`, but `||` would still produce 8 and be a pretty normal precedence question compared to a GNU extension for complex numbers that you'd have no reason to teach in a senior high class. – chris Oct 31 '22 at 11:33
  • 1
    Multiple choice is missing "Code isn't run, because it is rejected in review" – Caleth Oct 31 '22 at 11:34
  • I have to admit, this is the first time in many, many moons that I have really enjoyed a horrid test question. – Dúthomhas Oct 31 '22 at 11:36
  • Really appreciate it to you guys who answered my dumb question. I didn't know it's about the compiler extension. – wuj-jay Oct 31 '22 at 11:54

0 Answers0