+ 3

Please, Explain it...

#include <stdio.h> int main() { char arr[] = {'A','B','C','D'}; char *p = arr; *p++; printf("%c %c",*++p,--*p); return 0; } //Output is : C A // How ?

10th May 2019, 11:18 AM
Sp Maurya
Sp Maurya - avatar
6 odpowiedzi
+ 5
10th May 2019, 11:48 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
+ 5
*p++ increases the pointer to arr[1] (++ is quicker than *). I think the arguments in the printf statement are evaluated from right to left (but printed from left to right). --*p decreases the B at arr[1] to A; arr[1] is given out. Then the pointer is increased to arr[2] and the letter there given out. Undefined behaviour, though?
10th May 2019, 11:59 AM
HonFu
HonFu - avatar
+ 3
HonFu ya may be you are right but still doubtful btw thanks for your help...
10th May 2019, 4:11 PM
Sp Maurya
Sp Maurya - avatar
+ 2
That kind of riddles are made up with the aim to confuse us, so don't worry about it. 😉
10th May 2019, 4:18 PM
HonFu
HonFu - avatar
+ 2
Prokopios Poulimenos yes i think you are right i compared the same thing in c++ using cout the result was different (output was C B)... so it is probably the printf in which the variables are update after the statement execution instead of while statement is running...
10th May 2019, 4:44 PM
Sp Maurya
Sp Maurya - avatar
+ 1
😂🤣 oh thank you buddy... 😎😘
10th May 2019, 4:39 PM
Sp Maurya
Sp Maurya - avatar