• Advertisement
Sign in to follow this  

()different from (())

This topic is 3971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement