12

I am new to C++, I see following syntax in c++ to initialize variable.

int(i)=1;

Then, I have compiled in G++ compiler and compiler did not give any error or warning.

So, What does mean int(i)=1; in C and C++?

Also, I have tested in C, I thought, the C compiler give an error but it's also working fine.

  • 1
    why do you add the [tag:c] tag? C and C++ are very different languages – phuclv Aug 26 '17 at 06:12
  • 1
    @LưuVĩnhPhúc Because it is also working fine in C. –  Aug 26 '17 at 06:13
  • @Mahendra Nonetheless, something working in both languages *could* have very different behaviour and reasons. In this case, it's fine, but keep in mind that they are really different. – deviantfan Aug 26 '17 at 06:19
  • Why did you expect an lvalue error? i is an lvalue. – deviantfan Aug 26 '17 at 06:19
  • 1
    @Mahendra This is a very different code. i is an lvalue, foo() is not. And the crucial differences are not the parenthesis (well, sort of. They are the reason for the function call). Please look up what lvalues are. – deviantfan Aug 26 '17 at 06:25

1 Answers1

15

It's basically a strange way to write

int i = 1;

Nothing to worry about.

Sometimes, parenthesis around the variable name are necessary in defintions (eg. pointer to functions), and there is no reason to prohibit them for other cases, so it's allowed without any deeper reason.
Maythe the author didn't like spaces (such people exist).

deviantfan
  • 11,268
  • 3
  • 32
  • 49