+ 1

Whats the output and why ?

#include<stdio.h> main() { int x = 65, *p = &x; void *q=p; char *r=q; printf("%c",*r); }

2nd Dec 2016, 2:53 AM
Arshdeep singh
Arshdeep singh - avatar
3 Answers
+ 2
The output should be 'A' because 65 is the code point for 'A' in ASCII and UTF-8.
2nd Dec 2016, 5:00 AM
Gordon Garmaise
Gordon Garmaise - avatar
+ 2
I must differ with Varshini. A compiler will accept this code and execute it as I predicted. C (unlike more strongly typed languages) is permisive in the assignment of pointers and will perform type conversions automatically. This is particularly true of void pointers which are essentially typeless memory addresses. I should further qualify my earlier answer. I assumed a little-endian byte ordering (which is true for Intel based systems and many others). In the less common big-endian byte order, the pointer would point to a byte containing zero (0) which is a null character in most character encodings.
17th Jan 2017, 3:20 AM
Gordon Garmaise
Gordon Garmaise - avatar
+ 1
A pointer is a variable that is used to store the address of another variable.The data type of pointer variable and the address of variable to be stored must be same.In your case,you have declared different data types(int,void,char).The complier wont execute it.If you assign all pointer variables in integer,the output will be A. Since the ASCII value of 65 is A,it will display A.
15th Jan 2017, 8:17 AM
Varshini
Varshini - avatar