2020年4月12日 星期日

C 語言 - enum

enum - 列舉

    enum direction
    {
        North, // 0
        South, // 1
        East,  // 2
        West   // 3
    };

可搭配 typedef

    typedef enum direction Direction;

    enum direction
    {
        North,
        South,
        East,
        West
    };

    int main(void)
    {
        Direction dest = East;

        return 0;
    }

參考資料 :
https://michaelchen.tech/c-programming/enumeration/

Related Posts:

  • C/C++ - 左值、右值 ( lvalue、rvalue )lvalue、rvalue 基本概念 左值 (lvalue) : 一個佔據某個特定記憶體的值。 右值 (rvalue) : 一個 expression 結束後就消失的值。 基本上這兩個定義包含了全部的值,非左即右,非右即左。 int var = 4; // var 參數佔據記憶體 = lvalue 4 = var; // 4 不佔據記憶體 = rvalue (var + 1) = 4; // var … Read More
  • C 語言 - 正規表示法實作 ( regex.h )regex.h (Linux 原生, Windows 再說)     Regex.h 實作主要分 3 階段,regcom, regexec, regfree。 // 要被批配的 buffer 跟一些參數 int status, len, i; char buf[1024], data[1024]; getdata(data); // 正規表示式的會要先 compile (regcomp… Read More
  • C/C++ - Constconst 基本用途     告知編譯器這個值為「常數」無法被修改。 一般作用 const int index = 5; index = 8; // error: assignment of read-only variable 'index' 作用於 * // const pointer char * const constptr = initptr; *constptr = 1… Read More
  • C 語言 - enum enum - 列舉 enum direction { North, // 0 South, // 1 East, // 2 West // 3 }; 可搭配 typedef typedef enum direction Direction; enum direction { North, So… Read More
  • C 語言 - sprintf / snprintf sprintf (char *s, const char *format, ...)     C 語言並沒有其他語言方便的 container 去作字串轉換,所以 sprintf 就顯得強大許多。原理就是 printf 但輸入到字串。但會有 overflow 的 issue 產生。 int main() { char str[5]; sprintf(str, "ABC"); … Read More

0 意見:

張貼留言

Popular Posts