0

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

5
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by Prabakar
0

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);
0

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.

0

The real question here is whether or not macros are evaluated within strings. The answer: nope. ArkM's example is the correct approach.

0

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;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.