Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing "int" as argument in macro

Tags:

c++

c

macros

This has been bugging me for some time, I came across this while solving some objective type questions in C.

#define SWAP(a,b,c) c t;t=a;a=b;b=t;

int main() {
    int x=10,y=20;
    SWAP(x,y,int);     
}

The code gives the correct answer: Working code

In C are we supposed to pass just a data type as an argument.This supposedly works here but I want to know how.Also two more questions related to this:

  1. If I want to swap using pointers, will it work
  2. Will this work if SWAP is defined as a function instead of a macro.
like image 577
Tushar Avatar asked Nov 04 '14 04:11

Tushar


People also ask

Can we pass a macro as argument?

Function-like macros can take arguments, just like true functions. To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro definition that make the macro function-like.

How can you pass arguments to a macro class 10?

To assign a macro that you pass arguments to a button, shape, image, or any object, you first right-click that object and click Assign Macro and then type the name of the macro and the argument, following the pattern described in the above examples, and then click OK. 'show_msg "I clicked a button!"'

What is __ Va_args __ C++?

macro expansion possible specifying __VA_ARGS__ The '...' in the parameter list represents the variadic data when the macro is invoked and the __VA_ARGS__ in the expansion represents the variadic data in the expansion of the macro. Variadic data is of the form of 1 or more preprocessor tokens separated by commas.

What are the types of arguments used with macros?

There are two types of arguments: keyword and positional. Keyword arguments are assigned names in the macro definition; in the macro call, they are identified by name. Positional arguments are defined after the keyword !


1 Answers

Macros are pre-processed before compilation and you can virtually write anything in macros that would be replaced. In function arguments, you can not pass data types as arguments.

Side note:

#define SWAP(a,b,c) do { c t;t=a;a=b;b=t; } while(0)

is a safer macro implementation than the one mentioned by you. Moreover name t is quite common. If either of the argument name is t, this won't work as expected, so better choose some rare name. Capital letters are usually preferred in macro definition.

for ex: #define SWAP(a,b,c) do { c MACRO_TEMP_;MACRO_TEMP_=a;a=b;b=MACRO_TEMP_; } while(0)

SWAP(x,y,int); Becomes c t;t=a;a=b;b=t; where all occurances of c are replaced with int, a with x and b with y. Resulting in: ìnt t; t = x; x = y; y = t;

To understand the macros better, you can see the pre-processed output of your code. Output on my computer:

$ cat q26727935.c
#define SWAP(a,b,c) c t;t=a;a=b;b=t;

int main() {
    int x=10,y=20;
    SWAP(x,y,int);     
}

$ gcc -E q26727935.c 
# 1 "q26727935.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "q26727935.c"


int main() {
    int x=10,y=20;
    int t;t=x;x=y;y=t;;
}
$ 
  1. Macro is replacement at pre-processor stage, so swap will work even with pointers, although this is superfluous.

  2. In function you can not pass data type as arguments, so it won't work.

like image 141
Gyapti Jain Avatar answered Nov 14 '22 23:11

Gyapti Jain