C programming Printing a float as long double in hex

4159 views c
9

I am creating a program for homework, and the goal is trying to teach us how a union works conceptually. I believe I understand this concept, but I am having difficulty with one of my outputs; Long Double as Hex.

I am under the assumption anything long needs a capital L after the percent sign when printing. For example to print a long double float:

printf("\nLong double: %Lf\n", value.ld);

Similarly, if I wanted to print a Long Double float in hex I would use:

printf("\nLong double: %Lx", value.ld);

My professor's output is as follows:

Professors Output long double 22fde0

My Output is as follows:

My Output long double 62fe40

The differences are in the printed value for Long Double in hex. Is this a value that might be different from computer to computer or am I making a mistake in my thinking.

My code

#include <stdio.h>

// define union data
    union data {
        float f;
        double d;
        long double ld;
    }value; //union variable

// begin main function
int main(void){

    // get initial user input
    printf("Enter data for type float: ");
    scanf(" %f", &value.f);

    puts("\nBreakdown of the element in the union: ");
    printf("Float: %f", value.f);
    printf("\nDouble: %f", value.d);
    printf("\nLong double: %Lf\n", value.ld);

    puts("\nBreakdown in hex: ");
    printf("Float: %x", value.f);
    printf("\nDouble: %x", value.d);
    printf("\nLong double: %Lx", value.ld);

    // get user input
    printf("\n\nEnter data for type double: ");
    scanf(" %lf", &value.d);

    puts("\nBreakdown of the element in the union: ");
    printf("Float: %f", value.f);
    printf("\nDouble: %f", value.d);
    printf("\nLong double: %Lf\n", value.ld);

    puts("\nBreakdown in hex: ");
    printf("Float: %x", value.f);
    printf("\nDouble: %x", value.d);
    printf("\nLong double: %Lx", value.ld);

    // get user input
    printf("\n\nEnter data for type long double: ");
    scanf(" %lf", &value.ld);

    puts("\nBreakdown of the element in the union: ");
    printf("Float: %f", value.f);
    printf("\nDouble: %f", value.d);
    printf("\nLong double: %Lf\n", value.ld);

    puts("\nBreakdown in hex: ");
    printf("Float: %x", value.f);
    printf("\nDouble: %x", value.d);
    printf("\nLong double: %Lx", value.ld);

    return 0;
} // end main function

answered question

2 Answers

2

The %x format specifier is expecting an integer type to be printed in hex. Passing in a floating point type for this invokes undefined behavior.

You can print floating point numbers in hex with the %a format specifier:

printf("\nLong double: %La", value.ld);

posted this
7

Please use union, as pointed in how to print float in hex format in C?

Here is what I grabbed from the link:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <math.h>

int main (void) {

    float pi = (float)M_PI;
    union {
        float f;
        uint32_t u;
    } f2u = { .f = pi };

    printf ("pi : %f\n   : 0x%" PRIx32 "\n", pi, f2u.u);

    return 0;
}

Output:

$ ./bin/float_hex
pi : 3.141593
: 0x40490fdb

posted this

Have an answer?

JD

Please login first before posting an answer.