1

Why is this piece of C code valid?

int main() {
    int x = 0, y = 0;

    x * y; // See this?!
}

The most ubiquitous examples that explain the need for back-tracking in parser often shows this. And then goes on to say that depending on what x is, the meaning of * will change. For some time, I believed it was just a way of explaining until I compiled it and found to my surprise, it compiles and works!

Why is a multiplication without an LHS valid in C and what is it used for?

Edit This confusion is because, the same piece of code won't compile in Java. It seems Java compiler tries to prevent operations without LHS.

enter image description here

Unmanned Player
  • 1,109
  • 9
  • 30
  • 3
    Not all valid code is useful. – user2357112 Jan 31 '17 at 21:35
  • No one would spend years doing something useless. No? – Unmanned Player Jan 31 '17 at 21:38
  • 1
    Allowing it makes the grammar simpler. Also, it's useful in expression statements (a gcc/clang/tcc extension). – Petr Skocik Jan 31 '17 at 21:40
  • 1
    Did you spend years writing this? Do you know anyone who has? – user2357112 Jan 31 '17 at 21:42
  • Considering that assignment is an expression returning a value, requiring that every expression's value be used would be *difficult*. – EOF Jan 31 '17 at 21:42
  • 1
    Related: http://stackoverflow.com/q/41942049/2615940 – skrrgwasme Jan 31 '17 at 21:43
  • @user2357112: Expressions causing name lookup and then change the meaning of `*` isn't something you think off hand and put it in to your compiler for fun. Reckon, it ought to have some purpose. Hence the question. – Unmanned Player Jan 31 '17 at 21:49
  • 1
    The useful and useless ways to use `*` weren't implemented separately. No one made any special effort to put in useless uses of it. The useless uses exist because it was simpler to have them than to not. – user2357112 Jan 31 '17 at 21:56
  • Possible duplicate of [C - What does x+1 means without assignment?](http://stackoverflow.com/questions/41942049/c-what-does-x1-means-without-assignment) – Julien Lopez Jan 31 '17 at 22:19
  • The value of that expression is used to be assigned to an lvalue. It is valid because it has to be, otherwise it couldn't be assigned to anything. – 2501 Jan 31 '17 at 22:35
  • @JulienLopez I wanted an explanation from grammar point-of-view and a need to even have such a statement as valid. Not what it means, like in the possible duplicate. – Unmanned Player Jan 31 '17 at 22:58

2 Answers2

4

It's not used for anything.

The reason that it's valid is because it makes the language simpler. An expression is a valid statement all by itself, so you can do things like this:

f(x, y);

Even if the definition of f is:

int f(int x, int y) {
    return x * y;
}

There's no point to that either, but figuring out exactly which expressions "make sense" would make the language more complicated. It's good enough that the compiler warns us:

$ cc -c -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:3:7: warning: statement with no effect [-Wunused-value]
     x * y;
     ~~^~~
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
3

This is referred to as an expression statement.

From section 6.8.3 of the C standard:

1
expression-statement:

expressionopt ;

2 The expression in an expression statement is evaluated as a void expression for its side effects.

In your case, the expression is a multiplication. This statement has no side effect, so it has no effect at all.

If on the other hand you had this:

i++;

That's an expression that does have a side effect.

This is also considered an expression:

a = b * c;

In this case, the side effect is the assignment operator.

dbush
  • 205,898
  • 23
  • 218
  • 273