Tag: c预处理器

在初始化数组时使用#define时的语法错误,以及作为C中函数的参数?

在初始化数组时使用#define #include #define TEST 1; int main(int argc, const char *argv[]) { int array[] = { TEST }; printf(“%d\n”, array[0]); return 0; } 编译抱怨: test.c: In function ‘main’: test.c:7: error: expected ‘}’ before ‘;’ token make: *** [test] Error 1 使用#define作为函数输入参数 #include #define TEST 1; void print_arg(int arg) { printf(“%d”, arg); } int main(int argc, […]

在C中显示#define值

我有一系列来自这种库文件头的#defines: typedef int Lib_error; #define LIB_ERROR_A ((Lib_error) 0x0000) #define LIB_ERROR_D ((Lib_error) 0x0100) #define LIB_ERROR_F ((Lib_error) 0x0200) #define LIB_ERROR_K ((Lib_error) 0x0300) #define LIB_ERROR_O ((Lib_error) 0x0400) #define LIB_ERROR_P ((Lib_error) 0x0500) #define LIB_ERROR_R ((Lib_error) 0x0600) #define LIB_ERROR_X ((Lib_error) 0x0700) #define LIB_ERROR_Y ((Lib_error) 0x0800) #define LIB_ERROR_M ((Lib_error) 0x0900) /* and so on */ 有什么方法我可以打印这些值,所以例如 uint MyError; /* printf(“Error = […]

C预处理器#if表达式

我对可以与C语言中的#IF预处理器一起使用的表达式类型感到困惑。 我尝试了以下代码,但它无法正常工作。 请解释并提供可与预处理器一起使用的表达式的示例。 #include #include #include int c=1; #if c==1 #define check(a) (a==1)?a:5 #define TABLE_SIZE 100 #endif int main() { int a = 0, b; printf(“a = %d\n”, a); b = check(a); printf(“a = %d %d\n”, a, TABLE_SIZE); system(“PAUSE”); return 0; }

如何在C中定义一个定义?

是否可以编写定义#define ? 例如: #define FID_STRS(x) #x #define FID_STRE(x) FID_STRS(x) #define FID_DECL(n, v) static int FIDN_##n = v;static const char *FIDS_##n = FID_STRE(v) 但反而: #define FID_DECL2(n, v) #define FIDN_##nv \ FIDS_##n FID_STRE(v) FID_DECL工作正常但创建了两个静态变量。 是否可以使FID_DECL2工作并定义两个定义?

C预处理器的工作

下面的代码如何工作,换句话说C预处理器的算法是什么? 这适用于所有编译器吗? #include #define ba #define a 170 int main() { printf(“%i”, b); return 0; }

如何将#defined string literal转换为宽字符串文字?

可能重复: 如何使用C预处理器将连接字符串转换为wide-char? 我有一个使用#define定义的字符串文字: #define B “1234\0″ 如何在编译时使用此定义来获取此宽字符串文字?: L”1234\0” (只是带有L的#define d字符串文字,使其成为一个宽字符串)。 我试过这个: #define MAKEWIDE(s) L##s 但这会产生LB

#define的数组格式(C预处理器)

可能是一个天真的问题 – 我曾经在20年前制定过程,并且自那时起就没有多少编码。 我对C preprocessor如何工作的记忆自那时起已经萎缩了…… 我正在编写一个非常简单的C程序,我试图声明一些静态全局数组,但是数组的大小将依赖于(在非平凡的方式上) MODE变量。 类似下面的简化示例。 两个快点:我知道我可以根据任何MODE所需的最大尺寸来调整arrays的大小,但我不想这样做,因为(与下面的简化示例不同)有时候这些维度中的一小部分会非常很大,而其他人很小。 此外,我想使用静态定义的全局数组 – 而不是在运行时动态分配它们。 我希望编译器在编译时具有大小。 //** Simplified example of what I’d like to do **// #define SIZE_LIST_1[5] = {2, 7, 23, 33, 12, 76} // I don’t think this is valid syntax #define SIZE_LIST_2[5] = {11, 65, 222, 112, 444} #define MODE 4 #define S1 SIZE_LIST_1[MODE] #define […]

在C中说“#define FOO FOO”有什么意义?

我遇到了一些C代码,其中作者在整个地方使用以下习语: typedef __int32 FOO_INT32; #define FOO_INT32 FOO_INT32 这样做有什么意义? typedef不应该足够吗? 对于那些有些不稳定的C编译器来说,这是一种解决方法吗?

将数组文字作为宏参数传递

这已经困扰了我一段时间,例如,如果我正在尝试编写此代码: // find the length of an array #define ARRAY_LENGTH(arr) (sizeof(arr)/sizeof(int)) // declare an array together with a variable containing the array’s length #define ARRAY(name, arr) int name[] = arr; size_t name##_length = ARRAY_LENGTH(name); int main() { ARRAY(myarr, {1, 2, 3}); } 代码给出了这个错误: :8:31: error: macro “ARRAY” passed 4 arguments, but takes just 2 因为它看到ARRAY(myarr, […]

‘#’在C中究竟做了什么?

我有一个程序。 #include #define f(a,b) a##b #define g(a) #a #define h(a) g(a) int main() { printf(“%s\n”,h(f(1,2))); printf(“%s\n”,g(f(1,2))); return 0; } 该程序正常工作并输出如下: 12 f(1, 2) 我不明白编译器如何给出这个输出。 a##b和#a中#的function是什么?