C 编程的趣闻


在这里,将介绍 C 编程的一些有趣的事实。例如:

  • 有时,某些 switch 语句的 case 标签可以放在 if-else 语句中。

示例

#include <stdio.h>
main() {
   int x = 2, y = 2;
   switch(x) {
      case 1:
         ;
         if (y==5) {
            case 2:
               printf("Hello World");
         }
         else case 3: {
            //case 3 block
         }
   }
}

输出

Hello World
  • array[index] 可以写成 index[array]。原因是数组元素是使用指针算术访问的。array[5] 的值为 *(array + 5)。如果以相反的顺序(例如 5[array])编写,那么它也与 *(5 + array) 相同。

示例

#include <stdio.h>
main() {
   int array[10] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 110};
   printf("array[5]: %d
", array[5]);    printf("5[array]: %d
", 5[array]); }

输出

array[5]: 66
5[array]: 66
  • 我们可以在方括号 [,] 的位置使用 <: , :>,在花括号 {,} 的位置使用 <%, %>。

示例

#include <stdio.h>
main() <%
int array<:10:> = <%11, 22, 33, 44, 55, 66, 77, 88, 99, 110%>;
printf("array[5]: %d
", array<:5:>); %>

输出

array[5]: 66
  • 我们可以在某些奇怪的地方使用 #include。我们以一个文件 abc.txt 为例,其中包含一行“The Quick Brown Fox Jumps Over The Lazy Dog”。如果我们在 printf 语句后包含该文件,则我们可以打印该文件内容。

示例

#include <stdio.h>
main() {
   printf
   #include "abc.txt" ;
}

输出

The Quick Brown Fox Jumps Over The Lazy Dog
  • 我们可以使用 %*d 忽略 scanf() 中的输入。

示例

#include <stdio.h>
main() {
   int x;
   printf("Enter two numbers: ");
      scanf("%*d%d", &x);
   printf("The first one is not taken, the x is: %d", x);

}

输出

Enter two numbers: 56 69
The first one is not taken, the x is: 69

更新于:2019-07-30

332 次浏览

启动你的 职业

通过完成课程获得认证

开始
广告
© . All rights reserved.