c - How exactly can I produce the output for this? -


question: if binary equivalent of 5.375 in normalised form 0100 0000 1010 1100 0000 0000 0000 0000, o/p of following program`:

#include <stdio.h>  int main(void) {     float = 5.375;     char * p;     int i;     p = (char*)&a;     (i = 0; <= 3; i++)         printf("%02x\n",(unsigned char)p[i]);     return 0; } 

this question in 1 of textbooks , has 4 options associated it. here, have several doubts:

  1. what p contains? pointer to?
  2. what 02x means?
  3. won't actual o/p depend on whether machine little endian or big endian?

anyways, answer given is: 00 00 ac 40.

thanks!

  1. char* p; means p pointer char variable, , have:

    p = (char*)&a; 

    so p stores address (&) of variable a.

  2. %02x - convert unsigned integer hexadecimal form capital a-f letters (x), show 2 "digits" (2) preceding zeros (0), more here http://en.cppreference.com/w/c/io/fprintf

  3. it should depend, output 00 00 ac 40 little endian, 40 ac 00 00 big endian. here https://en.wikipedia.org/wiki/endianness , here https://en.wikipedia.org/wiki/floating_point see how floating point numbers stored in memory.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -