1

私たちのコードには、printfにハードコードされたバージョン情報が少なくとも20の異なるファイルに含まれています。 printf("Software version v11.2");これは、更新があるたびに20のファイルを変更することを意味します。

代わりに、マクロを使用して、それをcommon.hファイルに#includeして、バージョンの更新で1つのマクロを変更するだけにします。それだけです。

私は次のようなことを試しました:

#include <stdio.h>
#define VERSION "v11.2"
int main()
{
  printf("Trying to print macro: ", VERSION);
}

しかし、このスタイルの「文字列」「文字列」は、CではなくJavaで機能します。それを実現する方法はありますか?

コンパイルにはgccを使用します。

注:このマクロは、変数を使用できない一般的な* .rcファイルでも使用され、これらのrcファイルはSQLクエリを使用して解析されます。したがって、char ver []="v11.2"のような変数は使用できません。

4

4 に答える 4

11

ここに2つの可能な解決策があります。

#include <stdio.h>
#define VERSION "v11.2"
int main()
{
  // Let printf insert the string when doing the output.
  printf("Trying to print macro: %s\n", VERSION);
  // Let the compiler concatenate the strings.
  puts("Trying to print macro: " VERSION);
  // Let the compiler concatenate the strings, can be assigned to a variable.
  const char buf[] = "Trying to print macro: " VERSION;
  puts(buf);
}
于 2012-08-09T09:35:22.700 に答える
0
printf("Trying to print macro: %s", VERSION);
于 2012-08-09T09:35:14.097 に答える
0

文字列ですが、動作する%sはずです。

int main()
{
  printf("Trying to print macro: %s", VERSION);
}
于 2012-08-09T09:35:18.940 に答える
0

これを試して

#define PRINT(format,args...)\
\
    do { \
        printf(" your data...");\
        } \
    } while(0)
于 2012-08-09T10:23:03.313 に答える