How can you set data type of #define to long double?

151 views Asked by At

I have a c-code where I define some variables for pre-processing using the #define command.

However, I think that there are ways to toggle between float and double data types by doing for example:

#define 1.0f     // float data type

or

#define 1.0     // double data type

Is there a way I can use it to set a variable of long double data types?

3

There are 3 answers

0
Brian61354270 On BEST ANSWER

Suffix the float constant with l or L to give it type long double:

1.0L

Do note that float constants only have meaning to C. As far as the preprocessor is concerned, that's just a string of four characters, no different from any other.

0
machine_1 On

define expects an identifier. Your code is not valid C code as you are using a constant literal in place of identifier; but to answer your question, if you want your floating-point literal to be of type long double, you can use the suffix l or capital: L:

void foo(long double x) { // ...}
 foo(1.0L);
1
Cristian Rodríguez On
constexpr long double foo = 1.0; /* In C23  */

Is another cleaner way to do it.