2

HERE IS THE CODE

int i = 3
int a=(i*+3);

NOT UNDERSTAND CODE IS

a=(i*+3)

value of a = 9


Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

2 Answers2

3

It is just a bad code formatting. To make the declaration clear rewrite it like

int i = 3;
int a = i * +3;

inserting blanks between tokens. Though the unary plus operator is redundant here.

One more confusing expression

int a = i+++3;

It is the same as

int a = i++ + 3;

From the C Standard (6.4 Lexical elements)

4 If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token.

You can get many such confusing expressions omitting blanks between tokens.

Though in C there are dark corners that you should know.

For example if you have a function declaration like

void f( int x );

then it can be called like

(**********f)( 10 );

that is equivalent to

f( 10 );

Here is a demonstrative program

#include <stdio.h>

void f( int x )
{
    printf( "I'm called with %d stars!\n", x );
}

int main(void) 
{
    ( **********f )( 10 );

    return 0;
}

Its output is

I'm called with 10 stars!
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Oh, I like the [`-->` operator](https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c) as well... – Aconcagua Feb 11 '20 at 13:52
1

Context of usage in C is needed to understand several multi-use symbols, such as the + symbol, which by itself has at least 3 uses found commonly in C expressions:

  • addition operator ( eg int a = 3 + 5; )
  • sign indicating a positive value ( eg int a = +3 - (+3); )
  • and when doubled, a value incrementor. ( for(int a=0; a<limit; a++) )

When used with other multi-use symbols, such as *, determining whether it is sign, operator or incrementor is by context of usage.

Typically in this context you would use an implicit + sign:

int a=(i*3);// i * +3

But it is also legal (but not as readable) to use an explicit sign. So by context of usage in this statement...

int a=(i*+3);// by context '+' is a sign in this case, not an operator

...is simply using an explicit sign for 3, and the two statements are equivalent.

A more readable version:

int a=(i * (+3));

Another similar seemingly confusing scenario, but contextually meaningful:

int func(int *value){

    int a = 10+*value;//by context '*' is de-referencing 'value' not multiplying
    return a;
}

Note: readability could have been improved here by spacing:

int a = 10 + *value;  //spacing is good for readability and removing ambiguity
ryyker
  • 22,849
  • 3
  • 43
  • 87