The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+15 votes
3k views

What does the following fragment of C program print?

    char c[] = "GATE2011";
    char *p = c;
    printf("%s", p + p[3] - p[1]);
  1. $GATE2011$
  2. $E2011$
  3. $2011$
  4. $011$
asked in Programming by Veteran (101k points)
edited by | 3k views
0

it is clear that we  can use direct convention for this question

but my doubt is why answer is wrong for this method ?

subtraction of pointer are allowed .if  address allocation for

G   A   T   E  2   0  1   1  null

100 101 102 103 104 105 106 107 108

then p[3]=103

p[1]=101 so p[3]-p[1]=2

p+2=102 by this TE2011 has to be print 

@Bikram sir

0
i think instead of p[3]=103  it will give e (value at that address ) m i right ?

@Bikram sir
0
@set2018

p[3]=*(p+3)=
0

@saxena0612 yeah u r right it will give value not an address 

+7

set2018 

array index starts from 0 so p[3] is 4th element in that array c[ ] = "GATE2011"

p[3] = E and p[1] = A 

ASCII value of E = 69 , A = 65 

E- A = 69 - 65 = 4 

p+ (p[3]-p[1] )= p+4 = p[4] = *(p+4) = 2011 

p[4] pointing to 2 in array c[ ] . 

0
P+4 is not P[4]

*(P+4) = P[4]

Please correct me if i am wrong or i missed something.

2 Answers

+48 votes
Best answer

2011 is the answer. 

In C, there is a rule that whatever character code be used by the compiler, codes of all alphabets and digits must be in order. So, if character code of '$A$' is $x$, then for '$B$' it must be $x+1$. 

Now %s means printf takes and address and prints all bytes starting from that address as characters till any byte becomes the code for '\0'. Now, the passed value to printf here is
$p + p[3] - p[1]$

$p$ is the starting address of array c. $p[3] = 'E'$ and $p[1] = 'A'$. So, $p[3] - p[1] = 4$, and $p + 4$ will be pointing to the fifth position in the array c. So, printf starts printing from $2$ and prints $2011$. 

(Here "$GATE2011$" is a string literal and by default a '\0' is added at the end of it by the compiler). 

NB: In this question %s is not required. 

 printf(p + p[3] - p[1]);

Also gives the same result as first argument to printf is a character pointer and only if we want to pass more arguments we need to use a format string. 
 

answered by Veteran (358k points)
edited by
0
But when I am running it in a  c  compiler then why I am getting an error ?
0
p[3] - p[1] = 4,  here how 4 has came @Arjun Sir Please explain.
+4
p[3] = E

p[1] = A

ASCII value of A=65, E=69

p + 69 - 65 = p+4 = p[4]
0
is there is particular chart for ascii values.?
+2

Yes, you can check here : http://www.asciitable.com/

You can solve the above question even if you don't remember the ASCII values since all the Alphabet are consecutive in the ASCII table.

Say, let us assume A = 1

then B is 2, C is 3 and so on. E becomes 5

E-A = 5-1 =4.

0

P+4 basically means P[4] right? so it is actually NOT 4th psition it is 5th position with index number-4, right?

+1

smartmeet 

 it is 5th position with index number-4, right?

Yes, right.

p[4] = *(p+4)

P+4 basically means P[4] that fifth element in the array , in array indexing starts from 0.

G A T E 2 0 1 1 

p[4] is  '2' . 

0
what is the reason behind taking ascii values of E  and  A .  how we came to know that we have to use ascii values here ?
0
printf(p + p[3] - p[1]);

Also gives the same result as first argument to printf is a character pointer and only if we want to pass more arguments we need to use a format string. 

@Arjun sir plz explain this.

+4
printf is a function. Imagine you wrote this function definition. Now, what it does entirely depends on what you wrote. The function is given a number of arguments, but you always start processing the first one which is just a char pointer to an address. You just keep on printing any character from that address till a '\0' (null character) is found. If '%' is found in between, you do different job like if '%d' is there you take the next argument and print its decimal equivalent etc.

Now, if you understand above '%s' is not required if we are just printing one string.
+1
ok got it sir.. :)
+4 votes
2011...option c
answered by (137 points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

39,751 questions
46,766 answers
140,658 comments
58,522 users