- auto
- storage class specifier (declaration)
- break
- statement (escape from switch or loop)
- case
- option prefix within switch statement
- char
- typename
- continue
- statement (branch to start of next loop)
- default
- option in switch statement
- do
- statement
- double
- typename
- else
- statement
- entry
- (reserved for the future use)
- extern
- storage class specifier
- float
- typename
- for
- statement
- goto
- goto label
- if
- statement
- int
- typename
- long
- typename
- register
- storage class specifier
- return
- functional statement
- short
- typename
- sizeof
- compile time operator
- static
- storage class specifier
- struct
- partial typename
- switch
- statement
- typedef
- statement
- union
- partial typename
- unsigned
- typename
- while
- statement
- enum
- partial typename: ordinal types only
- void
- typename
- const
- storage class specifier(no storage allocated)
- signed
- typename
- volatile
- storage class specifier
Preprocessor Directives
- #include
- include file for linking
- #define
- define a preprocessor symbol/macro
- #undef
- un-define a previously defnined symbol
- #if
- test for conditional compilation
- #ifdef
- (ditto)
- #ifndef
- (ditto)
- #else
- (ditto)
- #endif
- (ditto)
- #line
- debug tool
- #error
- debug tool
Header Files and Libraries
Header files contain macro definitions, type definitions and variable/ function declarations which are used in connection with standard libraries. They supplement the object code libraries which are linked at compile time for standard library functions. Some library facilities are not available unless header files are included. Typical names for header files are:
- stdio.h
- Standard I/O (libc).
- ctype.h
- Macro for character types.
- math.h
- Mathematical definitions (libm)
Constants
- Integer
- Characters 0..9 only
- Octal
- Prefix 0 (zero) chars 0..7 only
- Hexadecimal
- Prefix 0x (zero ex) chars a..f A..f 0..9
- Explicit Long
- Integer/Octal or Hexadecimal types can be declared long by writing L immediately after the constant.
- Character
- Declared in single quotes e.g. 'x' '\n'
- Float
- Characters 0..0 and one "." May also use scientific notation exponents with e or E preceding them. e.g. 2.14E12 3.2e-2
- Strings
- String constants are written in double quotes e.g. "This is a string" and have type pointer to character.
Primitive Data Types
- char
- Holds any character
- int
- Integer type
- short int
- Integer no larger than int
- long int
- Integer no smaller than int
- float
- Floating point (real number)
- long float
- Double precision float
- double
- (ditto)
- void
- Holds no value, uses no storage (except as a pointer)
Storage Classes
- auto
- Local variable (redundant keyword)
- const
- No variable allocated, value doesn't change
- extern
- Variable is defined in another file
- static
- Value is preserved between function calls
- register
- Stored in a register, if possible
- volatile
- Value can be changed by agents outside the program.
Identifiers
Idenitifiers may contain the characters: 0..9, A..Z, a..z and _ (the underscore character). Identifiers may not begin with a number. (The compiler assumes that an object beginning with a number is a number.)
Statements
A single statement is any valid string in C which ends with a semi colon. e.g.
a = 6;
printf ("I love C because...");
A compound statement is any number of single statements groued together in curly braces. The curly braces do not end with a semi colon and stand in place of a single statement. Any pair of curly braces may contain local declarations after the opening brace. e.g.
{
a = 6;
}
{ int a;
a = 6;
printf ("I love C because...");
}
Summary of Operators and Precedence
The highest priority operators are listed first.
Operator Operation Evaluated
() parentheses left to right
[] square brackets left to right
++ increment right to left
-- decrement right to left
(type) cast operator right to left
* the contents of right to left
& the address of right to left
- unary minus right to left
~ one's complement right to left
! logical NOT right to left
* multiply left to right
/ divide left to right
% remainder (MOD) left to right
+ add left to right
- subtract left to right
>> shift right left to right
<< shift left left to right
> is greater than left to right
>= greater than or equal to left to right
<= less than or equal to left to right
< less than left to right
== is equal to left to right
!= is not equal to left to right
& bitwise AND left to right
^ bitwise exclusive OR left to right
| bitwsie includive OR left to right
&& logical AND left to right
|| logical OR left to right
= assign right to left
+= add assign right to left
-= subtract assign right to left
*= multiply assign right to left
/= divide assign right to left
%= remainder assign right to left
>>= right shift assign right to left
<<= left shift assign right to left
&= AND assign right to left
^= exclusive OR assign right to left
|= inclusive OR assign right to left
Character Utilities
char ch;
- isalpha(ch)
- Is alphabetic a..z A..Z
- isupper(ch)
- Is upper case
- islower(ch)
- Is lower case
- isdigit(ch)
- Is in the range 0..9
- isxdigit(ch)
- Is 0..9 or a..f or A..F
- isspace(ch)
- Is white space character (space/newline/tab)
- ispunct(ch)
- Is punctuation or symbolic
- isalnum(ch)
- Is alphanumeric (alphavetic or number)
- isprint(ch)
- Is printable on the screen (and space)
- isgraph(ch)
- If the character is printable (not space)
- iscntrl(ch)
- Is a control character (not printable)
- isascii(ch)
- Is in the range 0..127
- iscsym(ch)
- Is a valid character for a C identifier
- toupper(ch)
- Converts character to upper case
- tolower(ch)
- Converts character to lower case
- toascii(ch)
- Converts character to ascii (masks off top bit)
Special Control Characters
Control characters are invisible on the screen. They have special purposes usually to do with cursor movement and are written into an ordinary string or character by typing a backslash character \ followed by some other character. These characters are listed below.
- \b
- backspace BS
- \f
- form feed FF (also clear screen)
- \n
- new line NL (like pressing return)
- \r
- carriage return CR (cursor to start of line)
- \t
- horizontal tab HT
- \v
- vertical tab (not all versions)
- \"
- double quotes (not all versions)
- \'
- single quote character '
- \\
- backslash character \
- \ddd
- character ddd where ddd is an ASCII code given in octal or base 8.
Input/Output Functions
- printf ()
- Formatted printing
- scanf ()
- Formatted input analysis
- getchar()
- Get one character from stdin file buffer
- putchar()
- Put one charcter in stdout file buffer
- gets ()
- Get a string from stdin
- puts ()
- Put a string in stdout
- fprintf()
- Formatted printing to general files
- fscanf()
- Formatted input from general files
- fgets()
- Get a string from a file
- fputs()
- Put a string in a file
- fopen()
- Open/create a file for high level access
- fclose()
- Close a file opened by fopen()
- getc()
- Get one character from a file (macro?)
- ungetc();
- Undo last get operation
- putc()
- Put a character to a file (macro?)
- fgetc()
- Get a character from a file (function)
- fputc()
- Put a character from a file (function)
- feof()
- End of file . returns true or false
- fread()
- Read a block of characters
- fwrite()
- Write a block of characters
- ftell()
- Returns file position
- fseek()
- Finds a file position
- rewind()
- Moves file position to the start of file
- fflush()
- Empties file buffers
- open()
- Open a file for low level use
- close()
- Close a file opened with open()
- creat()
- Create a new file
- read()
- Read a block of untranslated bytes
- write()
- Write a block of untranslated bytes
- rename()
- Rename a file
- unlink()
- Delete a file
- remove()
- Delete a file
- lseek()
- Find file position
printf conversion specifiers
- d
- signed denary integer
- u
- Unsigned denary integer
- x
- Hexadecimal integer
- o
- Octal integer
- s
- String
- c
- Single character
- f
- Fixed decimal floating point
- e
- Scientific notation floating point
- g
- Use f or e, whichever is shorter
The letter l (ell) can be prefixed before these for long types.
scanf conversion specifers
The conversion characters for scanf are not identical to those for printf and it is important to distinguish the long types here.
- d
- Denary integer
- ld
- Long int
- x
- Hexadecimal integer
- o
- Octal integer
- h
- Short integer
- f
- Float type
- lf
- Long float or double
- e
- Float type
- le
- Double
- c
- Single character
- s
- Character string
Maths Library
These functions require double parameters and return double values unless otherwise stated. It is important to include math.h.
- ABS(x)
- Return absolute (unsigned) value. (macro)
- fabs(x)
- Return absolute (unsigned) value. (Function)
- ceil(x)
- Rounds up a "double" variable
- floor(x)
- Rounds down (truncates) a "double" variable.
- exp(x)
- Find exponent
- log(x)
- Find natural logarithm
- log10(x)
- Find logarithm to base 10
- pow(x,y)
- Raise x to the power y
- sqrt(x)
- Square root
- sin(x)
- Sine of (x in radians)
- cos(x)
- Cosine of (x in radians)
- tan(x)
- Tangent of (x in radians)
- asin(x)
- Inverse sine of x in radians
- acos(x)
- Inverse cosine of x in radians
- atan(x)
- Inverse tangent of x in radians
- atan2(x,y)
- Inverse tangent of x/y in radians
- sinh(x)
- Hyperbolic sine
- cosh(x)
- Hyperbolic cosine
- tanh(x)
- Hyperbolic tangent
goto
This word is redundant in C and encourages poor programming style. For this reason it has been ignored in this book. For completeness, and for those who insist on using it (may their programs recover gracefully) the form of the goto statement is as follows:
goto label;
label is an identifier which occurs somewhere else in the given function and is defined as a label by using the colon:
label : printf ("Ugh! You used a goto!");
Three Languages: Words and Symbols Compared
If you are already familiar with Pascal (Algol..etc) or BBC BASIC, the following table will give you a rough and ready indication of how the main words and symbols of the three languages relate.
C Pascal BASIC
= := =
== = =
*,/ *,/ *,/
/,% div, mod DIV, MOD
printf (".."); writeln ('..'); PRINT ".."
write ('..');
scanf ("..",a); readln (a); INPUT a
read (a);
for (x = ..;...;) for x := ...to FOR x = ...
{ begin
} end; NEXT x
while (..) while ...do N/A
{ begin
} end;
do N/A N/A
{
}
while (..);
N/A repeat REPEAT
until (..) UNTIL ..
if (..) ..; if ... then ... IF .. THEN..
else ...; else ....; ELSE
switch (..) case .. of N/A
{
case :
} end;
/* .... */ { ..... } REM .....
* ^ ? ! $
struct record N/A
union N/A N/A
The conditional expressions if and switch are essentially identical to Pascal's own words if and case but there is no redundant "then". BASIC has no analogue of the switch construction. The loop constructions of C are far superior to those of either BASIC or Pascal however. Input and Output in C is more flexible than Pascal, though correspondingly less robust in terms of program crashability. Input and Output in C can match all of BASICs string operations and provide more, though string variables can be more awkward to deal with.