Search code examples
c++operatorsunary-operator

Using operator '+' and '*' to statements in C++


in C++ when i run this code :

void main()
{
  2;
  +
  3;
}

there is no error but when i run this code :

void main()
{
  2;
  *
  3;
}

there is this error:

main.cpp:5:3: error: invalid type argument of unary ‘*’ (have ‘int’)
    5 |   3;
      |   ^

please someone explain it thanks


Solution

  • Since C++ is free style language. So space doesn't matter.

    In first case it becomes +3 which is a valid statement. Read Here about +

    But in second case it becomes *3 which is invalid as compiler thinks you are trying to dereference 3 which is invalid.