+ 4
Here is simplified equivalent code that shows what happens in smaller steps. I hope this helps! char *str = "GATEEXAM"; //str points to 'G' //printf("%c,",*((str-- + 2)+1)-3); //This is equivalent to: char* tmp=str;  str=str-1;  // (str-- tmp=tmp+2;  // +2) //tmp points to 'T' tmp=tmp+1;  // +1 //tmp points to 'E' char ch=*tmp; // *( ) //ch is 'E' ch=ch-3;        // -3  // changes to 'B' printf("%c,",ch); // printf(); // prints "B," //Note: str now points to garbage location, 1 byte before 'G' //printf("%c,",*(--str+3)+32); str=str-1; // --str // now is 2 bytes before 'G' tmp=str+3; // +3) //tmp points to 'A' ch=*tmp; // *( ) //ch is 'A' ch=ch+32; // +32 // changes to lowercase 'a' printf("%c,", ch); // printf(); // prints "a," //printf("%c",*(++str+2)+4); str=str+1; // ++str // 1 byte before 'G' tmp=str+2; // +2) // tmp points to 'A' ch=*tmp; // *( ) // ch is 'A' ch=ch+4; // +4 // changes to 'E' printf("%c",ch); // printf(); // prints "E" //final output: B,a,E
4th Oct 2020, 4:13 PM
Brian
Brian - avatar
+ 4
Sorry for my mistakes but now i updated. Perhaps a program written like this better explains what's going on: https://code.sololearn.com/cLXNE8RnBgDP/?ref=app
5th Oct 2020, 8:18 AM
AS Raghuvanshi
AS Raghuvanshi - avatar
+ 3
str--+2+1 is 'E', and str is decreased by 1. 'E'-3 is 'B'. --str+3 is 'A', 'A'+32 is 'a'. str is decreased by 1. ++str+2 is 'A', 'A'+4 is 'E'. If you can't understand some complex expression - break it into simple ones, like char *pchar1 = str-- + 2 + 1; char char1 = *pchar1 - 3; printf("%c,",char1);//and so on with char2 and char3
5th Oct 2020, 2:09 AM
AS Raghuvanshi
AS Raghuvanshi - avatar
+ 2
EDIT: This comment is obsolete. The analysis that it mentions has been corrected. ♨️♨️ your analysis needs correction. It mistakenly adds values to the pointer expression where in fact they are being added after the pointer expression has been dereferenced. Examine carefully the pointer expressions inside the parentheses, and then notice the constants that are added afterward. Those constants are added to the value found in the memory location, not the pointer address. *((str-- + 2)+1) - 3 // 'E' - 3 = 'B' *(--str+3) + 32 // 'A' + 32 = 'a' *(++str+2) + 4 // 'A' + 4 = 'E'
5th Oct 2020, 4:45 AM
Brian
Brian - avatar