0

Why doesn't the double variable show a garbage value?
I know I am playing with pointers, but I meant to. And is there anything wrong with my code? It threw a few warnings because of incompatible pointer assignments.

#include "stdio.h"

double y= 0;    
double *dP = &y;    
int *iP = dP;    
void main()
{
    printf("%10#x %#10x %#10x %#10x \n",&y,dP,iP,iP+1);     
    printf("%#10lf %#10lf %#10lf %#10lf \n",y,*dP,*iP,*(iP+1));     
    scanf("%lf %d %d",&y,iP,iP+1);     
    printf("%10#x %#10x %#10x %#10x \n",&y,dP,iP,iP+1);     
    printf("%#10lf %#10lf %#10d %#10d \n",y,*dP,*iP,*(iP+1));
}

Output

  • 8
    You should post the code not the link to the screenshot. And you executed the code four times in which of them did you get the unexpected output? please post the code so it will be easy to more people try to help you. – Muzol Aug 25 '18 at 04:45
  • I'll post the code and the last output in the picture one shows the output for that code . – Ajay Sakthikumar Aug 25 '18 at 05:17
  • What do you mean by "Why does **not** ... ?". Most of what is printed is garabage and/or undefined behaviour? Could you focus on one output and explain about why you expect it to be garbage and why you think it is not? – Yunnosch Aug 25 '18 at 05:43
  • 1
    Most of it is not garbage . I think you mistook the hexadecimal values. And for your question I expect variable y to have garbage value ,this is because I am storing 2 integer values within a double variable using pointers . The double block has the values of 2 variables . How can the variable store values but still give 0.0000. – Ajay Sakthikumar Aug 25 '18 at 05:52
  • you're getting UB. [Pointers must be printed using `%p`](https://stackoverflow.com/q/9053658/995714) – phuclv Aug 26 '18 at 01:56

1 Answers1

1

Welcome to Stack Overflow. It's not very clear what you're trying to do with this code, but the first thing I'll say is that it does exactly what it says it does. It tries to format data with the wrong format string. The result is garbage, but that doesn't necessarily mean it will look like garbage.

If part of the idea is to print out the internal bit pattern of a double in hexadecimal, you can do that--but the code will be implementation-dependent. The following should work on just about any modern 32 or 64-bit desktop implementation using 64-bits for both double and long long int types:

double d = 3.141592653589793238;
printf("d = %g = 0x%016llX\n", d, *(long long*)&d);

The %g specification is a quick way to print out a double in (usually) easily readable form. The %llX format prints an unsigned long long int in hexadecimal. The byte order is implementation-dependent; even if you know that both double and long long int have the same number of bits. On a Mac, PC or other Intel/AMD architecture machine, you'll get the display in most-significant-digit-first order.

The *(long long *)&d expression (reading from right to left) will take the address of d, convert that double* pointer to a long long * pointer, then dereference that pointer to get a long long value to format.

Almost every implementation uses IEEE 754 format for hardware floating point this century. 64-bit IEEE format (aka double)

You can find out more about printf formatting at: http://www.cplusplus.com/reference/cstdio/printf/

Mike Housky
  • 3,959
  • 1
  • 17
  • 31
  • But my doubt is I have already stored 2 integer values inside the space allocated for a single double variable.Im sorry but I'm kind of noobie . Why does the printing it show zero when there already values present in it . And I can print both values of integers stored in it .which means that both parts of the double variable are occupied by some value . – Ajay Sakthikumar Aug 25 '18 at 06:02
  • @AjaySakthikumar The double version will look like a zero if you stored an integer between 0 and about couple million in the *upper* word. Any value with zeroes in all exponent bits is numerically 0.0 in IEEE floating point. Try storing a small negative number in the second int. That should give you a minus infinity (with 1 bits in the sign and all exponent bits.) – Mike Housky Aug 25 '18 at 23:23
  • @AjaySakthikumar For a more instructive way to look at binary encoding of floating point, try storing valid doubles and then looking at the hex representation. Also look at the docs. I'll add a link. – Mike Housky Aug 25 '18 at 23:25
  • 1. cplusplus.com is not a reliable source, with many incorrect information. 2. [`%lf` is completely valid in printf](https://en.cppreference.com/w/c/io/fprintf). [Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?](https://stackoverflow.com/a/28222471/995714) – phuclv Aug 26 '18 at 01:59
  • @phuclv Okay, "not valid" was too strong. Removed. But cplusplus.com bashing, while deserved a decade ago, is pretty strong too. They've cleaned up their act quite a bit. It's always been easier to navigate than cppreference; or anything else I've seen. – Mike Housky Aug 26 '18 at 11:23