A compile error of usage of "#if"

Here is the code I wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MY_CONTENT "123hello123"

#define SEARCH(TARGET, CONTENT)   \
({int ret = 0; do {               \
    if (strstr(CONTENT, TARGET))  \
        ret = 1;                  \
    else                          \
        ret = 0;                  \
} while (0);  ret;})


int main()
{
#if (SEARCH("hello", MY_CONTENT))
    printf("I found hello\n");
#else
    printf("I am so sad\n");
#endif

}


In this code, I'd like to use the macro function "SEARCH" to control compile with different code.
But when I compile the code, it prompt me this error:
gg.c:18:32: error: token "{" is not valid in preprocessor expressions

Do any guys know this compile error?
Any reason you're using macros instead of normal code? Also, what's the point of a do/while when the while condition is zero?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char* const my_content = "123hello123";

int search(const char* target, const char* content)
{
    int ret = 0;
    return (strstr(content,target) != 0);
}

int main()
{
    if(search("hello", my_content ))
        printf("I found hello\n");
    else
        printf("I am so sad\n");
}
@Disch

https://stackoverflow.com/questions/154136/do-while-and-if-else-statements-in-c-c-macros

1
2
3
4
5
6
7
8
9
#define SEARCH(TARGET, CONTENT) \
{ \
    int ret = 0;\
    do { \
        if (strstr(CONTENT, TARGET)) \
            ret = 1; \
        else \
            ret = 0; \
} while (0) 

@Disch
I just want to compile different code according to the dynamic macro result.
The "printf("I found hello\n");" is just a example.

Maybe I can write the code like below:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    int sock = 0;
    if(search("hello", my_content ))
    {
        sock = socket(....)
    }
    else {
        sock = fopen(....)
    }
}





@Smac89
Thanks your code.
But the compile error still existent.
gg.c:8:1: error: token "{" is not valid in preprocessor expressions
Taking your original code, your #if condition becomes equivalent to
#if (( {int ret = 0; do { if (strstr(CONTENT, TARGET)) ret = 1; else ret = 0; } while (0); ret;} )) .

I'm pretty sure you can't have curly braces in an #if condition...

In any case, even if you could, the preprocessor wouldn't know what the heck you're trying to do. It doesn't really know anything about the language -- so #if <some code here> is meaningless.
@long double main

Thanks your reply.
So you mean, my idea can't be implemented use the macro control?
closed account (3hM2Nwbp)
C++ provides meta-flow-control through templates and generalized constant expressions. The use of macros to mimic such behavior is generally frowned upon (and rightly so).
@Luc Lieber
Do you have any material or link about the "meta-flow-control"?

Topic archived. No new replies allowed.