C language constants defined

  • Use const keyword

    • Format: const constant name Data type = a constant value;
    • About naming constants name: beginning with 'k', mixed case. E.g:const int kHoursInDay = 24
    • Note: In c, const modified constant is unsafe, you can be modified by the pointer.
  • Use macro definition

    • Format: #define constant name value

    • Naming convention: all caps, use an underscore between words _separated. E.g:#define HOURS_IN_DAY 24

    • Step-compiled, pre-defined macros will be expanded.

      Using gcc -E a.c -o a.ithe source code preprocessing

      can see has no macro definition, and 24 is assigned a variable.

Guess you like

Origin www.cnblogs.com/guanlibin/p/12454257.html