what is the standard to declare constant variables in ANSI C? -


i teaching myself c going on c++ book , recoding problems in c. wanted know correct industry standard way of declaring variables constant in c. still use #define directive outside of main, or can use c++ style const int inside of main?

const in c different const in c++.

in c means object won't modified through identifier:

int = 42; const int *b = &a;  *b = 12; /* invalid, contents of `b` const */ = 12; /* ok, though *b changed */ 

also, unlike c++, const objects cannot used, instance, in switch labels:

const int k = 0; switch (x) {     case k: break; /* invalid use of const object */ } 

so ... depends on need.

your options are

  • #define: const uses preprocessor
  • const: not const
  • enum: limited int

larger example

#define const 42 const int konst = 42; enum /*unnamed*/ { fixed = 42 };  printf("%d %d %d\n", const, konst, fixed);  /* &const makes no sense */ &konst; /* can used */ /* &fixed makes no sense */ 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -