Cover | Table of Contents
http://ansi.org/public/std_info.html.
http://ansi.org/public/std_info.html.
main() and
showPage()
, and prints the beginning of a text
file to be specified on the command line when the program is started.
#include
directive.
showPage() in Figure 1-1 could also be placed before the function
main(). A function cannot be defined within
another function, however.
void. The basic types consist of the
integer types and the floating
types.
signed
char
, short
int
(or short),
int
, long
int
(or long), and long long
int
(*) (or
long long
(*)). For each
of these types there is a corresponding
unsigned integer type with the same
storage size. The unsigned type is designated by the prefix
unsigned in the type specifier, as in
unsigned int.
char
, signed char, and
unsigned char
are formally different. Depending on the
compiler settings, however, char is equivalent
either to signed char or to unsigned
char. The prefix signed has no meaning
for the types short, int,
long, and long
long
(*), however, since they are
always considered to be signed. Thus short and
signed short specify the same type.
char <=
short <= int <=
long <= long
long
(*). Furthermore, the size
of type short is at least 2 bytes,
long at least 4 bytes, and long
long at least 8 bytes. Their value ranges for a given
implementation are found in the header file
limits.h
.
_Bool
to represent Boolean
values. The Boolean value true is represented by
1 and false by
0. If the header file
stdbool.h
has been included, then
bool can be used as a synonym for
_Bool and the macros true and
false for the integer constants
int, long, unsigned long, long long(*).
int, unsigned int, long, unsigned long, long long(*), unsigned long long(*).
int.
The type can also be explicitly specified by one of the suffixes
L or l (for
long),
LL
(*) or
ll
(*) (for
long long
(*)), and/or
U or u (for
unsigned). Table 1-6 provides
some examples.
|
Decimal
|
Octal
|
|---|
void, a value. Some examples of expressions
follow:
4 * 512 // Type: int
printf("An example!\n") // Type: int
1.0 + sin(x) // Type: double
srand((unsigned)time(NULL)) // Type: void
(int*)malloc(count*sizeof(int)) // Type: int *
*, /, and %,
for example, take precedence over + and
-. In other words, the usual rules apply for the
order of operations in arithmetic expressions. For example:
4 + 6 * 512 // equivalent to 4 + (6 * 512)
(4 + 6) * 512
|
Priority
|
Operator
|
Grouping
|
|---|---|---|
|
1
|
() [] -> .
|
left to right
|
|
2
|
! ~ ++ -- + -
(type) * & sizeof
|
right to left
|
|
3
|
double to
float, for example.
_Bool,
char, unsigned char,
short, and unsigned short, as
well as bit-fields, can be used in expressions wherever operands of
type int or unsigned int are
permissible. In such cases, integer
promotion
is performed on the operands: they are
automatically converted to int or
unsigned int. Such operands are converted to
unsigned int only if the type
int cannot represent all values of the original
type.
int. If c is
a variable of type char, then its value in the
expression:
c + '0'
int before the addition takes place.&& and
||.
{[list of declarations][list of statements]}
{ int i = 0; /* Declarations */
static long a;
extern long max;
++a; /* Statements */
if( a >= max)
{ . . . } /* A nested block */
. . .
}
[expression] ;
y = x; // Assignment
for ( i = 0; str[i] != '\0'; ++i ) ; // Empty statement
[storage class] type D1 [, D2, ...];
extern,
static, auto, or
register.
void, enum type (enumeration),
struct or union type, or
typedef
name.
const.
char letter; int i, j, k; static double rate, price; extern char flag;
float dollars = 2.5F; // a variable of type float
dollars designates a region in memory
with a size of 4 bytes. The contents of these four bytes are
interpreted as a floating-point number, and initialized with the
value 2.5.
|
Specifier
|
|---|
enum
; for example:
enum toggle { OFF, ON, NO = 0, YES };
toggle is the
tag
of this
enumeration. This enumeration defines the identifiers in the list
(OFF, ON,
NO, and YES) as constants with
type int.
NO = 0 in the example above.
Identifiers for which no explicit value is specified are assigned a
value automatically based on their position in the list, as follows:
An enumerator without an explicit value has the value
0 if it is the first in the list; otherwise its
value is 1 greater than that of the preceding enumerator. Thus in the
example above, the constants OFF and
NO have the value 0, while
ON and YES have the value
1.
enum toggle t1, t2 = ON;
t1 and
t2 as variables with type enum
toggle, and also initializes t2 with the
value ON, or 1.
enum { black, blue, green, cyan, red, magenta, white };main()
, which is the first function executed
when the program starts. All other functions are subroutines.
extern double pow();
pow() is declared as a function that returns
a value with type double. Because function names
are external names by default, the storage class specifier
extern can also be omitted.
int.
pow() in the
example above contains no information about the number and type of
the function's parameters. Hence the compiler has no
way of testing whether the arguments supplied in a given function
call are compatible with the function's parameters.
This missing information is supplied by a function prototype.
double pow( double, double ); // prototype of pow()
pow() expects two arguments of type
double, and returns a result of type
double. Each parameter type may be followed by a
parameter name. This name has no more significance than a comment,
however, since its scope is limited to the function prototype itself.
For example:
double pow( double base, double exponent );
void.
For example:
void func1( char *str ); // func1 expects one string
// argument and has no return
// value.#. If
the directive is long, it can be continued on the next line by
inserting a backslash (\) as the last character
before the line break.
#define
directive is used
to define
macros.
define name[(parameter_list)] [replacement_text]
#define BUF_SIZE 512 // Symbolic constant #define MAX(a,b) ((a) > (b) ? (a) : (b))
BUF_SIZE and
MAX. If the replacement text is a constant
expression, the macro is also called a symbolic
constant
. Macros can also be nested; a macro,
once defined, can be used in another macro definition.
MAX is used in an expression, or when complex
expressions replace the parameters a and
b. For example, the preprocessor replaces the
macro invocation:
result = 2 * MAX( x, y & 0xFF );
result = 2 * ( (x) > (y & 0xFF) ? (x) : (y & 0xFF) );
# (called
the hash or stringizing operator). In this case, the preprocessor
sets the corresponding argument in quotation marks, thus converting
it into a string.
assert.h |
inttypes.h(*) |
signal.h |
stdlib.h |
complex.h(*) |
iso646.h(*) |
stdarg.h |
string.h |
ctype.h |
limits.h |
stdbool.h(*) |
tgmath.h(*) |
errno.h |
locale.h |
stddef.h |
time.h |
fenv.h(*) |
math.h |
stdint.h(*) |
wchar.h(*) |
float.h |
setjmp.h |
FILE
that contains information about the
stream. This information includes the address of the buffer, the
number of bytes not yet read, and other information about the file
itself. The file pointer is used to identify the file in all
subsequent operations.
stdin
stdout
stderr
stdin is generally associated with the keyboard,
while stdout and stderr are
associated with the display, unless redirection has been performed
using the function freopen() or by the environment
in which the program is running.
FILE structure. When the file is opened, the file
position is 0. It is increased by 1 with every character that is read
or written. Random file access is achieved by means of functions that
adjust the current file position.
|
Type
|
Minimum
|
Maximum
|
Maximum of the unsigned type
|
|---|---|---|---|
char |
CHAR_MIN |
CHAR_MAX |
UCHAR_MAX |
signed char |
SCHAR_MIN |
SCHAR_MAX | |
short |
SHRT_MIN |
SHRT_MAX |
USHRT_MAX |
int |
INT_MIN |
INT_MAX |
UINT_MAX |
long |
LONG_MIN |
int and long are declared in
stdlib.h.
int rand( void );
0 and RAND_MAX. The constant
RAND_MAX has a value of at least 32767, or
215 - 1.
void srand( unsigned n );
rand() generate a new
sequence of random numbers.
int abs( int x );
div_t div( int x, int y );
div_t, whose members
quot (the quotient) and rem
(the remainder) have type int. The type
div_t is defined in stdlib.h.
abs() and
div()) functions
labs()
,
llabs()
(*),
lldiv()
(*), and
ldiv()
are also provided for integers of type
long
long(*). Furthermore, the
functions imaxabs()
char are defined in the
header file ctype.h
. These functions, whose names begin
with is... or to..., accept a
one-character argument whose value is between 0 and 255, or
EOF.
is... functions, listed in Table 1-39, test whether the character is a member of a
specific category of characters. They return
"true," i.e., a non-zero value, if
the character is in the given category. If not, the return value is
0, or "false."
|
Category
|
Function
|
|---|---|
|
Letter
|
int isalpha( int c ); |
|
Lower-case letter
|
int islower( int c ); |
|
Upper-case letter
|
int isupper( int c ); |
|
Decimal digit
|
int isdigit( int c ); |
|
Hexadecimal digit
|
int isxdigit( int c ); |
|
Letter or decimal digit
|
int isalnum( int c ); |
|
Printable character
|
int isprint( int c ); |
|
Printable character other than space ' '
|
char array. A string is represented by a
char pointer that points to the first character in
the string.
char *strcat( char *s1, const char *s2 );
char *strchr( const char *s, int c );
int strcmp( const char *s1, const char *s2 );
0 to indicate whether
s1 is greater than, equal to, or less than
s2. A string is greater than another if
the first character code in it which differs from the corresponding
character code in the other string is greater than that character
code.
int strcoll( const char *s1, const char *s2 );
strxfrm(), then compares them using
strcmp() and returns the result.
void qsort(void *a, size_t n, size_t size,
int (*compare)(const void *,const void *));
void *bsearch( const void *key, const void *a,
size_t n, size_t size, int
(*compare)( const void*, const void* ) );
a. Usually this
function must be defined by you, the programmer. Its parameters are
two pointers to the array elements to be compared. The function must
return a value that is less than, equal to, or greater than
0 to indicate whether the first element is less
than, equal to, or greater than the second. To search or sort an
array of float values, for example, the following
comparison function could be specified:
int floatcmp( const void* p1, const void* p2 )
{ float x = *(float *)p1,
y = *(float *)p2;
return x <= y ? ( x < y ? -1 : 0) : 1;
}