C Programming - Pointers - Discussion
Discussion Forum : Pointers - Point Out Correct Statements (Q.No. 3)
3.
Which of the statements is correct about the program?
#include<stdio.h>
int main()
{
float a=3.14;
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
}
Discussion:
50 comments Page 4 of 5.
ASHISH said:
1 decade ago
What is happening here is that ASCII value of a character that looks like '|-' or a 'T' rotated by 90 degrees anti-clockwise is getting stored in the
first byte of the float variable a.
Now the ASCII value of this character actually is 195. But since 1 signed byte stores 8 bits. The first bit is for sign which leaves us with 7 bits to
store the ASCII value. So the range is -128 to 127. Since 195 > 127. 195 in binary form is 1100 0011.
NOW WHAT IS VALUE OF 195 IN UNSIGNED CHAR RANGE.....
The MSB i.e 1 is used as sign. MSB=1 indicates negative.
X100 0011
||
X011 1100<--1'S COMPLEMENT
+1<--2'S COMPLEMENT
------------
X011 1101 (32+16+8+4+1=61).
------------
NOW CONSIDERING MSB BIT 1, THE VALUE OF 195(1100 0011) IN UNSIGNED CHAR WILL BE -61.
first byte of the float variable a.
Now the ASCII value of this character actually is 195. But since 1 signed byte stores 8 bits. The first bit is for sign which leaves us with 7 bits to
store the ASCII value. So the range is -128 to 127. Since 195 > 127. 195 in binary form is 1100 0011.
NOW WHAT IS VALUE OF 195 IN UNSIGNED CHAR RANGE.....
The MSB i.e 1 is used as sign. MSB=1 indicates negative.
X100 0011
||
X011 1100<--1'S COMPLEMENT
+1<--2'S COMPLEMENT
------------
X011 1101 (32+16+8+4+1=61).
------------
NOW CONSIDERING MSB BIT 1, THE VALUE OF 195(1100 0011) IN UNSIGNED CHAR WILL BE -61.
Devendra said:
1 decade ago
What about %d it is a specifier which prints the decimal value of the variable not the ASCII value for which %c would have been used?
Balashankar said:
1 decade ago
Float value is stored in memory as IEEE754 format. This is different from normal binary conversion.
Madhu said:
1 decade ago
@Manasa.
Here float a = 3.
Hence in its 4 bytes it has its values as:
00000000 00000000 00000000 00000011.
As it is converted to char, its size is 1 byte and it points to first byte, whose value is '0'. Hence result is zero.
Here float a = 3.
Hence in its 4 bytes it has its values as:
00000000 00000000 00000000 00000011.
As it is converted to char, its size is 1 byte and it points to first byte, whose value is '0'. Hence result is zero.
Dilip said:
1 decade ago
Static and global variables are stored in Data section of memory.
Ichidan said:
1 decade ago
The answer is incorrect.
'It prints the 8-bit signed integer equivalent of the data present in the first byte of the float variable a'.
1. It's got nothing to do with ASCII. As ASCII is just a standard way of representing a 7-bit number i.e. numbers 0 - 127 are mapped to various symbols. e.g. 48 = '0', 65 = 'A', 66 = 'B', 97 = 'a'.
2. It's got as much to do with binary as it's got to do with transistors.
'It prints the 8-bit signed integer equivalent of the data present in the first byte of the float variable a'.
1. It's got nothing to do with ASCII. As ASCII is just a standard way of representing a 7-bit number i.e. numbers 0 - 127 are mapped to various symbols. e.g. 48 = '0', 65 = 'A', 66 = 'B', 97 = 'a'.
2. It's got as much to do with binary as it's got to do with transistors.
ASHISH GOPAL said:
1 decade ago
About heap memory:
Whenever we use dynamic memory allocation functions such as malloc() or calloc(), it allocates a memory and returns a pointer to it. The allocation of the memory is done in internal RAM of the micro controller, this memory is nothing but the heap memory.
Whenever we use dynamic memory allocation functions such as malloc() or calloc(), it allocates a memory and returns a pointer to it. The allocation of the memory is done in internal RAM of the micro controller, this memory is nothing but the heap memory.
Appaso said:
10 years ago
What is char* and *char?
Bindu said:
10 years ago
Here this program produces garbage value.
MrMino said:
10 years ago
printf will expect sizeof (int) value at %d. printf will read the first 4 bytes of the float, and print them as integer value. It will print garbage. Correct answer should be D!
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers