retagged by
20,165 views
42 votes
42 votes

Consider the following C program:

#include<stdio.h>
struct Ournode{
    char x, y, z;
};
int main() {
    struct Ournode p={'1', '0', 'a'+2};
    struct Ournode *q=&p;
    printf("%c, %c", *((char*)q+1), *((char*)q+2));
    return 0;
}

The output of this program is:

  1. 0, c
  2. 0, a+2
  3. '0', 'a+2'
  4. '0', 'c'
retagged by

9 Answers

0 votes
0 votes
char x = ‘a’+2;
The x variable here stores a character ‘c’ in it.
Because +2 will increment ascii value of a from 92 to 95.
Hence the structure p contains 3 character values and they are ‘1’, ‘0’, and ‘c’.
q is a pointer pointing to structure p.
Hence q is pointing to ‘1’, q+1 pointing to ‘0’ and q+2 pointing to ‘c’.
Option d cannot be correct, as though they are characters, printf will not print them in single quotes.
Answer:

Related questions

23 votes
23 votes
5 answers
3