Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using #define to a string in C++

Tags:

c++

curl

While trying to upload an image with cURL, I am confused because of the code below.

#define UPLOAD_FILE_AS  "testImage.jpg"
static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;

What I want to understand is,

  1. exact type of defined UPLOAD_FILE_AS : array of char / string / or something else?
  2. exact operation performed in second line : After second line, buf_1 becomes "RNFR testImage.jpg". But second line only have a space between "RNFR" and UPLOAD_FILE_AS. I've never heard a space can replace "+" operator or merging function. How is this possible?
like image 668
HansLee Avatar asked Jan 26 '26 00:01

HansLee


1 Answers

  1. The definition of the macro is preprocessor-style, it is just the sequence of characters, which happen to be within "". There is no type. The macro is expanded before the compiler (with its notion of types) starts the actual compilation.

  2. C++ will always concatenate all character-sequences-within-"". "A" "B" will always be handled as "AB" during building. There is not operator, no implicit operator either. This is often used to have very long string literals, spanning several line in code.

like image 164
Yunnosch Avatar answered Jan 28 '26 16:01

Yunnosch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!