Hey,
does anyone know if it possible to have something like
#DEFINE NUM_DEC 4
printf("This is your value:%.NUM_DECf\n"); thanks in advance
Hey,
does anyone know if it possible to have something like
#DEFINE NUM_DEC 4
printf("This is your value:%.NUM_DECf\n"); thanks in advance
Short answer: a macro name that appears inside a string literal is not treated as source tokens to be expanded, so the literal "%.NUM_DECf" will not become "%.4f" at preprocess time. and were right about that, and /’s runtime approach (passing the precision as an argument to the formatter) is the usual, simple fix — see the printf spec for the runtime-precision * feature.
If a compile-time format is preferred, you can generate a format literal using the preprocessor stringizing operator. The common pattern uses a two-step macro so the numeric macro is expanded before it gets turned into a string:
#define DECIMALS 4
#define _STR(x) #x
#define STR(x) _STR(x)
#define FMT "%." STR(DECIMALS) "f"
double value = 3.141592653589793;
printf("This is your value: " FMT "\n", value); Why the two steps? The stringize operator (#) will not expand its macro argument, so the indirection forces expansion of DECIMALS into 4 before stringizing. This yields a compile-time literal like "%.4f" without runtime overhead.
Cautions:
DECIMALS is a complex expression (you’ll get the source text stringified, not its evaluated value).References: cppreference on preprocessing and macro stringizing (preprocessor overview) and the POSIX printf description for runtime * width/precision (printf).
Jump to Post— ArkM 1,090Hey,
does anyone know if it possible to have something like#DEFINE NUM_DEC 4 printf("This is your value:%.NUM_DECf\n");Something like?.. No, it's impossible: no printed value presented ;)
printf("This is your value: %.*f\n",NUM_DEC,3.14159265358);
Hey,
does anyone know if it possible to have something like#DEFINE NUM_DEC 4 printf("This is your value:%.NUM_DECf\n");
Something like?.. No, it's impossible: no printed value presented ;)
printf("This is your value: %.*f\n",NUM_DEC,3.14159265358); Hey,
does anyone know if it possible to have something like#DEFINE NUM_DEC 4 printf("This is your value:%.NUM_DECf\n");thanks in advance
Its impossible because anything other than data type specifiers and escape characters within double quotes of printf function is treated as string or character constant and is printed as it is in output.
The real question here is whether or not macros are evaluated within strings. The answer: nope. ArkM's example is the correct approach.
I believe, your real requirement, is to set variable precision for your floating point number.
If so
The bad news is you can't do it that way.
The good news is there is . printf("This is your value:%.*f", 4, 22./7); Follow the link to know how it works
Or are you looking for something like this?
#include<stdio.h>
#define st "String 2"
#include<math.h>
int main ()
{
printf("Str 1 "st);//equivalent to printf("str 1 %s", st)
return 0;
} We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.