Lecture 4
Lecture 4
Lecture 4
Tokens & Syntax
The compiler collects the characters of a
program into tokens.
• Tokens make up the basic vocabulary of a
computer language.
The compiler then checks the tokens to
see if they can be formed into legal
strings according to the syntax (the
grammar rules) of the language.
Characters Used in C Programs
Lowercase letters
• a b c . . . z
Uppercase letters
• A B C . . . Z
Digits
• 0 1 2 3 4 5 6 7 8 9
Other characters
• + - * / = ( ) { } [ ] < > ‘ “
• ! @ # $ % & _ ^ ~ \ . , ; : ?
White space characters
• blank, newline, tab, etc.
The Six Kinds of Tokens in ANSI C
Keywords
Identifiers
Constants
String Constants
Operators
Punctuators
Keywords
Keywords are C tokens that have a strict
meaning.
• They are explicitly reserved and cannot be
redefined.
ANSII C has 32 key words.
• Some implementations such as Borland’s C
or Microsoft’s C have additional key words.
ANSII C Keywords
auto do goto signed unsigned
break double if sizeof void
case else int static volatile
char enum long struct while
const extern register switch
continue float return typedef
default for short union
Identifiers
An identifier is a token:
• Composed of a sequence of letters, digits, and
the underscore character _
– Note: Variable names are identifiers
Lower- and uppercase letters are treated
as distinct.
Identifiers should be chosen so that they
contribute to the readability and
documentation of the program.
Special Identifiers
main
• C programs always begin execution at the
function main.
Identifiers that begin with an underscore
should be used only by systems
programmers
• Because they can conflict with system names.
The Length of Discriminated Identifiers
d decimal integer
s string
A Peculiarity of scanf ( )
With printf() the %f format is used to
print either a float or a double.
With scanf() the format %f is used to
read in a float, and %lf is used to read in
a double.
Another scanf() Peculiarity
When reading in numbers, scanf() will
skip white space characters (blanks,
newlines, and tabs).
When reading characters, white space is
not skipped.
The Return Value of scanf()
When the scanf() function reads in data
typed by a user, it returns the number
of successful conversions.
• scanf(“%d%d%d”, &first, &second, &third);
– Should return a value 3 if the user
correctly types three integers.
– Suppose the user enters 2 integers
followed by a string -- what happens?
– What does our system do?
Common Programming Errors
Failure to correctly terminate a comment.
Leaving off a closing double quote
character at the end of a string.
Misspelling or not declaring a variable.
Misspelling a function name.
Omitting the ampersand (&) with
scanf( ).
Interrupting Program Execution
An executing program on a UNIX system
can often be interrupted by entering a ^c
from the keyboard.
The kill command is another way of
ending program execution.
If your program is in an infinite loop you
will have to use one of these methods to
interrupt its execution.
How the Compiler
Handles Comments
/* This is a comment */
The compiler first replaces each
comment with a single blank.
Thereafter, the compiler either
disregards white space or uses it to
separate tokens.
System Considerations
Syntax (Compile -Time) Errors
• Syntax errors are caught by the compiler.
• The compiler attempts to identify the error
and display a helpful error message.
Run-Time Errors
• Errors that occur during program execution.
• Memory errors caused by not using the
address operator & with a scanf ( ) argument.
Style
Use white space and comments to make
your code easier to read and understand.
• Indent logical subgroups of code by 3 spaces.
Choose variable names that convey their
use in the program.
Place all #includes, #defines, main()s, and
braces { } -- that begin and end the body
of a function -- in column 1.