retagged by
20,138 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

Best answer
53 votes
53 votes

char $x =  $'$a$' $+ 2$
i.e. $x ==$'$c$'

So, $p=${'$1$','$0$','$c$'}

$*((char*)q+1) == p[1]$
$*((char*)q+2) == p[2]$

printf("%c, %c", *((char*)q+1), *((char*)q+2));

Output:  $0, c$

Correct Answer: $A$

edited by
27 votes
27 votes

Important to understand:-

1)

int *p = &a;

In this declaration, p will store the address of a & it only says that the expected value stored at 'a' is integer data type or in simple words, p is pointing to an address which contains integer data. But in practice, a could be anything (int, char, even it could be a structure).    Try it yourself-

int a=101;

char *p=&a;

printf("%d",*p);           output:- 101

2)

WHAT IS CHAR DATA TYPE:- A single letter of any kind is character whether it is a, b, 1, 5, %, $...

only ASCII value of the corresponding letter is stored at the given address. So single integer values are also allowed.

How to store a character into char data type. char a='f'; (by using single quote) & string using double quote("C-programming")

Now come back to question

LINE 1     struct Ournode p = {'1', '0', 'a'+2} ;
LINE 2     struct Ournode *q = &p ;
LINE 3     printf ("%c, %c", *((char*)q+1), *((char*)q+2)) ;

Here pointer q is defined as it is expecting an address of a structure of type ournode. So if we replace LINE 3 with this statement 

printf("%c",*q);                 It will print 1st char data stored in the structure but if we replace LINE 3 with this

printf("%c",*(q+1));           it will jump to next record(skipping structure p completely) & won't print anything, because expected data of pointer q is structure so size of "struct Ournode" will be added to the address stored into pointer q.

Now replace LINE 3 with 

printf ("%c", *((char*)q+1));

if we (Typecast) or change the expected type of the pointer p to char data type then it will not jump to the next record but will add the size of the char data to the address stored into pointer q. so "0" will be printed.

Option A is correct.

If we change LINE 2 & 3 with this...

char *q = &p;

printf ("%c, %c", *(q+1), *(q+2)) ;

then we don't even have to typecast the pointers, It will also work and produces the same result.

 

10 votes
10 votes

Answer is: 0,c

char x = 'a' + 2 (ASCII value of a + 2  , so c will be stored)

So, p={'1','0',c};

*((char*)q+1) == p[1]  // q will be character pointer and point to p, which will be incremented by 1. Hence p[1]
*((char*)q+2) == p[2]; // q will be character pointer and point to p, which will be incremented by 2. Hence p[2]
 

Check compiled code here: https://ide.geeksforgeeks.org/jBd3XcMfAX

9 votes
9 votes

Single quotes are used for assigning values to char.

Double quotes are used for assigning values to string.

So, whatever single literal is enclosed in single quotes, will be the value of the char.

 

Hence, 0 and c. NOT '0' and 'c'.

Option A


To print chars enclosed in single quotes, we'll have to use ' ' explicitly in the print statement.

Answer:

Related questions

23 votes
23 votes
5 answers
3