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

Recommended Answers

All 4 Replies

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 another way. 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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.