首頁  >  文章  >  後端開發  >  有關C編程的有趣事實

有關C編程的有趣事實

WBOY
WBOY轉載
2023-09-06 12:41:051357瀏覽

有關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
  • #陣列[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</p><p>", array[5]);
   printf("5[array]: %d</p><p>", 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</p><p>", 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

以上是有關C編程的有趣事實的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除