()different from (())

Started by
1 comment, last by bubu LV 17 years ago
not work. if(GetAsyncKeyState( (VK_SPACE) & 0x8000) ) { MessageBox(0,"space pressed",0,0); break; } work as intent ,when press spacebar, MessageBox(0,"space pressed",0,0) run. if(GetAsyncKeyState(VK_SPACE) & 0x8000) { MessageBox(0,"space pressed",0,0); break; }
Advertisement
Quote:Original post by 3dcgmodeling
not work.
if(GetAsyncKeyState( (VK_SPACE) & 0x8000) )
{
MessageBox(0,"space pressed",0,0);
break;
}

work as intent ,when press spacebar, MessageBox(0,"space pressed",0,0) run.

if(GetAsyncKeyState(VK_SPACE) & 0x8000)
{
MessageBox(0,"space pressed",0,0);
break;
}
Among other things, parentheses in C/C++ are used in function calls (to enclose arguments), and to control the order in which terms are evaluated.

Note first of all that the parentheses around VK_SPACE are unnecessary; removing them might make the example more clear.

In your first example, the argument to GetAsyncKeyState() is 'VK_SPACE & 0x8000'. This is incorrect; GetAsyncKeyState() accepts a single integer value specifying a key, so by &'ing VK_SPACE with 0x8000 you're specifying a key other than VK_SPACE (or perhaps just submitting an invalid value).

In the second example, the argument to GetAsyncKeyState() is VK_SPACE (which is correct). The return value of this function call is then &'ed with 0x8000 which (according to the docs) tells you whether the key is currently down. The resulting integer value is then converted implicitly to a Boolean value for use in the 'if' conditional.
Also similar example from mathematics: f((x) + 8) is not always same as f(x) + 8, where f(x) is some function (for example take x^2).

This topic is closed to new replies.

Advertisement