Why should I wrap variable name inside parenthesis in C programming?

why should I use parenthesis to wrap the variable name

asked Jul 17, 2019 at 12:33 Md. A. Barik Md. A. Barik 447 6 6 silver badges 12 12 bronze badges Is this from some particularly old C code? There's no type given for the parameter a . Commented Jul 17, 2019 at 12:36 You have two variables in your code, both "wrapped inside parentheses". Which one do you mean? Commented Jul 17, 2019 at 12:37

@Md.A.Barik That's just the way the C language works. The variable in a function is not the same as the one outside it.

Commented Jul 17, 2019 at 12:43

@eyl327 Actually, there's a difference: the first one is ugly, and will make maintainers of the code lose some seconds thinking about why those parentheses are there and who put them there :-)

Commented Jul 17, 2019 at 12:54

In the code you have shown, there is no function call, so you are not passing the variable in the function as a parameter. You are (attempting to) define a function with a parameter with the same name as a variable. (The definition should be int check(int a) < . >.) The parameter and variable are different objects even though they have the same name. The variable a is hidden by parameter a inside the check function body.

Commented Jul 17, 2019 at 12:56

2 Answers 2

So why we need to declared the type like : int check(int a)

From this it is clear you're referring to the function and not to the variable itself. That part of the code is defining a function.

So, let's separate the two things in your code:

int (a); 

Declares variable a . As @eyl327 stated:

There is no difference between int (a); and int a;

So that int could be called whatever you wanted; that it now has the same name as the variable used in the function definition is just coincidence. It could be, for example, int my_integer; .

int check(a)

Here you define a function which receives an integer and which will return modulo 2 of that int (you can read this if you don't know what that means).

It is missing the type of the variable which receives the function. It should be, instead:

int check(int a)

Now, I think it is possible that the combination used in your code is valid, but I've never seen it before, and if you're learning now, I'd suggest you get familiar with the traditional way of defining variables and functions.

Hope this helps!

answered Jul 17, 2019 at 12:56 1,478 1 1 gold badge 14 14 silver badges 24 24 bronze badges Thanks for pointing it out; I was just editing the answer when your notification arrived. Commented Jul 17, 2019 at 13:02

From what I see, you are asking about the variable declaration. The function is using proper syntax except for the type of a e.g. int check(int a) <. >.

For the variable declaration, the parenthesis act as mathematical parenthesis as in they dictate the order to determine the variable type. In your case, this doesn't change the variable (it is still int).

However, you can have more complex types that require parenthesis. For example:

int (*aptr)[3]; //aptr is a pointer to an array of 3 integers int *bptr[3]; //bptr is an array of 3 integer pointers 

These parenthesis are necessary as the order matters. In this case, brackets ([]) take precedence. You can keep going with more complex types. But the idea is that parenthesis are used to set an "order of operations" when determining the type of a variable. You can almost always add redundant parenthesis to be verbose, but it is not necessary.