...
Wiki Markup |
---|
When used in program logic, literals can reduce the readability of source code. As a result, literals in general, and integer constants in particular, are frequently referred to as _magic numbers_ because their purpose is often obscured. Magic numbers may be constant values that represent either an arbitrary value (such as a determined appropriate buffer size) or a malleable concept (such as the age a person is considered an adult, which could change between geopolitical boundaries). Rather than embed literals in program logic, use appropriately named symbolic constants to clarify the intent of the code. In addition, if a specific value needs to be changed, reassigning a symbolic constant once is more efficient and less error prone than replacing every instance of the value \[[Saks 02|AA. C References#Saks 02]\]. |
The C programming language has several mechanisms for creating named, symbolic constants: const
-qualified objects, enumeration constants, and object-like macro definitions. Each of these mechanisms has associated advantages and disadvantages.
...
Code Block |
---|
const int max = 15; int a[max]; /* invalid declaration outside of a function */ const int *p; p = &max; /* a const-qualified object can have its address taken */ |
Wiki Markup |
---|
{{const}}\-qualified objects are likely to incur some runtime overhead |
\[[Saks 01b|AA. C References#Saks 02]\]. Most C compilers, for example, allocate memory for {{const}}\-qualified objects. {{const}}\-qualified objects declared inside a function body will have automatic storage duration. Consequently, if the compiler allocates storage for the object, it will be on the stack and this storage will need to be allocated an initialized each time the containing function is invoked. |
Enumeration Constants
Enumeration constants can be used to represent an integer constant expression that has a value representable as an int
. Unlike const
-qualified objects, enumeration constants do not consume memory. No storage is allocated for the value so it is not possible to take the address of an enumeration constant.
...
Enumeration constants do not allow the type of the value to be specified. An enumeration constant whose value can be represented as an int
is always an int
.
Object-
...
Like Macros
A preprocessing directive of the form:
...
Wiki Markup |
---|
defines an _object-like_ macro that causes each subsequent instance of the macro name to be replaced by the replacement list of preprocessing tokens that constitute the remainder of the directive \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\]. |
C programmers frequently define symbolic constants as object-like macros. For example, the code:
...
Code Block | ||
---|---|---|
| ||
#ifndef PORTNUMBER /* might be passed on compile line */
# define PORTNUMBER 1234
#endif
#ifndef HOSTNAME /* might be passed on compile line */
# define HOSTNAME "localhost"
#endif
/* ... */
if ( (ld = ldap_init(HOSTNAME, PORTNUMBER )) == NULL) {
perror("ldap_init");
return(1);
}
|
...
Note that this example does not check for invalid operations (taking the sqrt()
of a negative number). ) See FLP32-C. Prevent or detect domain and range errors in math functions for more information on detecting domain and range errors in math functions.
...
Wiki Markup |
---|
\[[Henricson 92|AA. C References#Henricson 92]\] Chapter 10, "[Constants|http://www.doc.ic.ac.uk/lab/cplus/c++.rules/chap10.html]" \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.3.2.1, "Lvalues, arrays, and function designators," Section 6.7.2.2, "Enumeration specifiers," and Section 6.10.3, "Macro replacement" \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.7, "Declarations" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "BRS Leveraging human experience" \[[Saks 01a|AA. C References#Saks 01]\] Dan Saks. [Symbolic Constants|http://www.embedded.com/story/OEG20011016S0116]. Embedded Systems Design. November, 2001. \[[Saks 01b|AA. C References#Saks 02]\] \[[Saks 01b02|AA. C References#Saks 02]\] Dan Saks. [Enumeration Constants vs. Constant Objects|http://www.embedded.com/columns/programmingpointers/9900402]. Embedded Systems Design. November, 2001. \[[Saks 02|AA. C References#Saks 02]\] Dan Saks. [Symbolic Constant Expressions|http://www.embedded.com/story/OEG20020124S0117]. Embedded Systems Design. February, 2002. \[[Summit 05|AA. C References#Summit 05]\] [Question 10.5b|http://c-faq.com/cpp/constvsdefine.html] |
...