Program c from GATE paper

Hi everyone yesterday, I gave my GATE exam, and the question arose on this exam:

What does the following C program fragment output?

char c [] = "GATE2011"; char *p =c; printf ( "%s", p+p [3] โˆ’ p [1]) ; 

options:

 (A) GATE2011 (B) E2011 (C) 2011 (D) 011 

Answer: C '. when I tried the code on my computer But how ??

Thank you in advance

+4
source share
2 answers

This pointer arithmetic is:

 char c [] = "GATE2011"; // indices: 01234567 char *p =c; 

p[1] is the character A , p[3] is the E character code, their difference ( E code minus A code) is 4, this difference is then added to the pointer p and you have the address of substring 2011 passed to printf() .

+5
source

Here p [3] = E p [1] = A ascii A โ†’ 65 and E โ†’ 69 69-65 = 4 p + 4 will indicate the 4th row index GATE2011 01234567

SO ... 2011 char[c]="GATE2011"

0
source

Source: https://habr.com/ru/post/1339606/


All Articles