Isaac Asante
Isaac Asante

Reputation: 455

Which specifier to use to print the max/min value of long double?

As a new C programming learner, I'm trying to understand which specifier I should use in printf to display the maximum and minimum values of a long double type. Per the following StackOverflow answer, I can use %Lg: https://stackoverflow.com/a/18797417/1536240

But that doesn't seem to do the trick. I've tried %e, %f, %Lf, %g and %Lg, but it's not working. Here's the relevant part of my code I'm having issues with (the rest of the stuff I'm doing's working fine):

printf("**long double**\nStorage size: %d bytes \t Minimum value: %Lg 
\t Maximum value: %Lg\n", sizeof(long double), LDBL_MIN, LDBL_MAX);

I've included these header files at the top:

#include <stdio.h>
#include <limits.h>
#include <float.h>

I'm kinda lost here.

EDIT: Sorry for not explaining what the output is. I'm using %Lg right now, and I'm getting this in the terminal.

my output

Upvotes: 0

Views: 1311

Answers (2)

Bathsheba
Bathsheba

Reputation: 234635

You should use "%Lf" or "%Lg" for a long double, and %zu (since and including C99) for the sizeof return type size_t.

Currently the behaviour of your program is undefined: %d is not appropriate for a size_t type.

Upvotes: 4

Pascal Cuoq
Pascal Cuoq

Reputation: 80265

While you should provide the obtained results in your question, your mistake likely is to use %d to print the result of sizeof, which should be printed with %zu.

This causes undefined behavior. For some platforms, the rest of printf's arguments, on the stack, will be misinterpreted because the width of int does not match the width of size_t.

Upvotes: 7

Related Questions