C programming Printing a float as long double in hex

format specifier for double in c
format specifiers in c++
format specifier in c++
print types in c
c format string double
print short int in c
printf in c
how to print unsigned int in c

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

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);

Format Specifiers in C, Real numbers are represented in C by the floating point types float, double, and long double. So (in a very low-precision format), 1 would be 1.000*20, 2 would be functions on floating-point values are not linked into C programs by default,​  printf and scanf function in C/C++ uses Microsoft C library and this library has no support for 10 byte long double. So when you are using printf and scanf function in your C/C++ code to print a long double as output and to take some input as a long double, it will always give you wrong result.

Is this a value that might be different from computer to computer

Definitely. It's not at all clear what your professor meant by "long double as hex". If you do as you've done, passing a long double to the %x specifier, then you get undefined behaviour - the %x specifier expects an integer (or variant) and it's not easy to predict what will happen if you pass it a float (or variant).

If you use the %a specifier, which is explicitly for printing floats in hex, then you get a hex representation of the floating point value, not the contents of memory. So, for example 1.5 would be printed as 0x1.8p+0 because .8 is hexadecimal for .5 in decimal.

If what you really want is the contents of memory, as hex, then you need to pass the %x specifier an integer. Easiest way to do that is add appropriate integer types to your union.

union data {
        float f;
        int i;
        double d;
        long int li;
        long double ld;
        long long int lli;
    }value; //union variable

Then, if you want the memory contents in hex of a double, for example, just use:

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

puts("\nBreakdown of the element in the union: ");
printf("Double: %f", value.d);
printf("Hex: %lx", value.li);

By the way, %l... (lowercase ell) is for integers, %L... (uppercase ell) is for float types. A long int is typically the same size as a double on 64 bit platforms (but not guaranteed to be so!). %f, somewhat confusingly, is for a double argument. There is no specifier for floats - they are automatically promoted to doubles.

C/FloatingPoint, The format specifier in printf() and scanf() are mostly the same but there is some of digits in integer, characters in string and number of digits after decimal part in floating value. #include <stdio.h>. int main(). {. char ch = 'A' ;. printf ( "%c\n" , ch);. return 0;. } Unsigned Hexadecimal for integer : %x, %X lld for long long int. The float value : 10.327000 The sum of float and int variable : 38.327000 Double. Double is also a datatype which is used to represent the floating point numbers. It is a 64-bit IEEE 754 double precision floating point number for the value. It has 15 decimal digits of precision. Here is the syntax of double in C language, double variable_name; Here is an example of double in C language, Example

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 (floating point): %f\n", pi);
    printf ("pi (hex representation): 0x%\n", f2u.u);

    return 0;
}

Output:

$ ./bin/float_hex
pi (floating point) : 3.141593
pi (hex representation): 0x40490fdb

Format specifiers in C, Language support library The exponent is never optional for hexadecimal floating-point literals: 0x1ffp10, 0X0p-1 (no suffix) defines double; f F defines float; l L defines long double although they can be parsed and printed by the I/​O functions since C++11: both C++ I/O streams when std::hexfloat is  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more unsigned long long type printing in hexadecimal format

Floating point literal, The format specifiers are used in C for input and output purposes. CServer Side ProgrammingProgramming %lu, Unsigned int or unsigned long f); //print float value printf("%e\n", f); //print in scientific notation int a = 67; printf("%o\n", a); //​print in octal format printf("%x\n", a); //print in hex format char str[]  Enter value of a: 64 Enter value of b: FAFA Value of a: Hex: 64, Decimal: 100 Value of b: Hex: FAFA, Decimal: 64250 Declaring integer array by assigning hexadecimal values. Consider the following example, where integer array is declaring with the Hexadecimal values and printing in both formats Decimal and Hexadecimal.

Format specifiers in C, Learn all about the C format specifier mini-language and become an expert at If you want to print an integer in octal or hexadecimal you'd use o for octal, Displaying floating point numbers has a ton of different options, best shown in a table: that you want to print d as an double; but d is not a double, it is a long double. Similarly, if I wanted to print a Long Double float in hex I would use: printf(" Long 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.

Printf format strings, printf places output on the standard output stream stdout. fprintf places output on the named An optional l specifying that a following d, o, u, x, or X conversion character applies to a long integer arg. For c, d, s, and u conversions, the flag has no effect. f: The float or double arg is converted to decimal notation in the style  C Tutorial – printf, Format Specifiers, Format Conversions and Formatted Output In this C programming language tutorial we take another look at the printf function. We will look at how to use format specifiers to print formatted output onto the screen.

Comments
  • Do you really think that an union can convert an object representation? In your case it seems that you expect that setting the float object of the union data magically give you the same value expressed in double or long double format at your desire?
  • @Frankie_C don't believe it should be the same, that is the point of this assignment. Whichever value we store into the union last is what is held inside of it.
  • "Whichever value we store into the union last is what is held inside of it" this is correct. So what should be the sense assigning a float and dumping the hex content of the long double? Also printing a double or a long double from a float init value is nosense...
  • @Frankie_C to show that the output isn't correct for anything other than what we've just scanned in. Are you saying our outputs are different because the value is no good to begin with?
  • @Frankie_C thanks I think I understand. I appreciate your help
  • The output using %La is 0x0.000000p-1022
  • However, I found that for converting a floating-point number to the hexadecimal exponent notation we use %a en.cppreference.com/w/c/io/fprintf
  • I am sorry but I have reviewed this post before making my own and my question is not answered by that thread. If I used %a or %La the hexadecimal output is 0x0.000000p-1022 which is very far from my professors of 22fde0
  • Maybe my professor is wrong as the more I research this the more I believe the output should be in the format of 0x0.000000p-1022
  • This answer does NOT use %a. It prints the hexa format of the four-byte representing the float - that is 0x40490fdb for the pi of 3.141593.