Determine #defined string length at compile time

后端 未结 4 2022
抹茶落季
抹茶落季 2021-02-07 01:28

I have a C-program (an Apache module, i.e. the program runs often), which is going to write() a 0-terminated string over a socket, so I need to know its length.

4条回答
  •  野性不改
    2021-02-07 01:52

    sizeof works at compile time

    #define POLICY "\n" \
       "\n" \
       "\n" \
       "\n" \
       "\0"
    
    char pol[sizeof POLICY];
    strcpy(pol, POLICY); /* safe, with an extra char to boot */
    

    If you need a pre-processor symbol with the size, just count the characters and write the symbol yourself :-)

    #define POLICY_LENGTH 78 /* just made that number up! */
    

提交回复
热议问题