Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shouldn't be this "=+" a syntax error?

Tags:

c++

c

gcc

Recently I was trying to use the following code:

int number = 4;
number += other_variable;//2
...
printf("Number:%d\n",number);//-->6

but I had an error typing and instead I've got this code:

int number = 4;
number =+ other_variable;//2
...
printf("Number:%d\n",number);//-->2

Apparently this compiled with gcc 4.7.3 and gcc 4.4.3 and the result was as a normal assignment operator. The question is: Shouldn't be this a syntax error?

like image 764
KiaMorot Avatar asked Sep 04 '13 14:09

KiaMorot


People also ask

What is my syntax error?

A syntax error occurs when a programmer writes an incorrect line of code. Most syntax errors involve missing punctuation or a misspelled name. If there is a syntax error in a compiled or interpreted programming language, then the code won't work.

Why does Python keep saying invalid syntax?

Some of the most common causes of syntax errors in Python are: Misspelled reserved keywords. Missing quotes. Missing required spaces.

What is syntax error in Python with example?

Syntax errors are mistakes in the use of the Python language, and are analogous to spelling or grammar mistakes in a language like English: for example, the sentence Would you some tea? does not make sense – it is missing a verb. Common Python syntax errors include: leaving out a keyword.


2 Answers

No - this is being parsed as:

number = +other_variable;

i.e. you have assignment and a unary + operator. You're reading it as =+ but it's two separate operators, = and +.

like image 193
Paul R Avatar answered Sep 28 '22 10:09

Paul R


No, it's just a null op.

number = +other_variable;
number = 0 + other_variable;

As a complement to these operations, which negate:

number =- other_variable;
number = -other_variable;
number = 0 - other_variable;
like image 34
user2586804 Avatar answered Sep 28 '22 10:09

user2586804