Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expanding macro variable within quoted string

I have found two different things in two well known books in c, first one is "Formal parameters are not replaced in quoted string in macro expansion" - by K&R c language page 76

second one is a code,

#define PRINT(var,format) printf("variable is %format\n",var)
PRINT(x_var,f);

later macro invocation would be expanded as

printf("x_var is %f\n",x_var);
  • this is by programming in ansi c - E. balagurusamy at page 448.

Surely two citations are contradictory one with another. as far I know first one is true and my compiler giving me result so. But second book is also well known and popular. I want to know if there was such things in previous versions of c or the second citation is a false one.

like image 711
amin__ Avatar asked Jun 27 '12 17:06

amin__


People also ask

How do you resolve a macro variable in a quote?

Macro variables can be resolved between single quotation marks using the %STR function inside an %UNQUOTE function. The %UNQUOTE function is recommended to remove any remaining macro delta characters created by the %STR function.

How do you put quotes around a macro variable in SAS?

Macro variables in SAS won't resolve when they are in single quotes, '&myvar' . They need to be in double quotes, "&myvar" , in order to resolve properly. The %' inside of %str will place a single quote character (or apostrophe) in the text string by itself without causing it to be quoted.

How do you assign a macro to a variable value?

The simplest way to assign a value to a macro variable is to use the macro program statement %LET: %let dsname=Newdata; DSNAME is the name of the macro variable. Newdata is the value of the macro variable DSNAME.

What does the '#' symbol do in macro expansion?

The number-sign or "stringizing" operator (#) converts macro parameters to string literals without expanding the parameter definition. It's used only with macros that take arguments.


1 Answers

The second book is wrong: it is easy to check that the macro will not be expanded like that. However, you can get the effect that they describe by stringizing tokens using the # preprocessor operator:

#define PRINT(var,format) printf(#var" is %"#format"\n",var)

Now you can print your variable as follows:

int xyz = 123;
PRINT(xyz, d);

Here is a link to a working sample on ideone.

Note the addition of the double quotes before and after the '#format', and the '#' before 'var' and 'format'. The '#' operator causes the value of the variable to be made into a quoted string -- with double quotes of its own. This makes the replaced strings be four quoted strings in a row, that the C compiler recognizes as a request to concatenate into one string. Thus the strings: "xyz", " is %", "d" and "\n" are concatenated into: "xyz is %d\n"

(Note this example is different from the example in the original question in that the orignal example had "variable is..." where the answer replaced 'variable' with an instance of the 'var' macro argument)

like image 189
Sergey Kalinichenko Avatar answered Sep 23 '22 04:09

Sergey Kalinichenko