CHAPTER 1 C Programing
CHAPTER 1 C Programing
III- Identifiers
Identifiers (i.e., variable names, function names, etc) are made up of letters and
digits, and are
case-sensitive. The first character of an identifier must be a letter, which includes
underscore (_). The C language has 32 keywords which are reserved and may not
be used as identifiers (eg, int,while, etc). Furthermore, it is a good idea to avoid
redefining identifiers used by the C standard library (such as standard function
names, etc).
Style Note. Use lowercase for variable names and uppercase for symbolic
constants.
Local variable names should be short and external names should be longer and more
descriptive. Variable names
can begin with an underscore (_), but this should be avoided as such names, by convention,
are reserved for library implementations.
IV- Types
C is a typed language. Each variable is given a specific type which defines what values it can
represent, how its data is stored in memory, and what operations can be performed on it. By
forcing the programmer to explicitly define a type for all variables and interfaces, the type
system enables
the compiler to catch type-mismatch errors, thereby preventing a significant source of bugs.
There are three basic types in the C language: characters, and integer and floating-point
-integer: int x;
- character: char x;
floating point (approximate representation for real numbers): float x
The numerical types come in several of sizes. Table 2.1 shows a list of C types and their
typical
Note. The size of a type in number of characters (which is usually equivalent to number of
bytes)
can be found using the sizeof operator. This operator is not a function, although it often
appears
like one, but a keyword. It returns an unsigned integer of type size_t, which is defined in
header-file
stddef.h.
The keywords short and long are known as type qualifiers because they affect the size of a
basic
int type. (The qualifier long may also be applied to type double.) Note, short and long, when
used on their own as in
short a;
long x;
are equivalent to writing short int and long int, respectively
Other type qualifiers are signed,
unsigned, const, and volatile. The qualifiers signed or unsigned can apply to char or any
integer type. A signed type may represent negative values; the most-significant-bit (MSB) of
the
number is its sign-bit, and the value is typically encoded in 2’s-complement binary.
Note. Integer types are signed by default (e.g., writing short is equivalent to writing signed
short int). However, whether plain char’s are signed or unsigned by default is machine dependent.
The qualifier const means that the variable to which it refers cannot be changed.
const int DoesNotChange = 5;
DoesNotChange = 6; /* Error: will not compile */
The qualifier volatile refe
rs to variables whose value may change in a manner beyond the normal
control of the program. This is useful for, say, multi-threaded programming or interfacing to
hardware; topics which are beyond the scope of this text. The volatile qualifier is not directly
relevant
to standard-conforming C programs, and so will not be addressed further in this text.
IV- Constants
Constants can have different types and representations. This section presents
various constant types
by example. First, an integer constant 1234 is of type int. An constant of type long
int is suffixed by an L, 1234L; (integer constants too big for int are implicitly
taken as long). An unsigned int is suffixed by a U, 1234U, and UL specifies
unsigned long.
Integer constants may also be specified by octal (base 8) or hexadecimal (base 16)
values, rather than decimal (base 10). Octal numbers are preceded by a 0 and hex
by 0x. Thus, 1234 in decimal is equivalent to 02322 and 0x4D2. It is important to
remember that these three constants represent exactly the same value (0101 1101
0010 in binary). For example, the following code
#include <stdio.h>
#include <stdlib.h>
Note. The #define symbol, like the #include symbol for file inclusion, is a preprocessor
command
(see Section 10.2). As such, it is subject to different rules than the core C language.
Importantly,
the # must be the first character on a line; it must not be indented.
Another form of symbolic constant is an enumeration, which is a list of constant integer
values.
For example,
enum Boolean { FALSE, TRUE };
The enumeration tag Boolean defines the “type” of the enumeration list, such that a variable
may
be declared of the particular type.
enum Boolean x = FALSE;
If an enumeration list is defined without an explicit tag, it assumes the type int. 4 For
example,
enum { RED=2, GREEN, BLUE, YELLOW=4, BLACK };
int y = BLUE;
The value of enumeration lists starts from zero by default, and increments by one for each subsequent
member (e.g., FALSE is 0 and TRUE is 1). List members can also be given explicit integer values,
and non-specified members are each one greater than the previous member (e.g., RED is 2, GREEN
is 3, BLUE is 4, YELLOW is 4, and BLACK is 5).
VI- Declarations
All variables must b e declared before they are used. They must be declared at the top of a
block (a
section of code enclosed in brackets { and }) before any statements. They may be initialised
by a
constant or an expression when declared. The following are a set of example declarations
Assignment Operators
Expressions involving the arithmetic or bitwise operators often involve the assignment
operator = (for example, z = x + y). Sometimes in these expressions, the left-hand-side
variable is repeated immediately on the right (e.g., x = x + y). These types of expression can
be written in the compressed form x += y, where the operator += is called an assignment
operator.
The binary arithmetic operators, +, −, *, /, and %, each have a corresponding assignment
operator +=, -=, *=, /=, and %=. Thus, we can write x *= y + 1 rather than x = x * (y + 1) .
For we mention also the bitwise assignment op erators: &=, |=, ^=, <<=, and >>=. We
return to the bitwise operators in Chapter 12.