• retagged by
38,003 views
89 89 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'

11 Answers

Best answer
91 91 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
64 64 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.

 

16 16 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.

12 12 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

7 7 votes
Answer is: (A) 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]
4 4 votes

'a' + 2 will give 'c'

Ournode p={'1', '0', 'c'}

$*((char*)q+1) \Rightarrow 0$

$*((char*)q+2) \Rightarrow c$

Answer:
Position:
Show:

Related questions

56 56 votes
7 answers 7 answers
29.1k
29.1k views
gatecse asked Feb 14, 2018
29,086 views
Consider the following C code. Assume that unsigned long int type length is $64$ bits.unsigned long int fun(unsigned long int n) { unsigned long int i, j=0, sum = 0; for(...
78 78 votes
7 answers 7 answers
26.2k
26.2k views
gatecse asked Feb 14, 2018
26,175 views
#include<stdio.h void fun1(char* s1, char* s2){ char* temp; temp = s1; s1 = s2; s2 = temp; } void fun2(char s1, char s2){ char* temp; temp = *s1; *s1 = *s2; *s2 = temp;...
51 51 votes
3 answers 3 answers
16.1k
16.1k views
Ishrat Jahan asked Nov 2, 2014
16,121 views
Consider the following C program:#include <stdio.h typedef struct { char *a; char *b; } t; void f1 (t s); void f2 (t *p); main() { static t s = {"A", "B"}; printf ("%s %s...
150 150 votes
7 answers 7 answers
40.8k
40.8k views
Ishrat Jahan asked Oct 31, 2014
40,789 views
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, "be...