首页 > 文章列表 > 有关C编程的有趣事实

有关C编程的有趣事实

c语言 编程事实 有趣。
226 2023-09-03

在这里我们将看到一些有关 C 编程的有趣事实。如下所示。

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

示例< /h2>
#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
  • 数组[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
  • 我们可以使用 <: , :> 代替方括号 [,],使用 <%, %> 代替大括号 {,}。
  • ul>

    示例

    #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
    • 我们可以在 scanf() 中使用 %*d 忽略输入。

    示例

    #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