+ 1

Can somebody explain how output is C A?

char a[] = { 'A', 'B', 'C', 'D' }; char* ppp = &a[0]; *ppp++; printf("%c %c ", *++ppp, --*ppp);

8th Jun 2019, 2:52 PM
Shubham Tandale
Shubham Tandale - avatar
4 Réponses
+ 4
Kinshuk Vasisht The order in which the arguments are evaluated isn't defined though. All we know is that ppp was increased once and decreased once when the sequence point (;) is reached. We can't tell if the first or second argument of the printf statement is evaluated first because every compiler can "decide" how to evaluate the statement. This should be a case of undefined behavior IMO (also, compare the second to last example in the link I posted)
8th Jun 2019, 5:02 PM
Anna
Anna - avatar
+ 2
Anna I don't think the output should be considered undefined, as no same lvalue is being modified twice in any expression in the fragment posted above, and a valid explanation exists for the output, as opposed to the statements discussed in the posted link. Edit: Yes, you are right. Sorry for the inconvenience.
8th Jun 2019, 4:56 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
The Cowboy In line 3, or *ppp++;, the ++ operator takes precedence over * and so ppp points to the successive address or to the address of a[1] in the array rather than having the value at the current address it points to or the value of a[0] incremented. The same happens in the next line and ppp after the printf statement points to a[2]. You can verify the same by printing the array and the difference (ppp - a).
8th Jun 2019, 4:52 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar