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 3 of 5.
Himanshu Chauhan said:
1 decade ago
Hey can any one please explain me that what is the main difference between printf and scanf?
Manasa said:
1 decade ago
#include<stdio.h>
int main()
{
float a=3;
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
}
Can somebody explain why the answer is "0" here.
int main()
{
float a=3;
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
}
Can somebody explain why the answer is "0" here.
Raja said:
1 decade ago
It prints -61 that is garbage.
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.
Amit said:
1 decade ago
How is the ASCII value of 3.14 found?
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)
Prasanna Hegde said:
1 decade ago
-61 is the answer when compiled using a GNU compiler. I don't know about turbo c/c++ and I wonder why people use that even if it doesn't follow universally accepted ANSI standard!
Ganesh said:
1 decade ago
@Vishwas
which is the 1st byte in 3.140000? 03 or 00?? whatever it is.. ascii value for 0 is 60 and that of 3 is 63.. so how can ans be -61??
which is the 1st byte in 3.140000? 03 or 00?? whatever it is.. ascii value for 0 is 60 and that of 3 is 63.. so how can ans be -61??
Vishwas S said:
1 decade ago
Can anyone please explain how 3.14 is stored in a computer?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers