0

Please consider the following codes

#define FIRSTNAME ""
#define SECONDNAME "JOHN"
# define PATHSAVE(a) func(strcat(strcpy(tmpFileName, appDir), a))
int main() {
  PATHSAVE(FIRSTNAME SECONDNAME);
}

By analyzing I found out that value "John" is passed to the function PATHSAVE. By I couldnt understand why two parameters are used in this function PATHSAVE(FIRSTNAME SECONDNAME)

JibinNajeeb
  • 784
  • 1
  • 10
  • 31
  • You are not passing 2 parameters. You are passing one parameter which is the result of concatenation of `FIRSTNAME` and `SECONDNAME`. Different parameters are delimited by comma (`,`). – Algirdas Preidžius Nov 18 '16 at 12:45
  • Sending this [through the preprocessor *only*](http://stackoverflow.com/questions/4900870/can-gcc-output-c-code-after-preprocessing), and examining the output would probably answer your question. – WhozCraig Nov 18 '16 at 12:49
  • Looks like it is an example of string concatenation. – NathanOliver Nov 18 '16 at 12:49

3 Answers3

2

What you wrote will be expanded as follows

func(strcat(strcpy(tmpFileName, appDir), "" "JOHN"));
                                         ^^ ^^^^^^
                                         || ||||||
                                         || SECONDNAME
                                         ||
                                         FIRSTNAME

Passing two parameters to a macro require them to be separated by , and not by a space

Marco A.
  • 43,032
  • 26
  • 132
  • 246
1
PATHSAVE(FIRSTNAME SECONDNAME);

will expand to PATHSAVE("JOHN") as the preprocessor will concatinate the 2 strings together.

This will then be further expanded to

func(strcat(strcpy(tmpFileName, appDir), "JOHN"))
UKMonkey
  • 6,941
  • 3
  • 21
  • 30
1

You can use the c pre processor if you want to know what is going on.

I pasted your code in a file named ex.c, here is the output of:

cpp ex.c

# 1 "ex.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "ex.c"



int main() {
  func(strcat(strcpy(tmpFileName, appDir), "" "JOHN"));
}
Fernando Coelho
  • 237
  • 1
  • 8