Types, Operators, and Expressions
Variables and constants are the basic data objects manipulated in a program. Declarations list the variables to be used, and state what type they have and perhaps what their initial values are. Operators specify what is to be done to them. Quote combine variable and constants to produce new values. The type of an object determines the set of values it can have and what operations can be performed on it [KR88, page 35].
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 (_).1 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.
Variables and constants are the basic data objects manipulated in a program. Declarations list the variables to be used, and state what type they have and perhaps what their initial values are. Operators specify what is to be done to them. Quote combine variable and constants to produce new values. The type of an object determines the set of values it can have and what operations can be performed on it [KR88, page 35].
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 (_).1 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.
Types class 2
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 numbers.
The numerical types come in several of sizes. Table 2.1 shows a list of C types and their typical.
The ISO standard states that identifiers for internal names (i.e., names with file-scope or less, see Chapter 5) must recognise at least the first 31 characters as significant—including letter case. However, external names (i.e., names with storage class extern, see Section 5.2) must consider at least the first 6 characters as significant, and these case insensitive. For example, externalVar1 might be seen as equivalent to eXtErNaLvar2. In practice, most implementations recognise far more characters of an external identifer than the standard minimum.
ISO C states that implementations must consider as unique those external identifiers whose spellings differ in the first six characters, not counting letter case. (Notice is also given that future versions of the standard could increase this limit.) However, by far the majority of implementations allow external names of at least 31 characters [HS95, page 22].
C Data Types
char usually 8-bits (1 byte)
int usually the natural word size for a
machine or OS (e.g., 16, 32, 64 bits)
short int at least 16-bits
long int at least 32-bits
float usually 32-bits
double usually 64-bits
long double usually at least 64-bits
Table 2.1: C data types and their usual sizes.
sizes, although the sizes may vary from platform to platform. Nearly all current machines represent
an int with at least 32-bits and many now use 64-bits. The size of an int generally represents the
natural word-size of a machine; the native size with which the CPU handles instructions and data.
With regard to size, the standard merely states that a short int be at least 16-bits, a long int at least 32-bit, and
short int ≤ int ≤ long int
The standard says nothing about the size of floating-point numbers except that
float ≤ double ≤ long double.
A program to print the range of values for certain data types is shown below. The parameters such as INT_MIN can be found in standard headers limits.h and float.h (also see, for example, [KR88, page 257] or [HS95, pages 112, 118]).
- #include <stdio.h>
- #include <limits.h> /* integer specifications */
- #include <float.h> /* floating-point specifications */
- /* Look at range limits of certain types */
- int main (void)
- {
- printf("Integer range:\t%d\t%d\n", INT MIN, INT MAX);
- printf("Long range:\t%ld\t%ld\n", LONG MIN, LONG MAX);
- printf("Float range:\t%e\t%e\n", FLT MIN, FLT MAX);
- printf("Double range:\t%e\t%e\n", DBL MIN, DBL MAX);
- printf("Long double range:\t%e\t%e\n", LDBL MIN, LDBL MAX);
- printf("Float-Double epsilon:\t%e\t%e\n", FLT EPSILON, DBL EPSILON);
- }
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.
- #include <stdio.h>
- int main (void)
- /* Print the size of various types in “number-of-chars” */
- {
- printf("void\tchar\tshort\tint\tlong\tfloat\tdouble\n");
- printf("%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\n",
- sizeof (void), sizeof (char), sizeof (short), sizeof (int),
- sizeof (long), sizeof (float), sizeof (double));
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 qualifiers2 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. An unsigned
type is always non-negative, and the MSB is part of the numerical value—doubling the maximum
representable value compared to an equivalent signed type. For example, a 16-bit signed short
can represent the numbers −32768 to 32767 (i.e., −215 to 215 − 1), while a 16-bit unsigned short
can represent the numbers 0 to 65535 (i.e., 0 to 216 −1). (For more detail on the binary representation
of signed and unsigned integers see Section 12.1.)
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 depen-
dent.
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 refers 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 hard-
ware; 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.
Finally, there is a type called void, which specifies a “no value” type. It is used as an argument
for functions that have no arguments, and as a return type for functions that return no value (see
Chapter 4).
No comments:
Post a Comment