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 1 of 5.
Sarvesh karan said:
6 years ago
Float a=3.14;
All variables are stored in memory in binary representation only.
Like int a=2; four bytes will be occupied in memory where first byte will be stored as 2 or 10 in binary and rest 3 bytes will be stored as 0.
Similarly, float quantities are also stored in binary representation
Sign exponent mantissa. And this information is spread among 4 bytes.
Since here we are type casting it to char*
We are informing compiler to take 1st byte out of 4 bytes of the float.
So we get the partial value of 3.14 mantissa in the first byte of that float variable.
Now y is negative value.
While printing we are again taking 4 bytes of that variable, where only one byte is having data rest all are 0.
As it was typecast to char last byte is holding now 195,
A signed Char ranges between -128 to 127.
So after 127, it will repeat cycle from negative most quantity..i.e -127 -126 -125 .... -61.
All variables are stored in memory in binary representation only.
Like int a=2; four bytes will be occupied in memory where first byte will be stored as 2 or 10 in binary and rest 3 bytes will be stored as 0.
Similarly, float quantities are also stored in binary representation
Sign exponent mantissa. And this information is spread among 4 bytes.
Since here we are type casting it to char*
We are informing compiler to take 1st byte out of 4 bytes of the float.
So we get the partial value of 3.14 mantissa in the first byte of that float variable.
Now y is negative value.
While printing we are again taking 4 bytes of that variable, where only one byte is having data rest all are 0.
As it was typecast to char last byte is holding now 195,
A signed Char ranges between -128 to 127.
So after 127, it will repeat cycle from negative most quantity..i.e -127 -126 -125 .... -61.
(2)
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.
Shivaji Vidhale 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.
The MSB i.e 1 is used as sign. MSB=1 indicates negative.
Remaining (1000011)bits count to 67. After 127 the value becomes -128 since 01111111 (+127)
+ 1
------------
= 11111111 (-128)
Now add 67 till you get 195.
So -128 + 67 = 61
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.
The MSB i.e 1 is used as sign. MSB=1 indicates negative.
Remaining (1000011)bits count to 67. After 127 the value becomes -128 since 01111111 (+127)
+ 1
------------
= 11111111 (-128)
Now add 67 till you get 195.
So -128 + 67 = 61
(1)
Zdd said:
7 years ago
@Vishwas.
3.14D = 11.0010001111010111B.
the format of float:SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM
S: it's 0, it's a postive
E: 10000000 (1+127), the index part is 1.E is positive, hence the base's decimal point move to the right by 1 digit, otherwise, move to the left.
M: (1.)10010001111010111here omitted a 1. in the left.
Then The decimal point is shifted to the right by 1 digit, i.e. 11.0010001111010111B i.e. 3.14D.
Hence it is stored as 01000000 0100100 11110101 11000011 (the width of float is 4 bytes).
3.14D = 11.0010001111010111B.
the format of float:SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM
S: it's 0, it's a postive
E: 10000000 (1+127), the index part is 1.E is positive, hence the base's decimal point move to the right by 1 digit, otherwise, move to the left.
M: (1.)10010001111010111here omitted a 1. in the left.
Then The decimal point is shifted to the right by 1 digit, i.e. 11.0010001111010111B i.e. 3.14D.
Hence it is stored as 01000000 0100100 11110101 11000011 (the width of float is 4 bytes).
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.
Dilip Sharma said:
1 decade ago
float a=3.14;
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
//here j is pointer to char a are declare as 3.14 which is float in third line &a is nothing but the reference of a i.e direct retrived the value 3.14 and cast it as (char*) ;
so here float value are casted.hence it will print the ASCII charactor of first byte.
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
//here j is pointer to char a are declare as 3.14 which is float in third line &a is nothing but the reference of a i.e direct retrived the value 3.14 and cast it as (char*) ;
so here float value are casted.hence it will print the ASCII charactor of first byte.
Noel Nosse said:
7 years ago
This problem/code appears to me to have been written with errors.
The %d in the printf does not match the float or char. If you make both the value (which is float 3.14) AND the pointer a float AND change the %d to %f, you get the meaningful printing of the 3.14.
Can anyone help me to get it of?
The %d in the printf does not match the float or char. If you make both the value (which is float 3.14) AND the pointer a float AND change the %d to %f, you get the meaningful printing of the 3.14.
Can anyone help me to get it of?
ASHISH GOPAL said:
10 years 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.
Ritesh Agarwal said:
1 decade ago
#include<stdio.h>
int main()
{
int a=3;
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
}
This code is similar. But the output is 3. why? integer is also 4 byte. Here ASCII value is not printed.
int main()
{
int a=3;
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
}
This code is similar. But the output is 3. why? integer is also 4 byte. Here ASCII value is not printed.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers