edited by
26,871 views
100 votes
100 votes

Which one of the choices given below would be printed when the following program is executed ?

#include <stdio.h>
struct test {
               int i;
               char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
main ()
{ 
    struct test *p = st;
    p += 1;
    ++p -> c;
    printf("%s,", p++ -> c);
    printf("%c,", *++p -> c);
    printf("%d,", p[0].i);
    printf("%s \n", p -> c);
}
  1. $\text{jungle, n, 8, nclastor}$
  2. $\text{etter, u, 6, ungle}$
  3. $\text{cetter, k, 6, jungle}$
  4. $\text{etter, u, 8, ncestor}$
edited by

6 Answers

Best answer
370 votes
370 votes

code :

#include <stdio.h>

struct test {
    int i;
    char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};

int main () { 
    //printf("size = %d\n",sizeof(struct test) );
    struct test *p = st;
    p += 1;
    ++p->c; // ++(p->c)
    printf("%s,", p++->c); // (p++)->c 
    printf("%c,", *++p->c); // *(++(p->c))
    printf("%d,", p[0].i);
    printf("%s \n", p->c);
}

We will assume few things:

  • Size of integer $4$ Bytes.
  • Size of a pointer $4$ Bytes.

Neglecting any alignment issues with the storage of this structure we will have $8$ Bytes per structure. 

And one precedence rule we need to use:


Initial situation :


struct test *p = st;


p += 1;

We know that if ptr is a pointer then,  ptr + x = ptr + x*sizeof(*ptr);


++p->c;


printf("%s,", p++->c);         // (p++)->c


printf("%c,", *++p->c);     // *(++(p->c))


printf("%d,", p[0].i);


printf("%s \n", p->c);

Correct Answer: $B$

edited by
66 votes
66 votes
#include <stdio.h>
struct test {
               int i;
               char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
main ()
{ 
    struct test *p = st;
    

p += 1;

++p -> c;
printf("%s,", p++ -> c);
    

Prints value and  increments the pointer. Pointing ti the next structure

printf("%c,", *++p -> c); // *(++( p->c ))

printf("%d,", p[0].i); //Prints the integer value associated with struct pointed by p

printf("%s \n", p -> c); //Prints the character string pointed by p

PS : Thank you @monanshi for the explanation

23 votes
23 votes

Answer is B )

Run The code.

http://codepad.org/WmSREd4G

For extra inofrmation =>

++p -> c will become ++(p -> c)
p++ -> c will become (p++) -> c
 *++p -> c will become => *++(p->c)

Reference-> C Puzzle Book

15 votes
15 votes

I am getting B.

 

1.#include <stdio.h>
2.struct test {
3.               int i;
4.              char *c;
5.}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
6.main ()
7.{ 
8.   struct test *p = st;
9.    p += 1;
10.    ++p -> c;
11.    printf("%s,", p++ -> c);
12.    printf("%c,", *++p -> c);
13.    printf("%d,", p[0].i);
14.    printf("%s \n", p -> c);
15.}


Line 8 => Initially p is pointing to st, i.e, first element of st which is {5, "become"}
Line 9 => Now p is pointing to {4, "better"}
Line 10=> ++(p->c) since -> has higher precedence. So, p->c points to 'e' of 'better'
Line 11=> prints 'etter' and p now points to {6, "jungle"}
Line 12=> *++(p->c) since -> has higher precedence. prints 'u'
Line 13=> p->i, which is 6 so prints '6'
Line 14=> prints 'ungle' since p is pointing to 'u'.

So, output is "etter, u, 6, ungle" that is B.

Answer:

Related questions

42 votes
42 votes
9 answers
4