Wikipedia:Reference desk/Archives/Computing/2016 April 21

Computing desk
< April 20 << Mar | April | May >> April 22 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


April 21

edit

Unary Operator in C

edit

What is the output of following code?

#include <stdio.h>
void main(){
 int a=5,v;
 v=(++a)*(++a)+(++a);
 printf("%d , %d",v,a);
}

When I compiled it , I get different result according to compiler.

 In Turbo C ,I get 72,8
 In Dev c++ and Code::Blocks (Based on GNU GCC), I get 57,8
 In C4Droid I get 50,8 

Which is correct and how? Can you explain this for me? Thanks — Preceding unsigned comment added by Amrit Ghimire Ranjit (talkcontribs) 04:52, 21 April 2016 (UTC)[reply]

They are all equally correct. You shouldn't increment "a" several times within a statement. If an object is modified more than once in a statement (technically between two sequence points), behavior is undefined. Undefined means any result is allowed, including giving a random number result, crashing the program, or setting your computer on fire. 91.155.193.199 (talk) 06:43, 21 April 2016 (UTC)[reply]
To explain what's happend, the evaluation sequences are:
           v=(++a)*(++a)+(++a);
Turbo C        1  4  2  5  3      : (8 * 8) + 8 = 72
GNU C          1  3  2  5  4      : (7 * 7) + 8 = 57
C4Droid        1  2  3  5  4      : (6 * 7) + 8 = 50
The C4Droid sequence might seem the obvious way of doing it, but the other two aren't wrong, just examples of what "undefined behaviour" can mean. Tevildo (talk) 07:21, 21 April 2016 (UTC)[reply]
(ec) The expression given has undefined behavior as defined in the C standard. The C standard defines a concept called sequence points, which for the purposes of this discussion can be thought of as the start and end of the statement in question. Performing more than one update to a or accessing a elsewhere in the same expression where it is updated are both cause for considering the operation to have undefined behavior. With undefined behavior, all bets are off. There is not even a guarantee that the results make any sense at all. A google of sequence point yields much further discussion. -- Tom N talk/contrib 06:45, 21 April 2016 (UTC)[reply]
Small clarification. A sequence point is a point in time, not a place in the program. The relevant sequence points are before and after the statement is executed. --69.159.61.172 (talk) 12:04, 21 April 2016 (UTC)[reply]
Well, the concept may be one of execution order (hence time), but in C and C++, sequence points correspond to particular constructs in a program. See Sequence point. --Stephan Schulz (talk) 15:20, 22 April 2016 (UTC)[reply]

Actually, even void main() is non-standard and might induce undefined behaviour by itself. main() should be defined as int main() or int main(int argc, char **argv). You still don't need to return anything from main(), a missing return statement from main() is taken as return 0; meaning success. JIP | Talk 21:04, 22 April 2016 (UTC)[reply]

Does Google Cloud Messaging work for endpoint devices in China? Many Google services are blocked in China, but some are not. I'd like to know whether Google Cloud Messaging is among the blocked services or not. Johnson&Johnson&Son (talk) 06:08, 21 April 2016 (UTC)[reply]

I haven't seen Google Cloud Messaging that isn't dependent on Google Play Services. The entire Google Play Services SDK is blocked in China, so any apps that are dependent on it won't work. 209.149.115.199 (talk) 14:33, 21 April 2016 (UTC)[reply]