-3

How to define a double constant in C ?

For float is like so:

#define powerFactor 1.00f

For double i dont know:

#define energy 55639.00xx

Should xx be d, ld, lf, nothing or compiler specific?

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
baldhead
  • 47
  • 5
  • 1
    Please [read this](https://stackoverflow.com/help/how-to-ask): "Before posting a question, we strongly recommend that you spend a reasonable amount of time researching the problem and searching for existing questions on this site that may provide an answer." So, what research did you do? – Andrew Henle Apr 27 '23 at 23:20
  • 1
    Consider taking a look at [cppreference](https://en.cppreference.com/w/cpp/language/floating_literal) – Blackgaurd Apr 27 '23 at 23:20
  • 3
    Hint: why do you need to append `f` for a `float` literal? What would happen without it? – jamesdlin Apr 27 '23 at 23:21
  • @Andrew Henle, I searched all over google and couldn't find it, and here too. – baldhead Apr 27 '23 at 23:25
  • What makes you think you need to add something on the end? Does `10` need something on the end to be an `int`? Anyway, `#define` just does token substitutions, and is not related to the syntax of C. – pmacfarlane Apr 27 '23 at 23:27
  • @jamesdlin, i am converting to bytes to send over a socket, and, for tests purposes. i only want to learn the right way to do it too. – baldhead Apr 27 '23 at 23:31
  • 1
    @Blackgaurd not the best source. There are no "floating point literals" in `C` language. Only string literals and compound literals – 0___________ Apr 27 '23 at 23:53
  • 1
    @Blackgaurd Wrong reference. That's for C++. This is the [C reference](https://en.cppreference.com/w/c/language/floating_constant). – Mark Adler Apr 28 '23 at 01:45
  • @jamesdlin "What would happen without it?" --> You may enjoy: [When does appending an 'f' change the value of a floating constant when assigned to a `float`?](https://stackoverflow.com/q/66631288/2410359). – chux - Reinstate Monica Apr 28 '23 at 02:15

3 Answers3

2

In C double doesn't have a suffix, according to the C17 standard chapter 6.4.4.2 paragraph 4. A floating point literal without a suffix will be of type double:

#define energy 55639.00

There are also other situations where double is assumed to be the "default" floating point type. For example the math functions would have no suffix for double arguments (sin), and would have other suffixes for other types (e.g. sinf for float); printf (but not scanf) will assume double by default too.

the busybee
  • 10,755
  • 3
  • 13
  • 30
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • Aside: `scanf()` is not a `double` vs. `float` issue, but a `double *` vs. `float *` one. – chux - Reinstate Monica Apr 28 '23 at 02:20
  • Also, the `double` type is actually used when a `float` type is passed as a variadic parameter. The floating-point `printf` conversion specifiers `%f`, `%g` and `%e`, unless told otherwise, accept `double` arguments: `%f` means fixed, not `foat`. – Kaz Apr 28 '23 at 09:42
2

How to define a double constant in C?
Should xx be d, ld, lf, nothing or compiler specific?

"nothing"
For compliant C compilers, a floating-point constant without a suffix is type double.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

To be 100% sure that it will be double:

#define energy ((double)55639.00L)

Some compilers have a command line option to treat float point constants without suffixes as float not double. For example gcc -fsingle-precision-constant

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Nope. Doesn't work. Whether I do `printf("%.15f\n", 3.141592653589793);` or `printf("%.15f\n", (double)3.141592653589793);` with that compiler option, the result is still `3.141592741012573`. If, however, I put an `L` on there for a _long_ double, then it works. Though you need the `(double)` in that case. `printf("%.15f\n", (double)3.14159265358979323L);` with that option gives `3.141592653589793`. gcc really should have a non-standard suffix for doubles to go along with that non-standard option. – Mark Adler Apr 28 '23 at 01:42
  • @MarkAdler amended – 0___________ Apr 28 '23 at 07:17
  • 1
    All you're doing is making that command line option useless. The command line option changes the language into a nonstandard C dialect. Coding defensively so that your code works the same in nonstandard dialects is a fool's errand except in narrowly boxed circumstances. – Kaz Apr 28 '23 at 09:52
  • @Kaz dejure ISO/IEC 9899:2018 is the Standard, defacto gcc is the Standard. – 0___________ Apr 28 '23 at 18:08
  • Firstly, gcc isn't any kind of standard because a churning mess cannot be considered a standard. Secondly, only gcc **invoked without obscure code generation (or even diagnostic) options** could possibly be a *de jure* language standard. I.e. the language "GNU C" could possibly be a de facto standard. With suitable options, GCC can reject `(A && B || C)` as an error. That doesn't make it a de facto standard that this construct is incorrect. – Kaz Apr 28 '23 at 23:57