Why is the asterisk before the variable name, rather than after the type?

WBlasko picture WBlasko · Dec 29, 2008 · Viewed 59.7k times · Source

Why do most C programmers name variables like this:

int *myVariable;

rather than like this:

int* myVariable;

Both are valid. It seems to me that the asterisk is a part of the type, not a part of the variable name. Can anyone explain this logic?

Answer

luiscubal picture luiscubal · Dec 29, 2008

They are EXACTLY equivalent. However, in

int *myVariable, myVariable2;

It seems obvious that myVariable has type int*, while myVariable2 has type int. In

int* myVariable, myVariable2;

it may seem obvious that both are of type int*, but that is not correct as myVariable2 has type int.

Therefore, the first programming style is more intuitive.