Tips, Tricks, Traps
Embedded Systems in Ukraine
Web: http://embedded.in.ua Email: email@example.com Twitter: embedded_in_ua
• A null pointer is a pointer that points to no valid location by having a
value of 0. A null pointer is known not to point to any object; an
uninitialized pointer might point anywhere.
• The "NULL" and "0" are interchangeable. But any usage of "NULL" (as
opposed to "0") should be considered a gentle reminder that a pointer is
• The preprocessor macro NULL is #defined with value 0. Or (void *) 0,
which will not work in non-pointer contexts.
“char *a” and “char a”
• Arrays automatically allocate space, but can't be relocated or resized.
• Pointers must be explicitly assigned to point to allocated space, but can
be reassigned (i.e. pointed at different objects) at will, and have many
other uses besides serving as the base of blocks of memory.
“extern” in a function declaration
• The extern keyword means "declare without defining". In other words, it
is a way to explicitly declare a variable, or to force a declaration without a
• It can be used as a stylistic hint to indicate that the function's definition is
probably in another source file, but there is no formal difference between
“extern int f();” and “int f();”
Right type to use for boolean values in C? (1)
• The initial standards for the C language (1972) provided no Boolean type;
and, to this day, Boolean values are commonly represented by integers in
• The preprocessor macros TRUE and FALSE are used for code readability,
not because the underlying values might ever change.
• The approach ("Boolean values are just integers") was retained in all later
versions of C. Some of its dialects, like C99, provide standard definitions
of a Boolean type as a synonym of int and macros for "false" and "true"
as 0 and 1, respectively.
Right type to use for boolean values in C? (2)
• The comparison operators ('>', '==', etc.), logical operators ('&&', '||', '!',
etc.) and condition-testing statements ('if', 'while') are defined to return a
signed integer (int) result, either zero (for false) or 1 (for true).
• A good rule of thumb is to use TRUE and FALSE (or the like) only for
assignment to a Boolean variable or function parameter, or as the return
value from a Boolean function, but never in a comparison.
"#define" and "const"?
• The "const" in C does not mean something is constant. It just means a
variable is read-only. But all other attributes of variable persist: it has
allocated storage, and this storage may be addressed. So any code do not
just use it as literal, but refers to it by accessing to specified memory
• In places where the compiler requires a true constant, using a const
variable is just not possible.
• The macro, which after preprocessing expands to the constant
expression; and is known at compile time, so it can be used for array
dimensions, case labels, etc.
Defining versus declaring
• When you declare a variable, you are telling the compiler that the
variable was defined elsewhere. You are just telling the compiler that a
variable by that name and type exists, but the compiler should not
allocate memory for it since it is done somewhere else.
• When you define a variable, you are telling the compiler to allocate
memory for that variable, and possibly also to initialize its contents to
• Why do most C developers use define instead of const? - Stackoverflow
• External variable - Wikipedia, the free encyclopedia
• Boolean data type - Wikipedia, the free encyclopedia
• Boolean Expressions and Variables - FAQ in comp.lang.c
• Null Pointers - FAQ in comp.lang.c