C Programming - Floating Point Issues - Discussion
Discussion Forum : Floating Point Issues - General Questions (Q.No. 3)
3.
If the binary eauivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000 0000 0000, what will be the output of the program (on intel machine)?
#include<stdio.h>
#include<math.h>
int main()
{
float a=5.375;
char *p;
int i;
p = (char*)&a;
for(i=0; i<=3; i++)
printf("%02x\n", (unsigned char)p[i]);
return 0;
}
Discussion:
117 comments Page 3 of 12.
Anand said:
1 decade ago
Hexadecimal conversion of the variable in the code. That's it nothing more than that.
Venkatesh said:
1 decade ago
#include<stdio.h>
int main()
{
int i=1; // i=0001 it will store in memory like this 00000000 00000000 00000000 00000001
char *p; // p is declared as a char pointer
p=(char*)&i; //here integer is typecasting to char i.e. 00000001
00000000
00000000
00000000 and pointer is always points to starting address.
if(*p)
printf("=====little-endian);
else
printf("=====big-endian);
return 0;
}
int main()
{
int i=1; // i=0001 it will store in memory like this 00000000 00000000 00000000 00000001
char *p; // p is declared as a char pointer
p=(char*)&i; //here integer is typecasting to char i.e. 00000001
00000000
00000000
00000000 and pointer is always points to starting address.
if(*p)
printf("=====little-endian);
else
printf("=====big-endian);
return 0;
}
Dheeraj said:
1 decade ago
Why we are using %02x? any one can explain me.
Sameer Sarkaar said:
1 decade ago
What is little endian?
Suresh Gupta said:
1 decade ago
We are use database in C or not ?
Sayan bose said:
1 decade ago
How is hexadecimal operation taking place in a compiler?
Sudhir said:
1 decade ago
Here what is mean by printf("%02x\n", (unsigned char)p[i]); statement, please explain me.
Piya said:
1 decade ago
Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address. That is, the little end comes first.
For example, a 4 byte, 32-bit integer.
Byte3 Byte2 Byte1 Byte0.
will be arranged in memory as follows:
Base_Address+0 Byte0.
Base_Address+1 Byte1.
Base_Address+2 Byte2.
Base_Address+3 Byte3.
Example :Intel processors use "Little Endian" byte order.
"Big Endian" means that the higher order byte of the number is stored in memory at the lowest address, and the lower order byte at the highest address. The big end comes first.
Base_Address+0 Byte3.
Base_Address+1 Byte2.
Base_Address+2 Byte1.
Base_Address+3 Byte0.
Motorola, Solaris processors use "Big Endian" byte order.
For example, a 4 byte, 32-bit integer.
Byte3 Byte2 Byte1 Byte0.
will be arranged in memory as follows:
Base_Address+0 Byte0.
Base_Address+1 Byte1.
Base_Address+2 Byte2.
Base_Address+3 Byte3.
Example :Intel processors use "Little Endian" byte order.
"Big Endian" means that the higher order byte of the number is stored in memory at the lowest address, and the lower order byte at the highest address. The big end comes first.
Base_Address+0 Byte3.
Base_Address+1 Byte2.
Base_Address+2 Byte1.
Base_Address+3 Byte0.
Motorola, Solaris processors use "Big Endian" byte order.
Praful said:
1 decade ago
I think this value stored in this manner.
0100 0000 1010 1100 0000 0000 0000 0000 which is 40ac0000.
float taken 4 byte in memory in program p=(char*)&a casting this to char type nw p(it char pointer its point to i byte value) point to lower byte of the a(5.375) which is 0000 0000.
p[0]-> contain the fist lower byte data 00(because print bye the hexa format specifier withe 2 width)
p[1]-> contain 00
p[2]-> contain ac
p[3]-> contain 40
Then output become ...0000ac40.
0100 0000 1010 1100 0000 0000 0000 0000 which is 40ac0000.
float taken 4 byte in memory in program p=(char*)&a casting this to char type nw p(it char pointer its point to i byte value) point to lower byte of the a(5.375) which is 0000 0000.
p[0]-> contain the fist lower byte data 00(because print bye the hexa format specifier withe 2 width)
p[1]-> contain 00
p[2]-> contain ac
p[3]-> contain 40
Then output become ...0000ac40.
Suma said:
1 decade ago
@Shilpa m raj.
1.01011*2^2 = 1.01011*100 where 2 is true exponent.
I didn't understand, how *2^2 become *100.
1.01011*2^2 = 1.01011*100 where 2 is true exponent.
I didn't understand, how *2^2 become *100.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers