代码之家  ›  专栏  ›  技术社区  ›  C. Ross trotttrotttrott

为什么大多数C开发人员使用define而不是const?[副本]

  •  88
  • C. Ross trotttrotttrott  · 技术社区  · 14 年前

    这个问题已经有了答案:

    在许多程序中 #define 与常量的作用相同。例如。

    #define FIELD_WIDTH 10
    const int fieldWidth = 10;
    

    我通常看到第一个表单比另一个表单更受欢迎,它依赖于预处理器来处理基本上是应用程序决策的内容。这种传统有什么原因吗?

    9 回复  |  直到 7 年前
        1
  •  152
  •   Bart van Ingen Schenau    14 年前

    const

    fieldWidth

        2
  •  20
  •   Boann    7 年前

    const static const extern

    #define #ifdef #

    #define SCALE 1
    
    ...
    
    scaled_x = x * SCALE;
    

    SCALE 1 x * 1 == x

    enum dummy_enum {
       constant_value = 10010
    };
    

        3
  •  13
  •   R.. GitHub STOP HELPING ICE    14 年前

    const static

        4
  •  8
  •   John Bode    14 年前

    fieldWidth const switch

    FIELD_WIDTH 10

        5
  •  6
  •   Jens Gustedt    14 年前

    int

    enum { fieldWidth = 10 };
    

        6
  •  4
  •   CashCow    14 年前

    enum { FIELD_WIDTH = 16384 };
    char buf[FIELD_WIDTH];
    

        7
  •  2
  •   Shlomi Loubaton    14 年前

        8
  •  1
  •   Community CDub    7 年前
        9
  •  0
  •   esoriano    9 年前