Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C preprocessor concatenation to get number in exponential notation

Why does code:

#define EXPONENT(num, exp) num ## e ## exp
EXPONENT(1,1)
EXPONENT(1,-1)
EXPONENT(1,+1)

after preprocessing changes into:

1e1
1e- 1
1e+ 1

and not into

1e1
1e-1
1e+1

? I suspect it might be because -1,+1 are parsed as two tokens (?). However, how in that case obtain the latter result?

like image 910
kwitek Avatar asked Jul 12 '12 13:07

kwitek


1 Answers

You're right, -1 and +1 are two preprocessing tokens, hence only the first is pasted to the e.

For me,

#define EXPO(num, sign, ex) num ## e ## sign ## ex

EXPO(1,,1)
EXPO(1,-,1)
EXPO(1,+,1)

worked with gcc-4.5.1.

like image 182
Daniel Fischer Avatar answered Oct 19 '22 12:10

Daniel Fischer