这里我们将看到 C 语言中的卫生宏。我们知道 C 语言中宏的用法。但是有时,由于意外捕获标识符,它不会返回预期的结果。如果我们看到下面的代码,我们可以看到它不能正常工作。示例 #include #define INCREMENT(i) do { int a = 0; ++i; } while(0) main(void) { int a = 10, b = 20; //对 a 和 b 调用宏两次 INCREMENT(a); INCREMENT(b); printf("a = %d, b = %d", a, b); }预处理后的代码将如下所示 - 示例 #include #define ... 阅读更多
这里我们将看到 C 和 C++ 之间的一些不兼容性。一些可以使用 C 编译器编译的 C 代码,但在 C++ 编译器中无法编译。并且还会返回错误。我们可以使用一种语法定义函数,该语法可以选择地在参数列表之后指定参数类型。示例 #include void my_function(x, y) int x; int y; { // 在 C++ 中无效 printf("x = %d, y = %d", x, y); } int main() { my_function(10, 20); }输出 x = 10, y = 20输出 C++ 中的错误:- x 和 y 在此范围内未声明 在 C 语言或某些较旧版本的 C++ 中,... 阅读更多
这里我们将看到 C 或 C++ 中 nextafter() 和 nextforward() 函数的效果。这些函数存在于 math.h 或 cmath 库中。如果函数类似于 nextafter(a, b) 和 nextforward(a, b)。这些函数用于查找 a 之后朝 b 方向的下一个可表示值。nextforward() 具有更精确的第二个参数 b。示例 #include #include main () { //nextafter 函数() printf ("Smallest representable number after 0 towards 1 : %e", nextafter(0.0, 1.0)); printf ("Largest representable number before -1 towards 0 :%e", nextafter(0.0, -1.0)); printf ("Largest +ve representable number ... 阅读更多
这里我们将看到一些关于 C 语言编程的有趣事实。如下所示。有时,某些 switch 语句的 case 标签可以放在 if-else 语句中。示例 #include main() { int x = 2, y = 2; switch(x) { case 1: ; if (y==5) { case 2: printf("Hello World"); } else case 3: { //case 3 块 ... 阅读更多