Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between the macros "#define STR(x) #x" and "#define STR(x) VAL(x)" with "#define VAL(x) #x"?

Tags:

c

When I use this code:

#include <stdio.h>
#define STR(x) #x

int main(void)
{
    printf(__FILE__ STR(__LINE__) "hello!\n");
    return 0;
}

it prints

hello.c__LINE__hello!

but when I use this:

#include <stdio.h>
#define STR(x) VAL(x)
#define VAL(x) #x

int main(void)
{
    printf(__FILE__ STR(__LINE__) "hello!\n");
    return 0;
}

it prints

hello.c7hello!

what's the difference between

#define STR(x) #x

and

#define STR(x) VAL(x)
#define VAL(x) #x
like image 853
vv1133 Avatar asked Nov 27 '11 04:11

vv1133


People also ask

What is the best macro for weight loss?

The best macros for fat loss According to McMaster University research, a 5 : 3.5 : 1.5 ratio of carbs, protein and fat (when coupled with doing a four-week workout programme) can deliver healthy fat loss results. And in fact, perhaps better results than when reducing carbs and increasing protein.

What are the 3 types of macros?

Carbohydrates, fat and protein are called macronutrients. They are the nutrients you use in the largest amounts.

How do you know which macros are right for you?

Work out how many grams of each macro you need to eat To work out how many grams of each you need, you multiply your total daily calories by 0.4 for protein and 0.3 for carbohydrates and fat. The protein and carbohydrate figures are then divided by 4 and the fat figure by 9.

Which macro ratio is best?

The acceptable macronutrient distribution ranges (AMDR) set forth by the Institute of Medicine of the National Academies recommend that people get (26): 45–65% of their calories from carbs. 20–35% of their calories from fats. 10–35% of their calories from proteins.


1 Answers

Arguments to macros are themselves macro-expanded, except where the macro argument name appears in the macro body with the stringifier # or the token-paster ##.

In the first case, the argument of STR is not macro-expanded, and so you just get the name of the LINE macro.

In the second case, the argument of STR is macro-expanded when it is substituted into the definition of VAL, and so it works -- you get the actual line number because the LINE macro is expanded.

like image 199
Anthony Blake Avatar answered Oct 26 '22 14:10

Anthony Blake