-3
#include <stdio.h>

int main() {
    int a,b;
    a = -3--3;
    b = -3--(-3);
    printf("a=%d b=%d", a,b);
}

This program is an assignment given to us to find output. But it shows expression is not assignable

However, my other classmates got the output.

a = 0, b = -6

I don't understand how because the program feels wrong to me.

Mazino
  • 316
  • 1
  • 6
  • 18
  • 1
    A compiler can be used to tell you if a program is syntactically correct, and if not, what and where the problems are. – Paul Hankin Jan 26 '21 at 13:12
  • 2
    What compiler shows that error, where? [Edit] to explain how/where you compile and quote the message in full including the line/column numbers implicated. – underscore_d Jan 26 '21 at 13:12
  • 3
    Use some recent [GCC](http://gcc.gnu.org/) as `gcc -Wall -Wextra -g` – Basile Starynkevitch Jan 26 '21 at 13:13
  • 4
    C tokenisation follows the [maximal munch rule](https://en.wikipedia.org/wiki/Maximal_munch). The expression `a=-3--3;` is tokenized as `a`, `=`, `-`, `3`, `--`, `3`, `;` ... and the token `--` cannot be syntactically associated to either it's left or right token. – pmg Jan 26 '21 at 13:18
  • 3
    This was incorrectly closed. Should be closed as dupe to for example this fairly canonical "maximal munch" duplicate https://stackoverflow.com/questions/5341202/why-doesnt-ab-work. – Lundin Jan 26 '21 at 13:25

1 Answers1

8

I'm guessing its just an issue of spacing your operators:

#include <stdio.h>

int main() {
    int a,b;
    a = -3 - -3;
    b = -3 - -(-3);
    printf("a=%d b=%d", a,b);
}

This gives your desired output:

a = 0, b = -6

the -- in your code is considered as a decrement operator instead of a negative sign

itsDV7
  • 854
  • 5
  • 12