c programming lecture 4. tokens & syntax b the compiler collects the characters of a program...

34
C Programming C Programming Lecture 4 Lecture 4

Upload: adele-cross

Post on 23-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

C ProgrammingC Programming

Lecture 4Lecture 4

Page 2: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Tokens & SyntaxTokens & Syntax

The compiler collects the characters of a The compiler collects the characters of a program into program into tokenstokens..• Tokens make up the basic vocabulary of a Tokens make up the basic vocabulary of a

computer language.computer language. The compiler then checks the tokens to The compiler then checks the tokens to

see if they can be formed into legal see if they can be formed into legal strings according to the strings according to the syntaxsyntax (the (the grammar rules)grammar rules) of the language. of the language.

Page 3: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Characters Used in C Characters Used in C ProgramsPrograms

Lowercase lettersLowercase letters• a b c . . . za b c . . . z

Uppercase lettersUppercase letters• A B C . . . ZA B C . . . Z

DigitsDigits• 0 1 2 3 4 5 6 7 8 90 1 2 3 4 5 6 7 8 9

Other charactersOther characters• + - * / = ( ) { } [ ] < > ‘ “ + - * / = ( ) { } [ ] < > ‘ “ • ! @ # $ % & _ ^ ~ \ . , ; : ?! @ # $ % & _ ^ ~ \ . , ; : ?

White space charactersWhite space characters• blank, newline, tab, etc.blank, newline, tab, etc.

Page 4: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

The Six Kinds of Tokens in The Six Kinds of Tokens in ANSI CANSI C

KeywordsKeywords IdentifiersIdentifiers ConstantsConstants String ConstantsString Constants OperatorsOperators PunctuatorsPunctuators

Page 5: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

KeywordsKeywords

KeywordsKeywords are are C tokensC tokens that have a strict that have a strict meaning.meaning.• They are They are explicitly reservedexplicitly reserved and cannot be and cannot be

redefined.redefined. ANSII C has 32 key words.ANSII C has 32 key words.

• Some implementations such as Borland’s C Some implementations such as Borland’s C or Microsoft’s C have additional key words.or Microsoft’s C have additional key words.

Page 6: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

ANSII C KeywordsANSII C Keywords

auto do goto signed unsignedauto do goto signed unsigned

break double if sizeof voidbreak double if sizeof void

case else int static volatilecase else int static volatile

char enum long struct whilechar enum long struct while

const extern register switch const extern register switch

continue float return typedefcontinue float return typedef

default for short uniondefault for short union

Page 7: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

IdentifiersIdentifiers

An identifier is a token:An identifier is a token:• Composed of a sequence of Composed of a sequence of lettersletters, , digitsdigits, ,

and the and the underscoreunderscore character _ character _– Note: Note: Variable namesVariable names are identifiers are identifiers

Lower- and uppercaseLower- and uppercase letters are treated letters are treated as as distinctdistinct..

Identifiers should be chosen so that they Identifiers should be chosen so that they contribute to the contribute to the readability and readability and documentationdocumentation of the program. of the program.

Page 8: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Special IdentifiersSpecial Identifiers

mainmain• C programs always begin execution at the C programs always begin execution at the

function main.function main. Identifiers that begin with an underscore Identifiers that begin with an underscore

should be used only by systems should be used only by systems programmersprogrammers• Because they can conflict with system names.Because they can conflict with system names.

Page 9: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

The Length of Discriminated The Length of Discriminated IdentifiersIdentifiers

On older systems only the On older systems only the first eightfirst eight characters of an identifier are characters of an identifier are discriminated.discriminated.• identifier_oneer_one and and identifiidentifier_twoer_two would would

be the same identifier.be the same identifier. In ANSI C, In ANSI C, at leastat least the the first 31first 31 characters characters

of an identifier are discriminated.of an identifier are discriminated.

Page 10: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

ConstantsConstants

Integer ConstantsInteger Constants• 25 and 025 and 0

Floating ConstantsFloating Constants• 3.14159 and 0.13.14159 and 0.1

Character ConstantsCharacter Constants• ‘‘a’ and ‘B’ and ‘+’ and ‘;’ but not “a” or a’ and ‘B’ and ‘+’ and ‘;’ but not “a” or

“B”“B”

Page 11: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Special Character Special Character ConstantsConstants

The The backslashbackslash is called the is called the escape escape charactercharacter..• The The newline characternewline character ‘\n’‘\n’ represents a represents a

single charactersingle character called newline. called newline.• Think of Think of \n\n as “ as “escapingescaping” the ” the usual meaningusual meaning

of n.of n. Enumeration constants will be discussed Enumeration constants will be discussed

later in the course.later in the course.

Page 12: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

String ConstantsString Constants

A sequence of characters enclosed in a pair of A sequence of characters enclosed in a pair of double quote marks, such as “abc” is a double quote marks, such as “abc” is a string constant, or a , or a string literal.

Character sequences that would have meaning Character sequences that would have meaning if outside a string constant are if outside a string constant are just a sequence just a sequence of charactersof characters when surrounded by double when surrounded by double quotes.quotes.

String constants are treated by the compiler as String constants are treated by the compiler as tokenstokens and the compiler provides the space in and the compiler provides the space in memory to store them.memory to store them.

Page 13: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Is it a String or Not a Is it a String or Not a String?String?

““this is a string constantthis is a string constant”” “”“” /* the null string *//* the null string */ “ ““ “ /* a string of blanks *//* a string of blanks */ ““ a = b + c; a = b + c; ““ /* is not executed */ /* is not executed */ ““ /* this is not a comment */ /* this is not a comment */ ““ /* /* ““ this is not a string this is not a string ““ */ */ ““ andand

neither is this neither is this ““ ‘‘aa’’ /* a character, not a string */ /* a character, not a string */

Page 14: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

The Mathematical The Mathematical OperatorsOperators

We looked at the mathematical operators We looked at the mathematical operators briefly in the 3rd class:briefly in the 3rd class:+ - * / %+ - * / %

In a C program we typically put white In a C program we typically put white space around binary operators to space around binary operators to improve readability.improve readability.a + ba + b rather than rather than a+ba+b

Page 15: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

The sizeof OperatorThe sizeof Operator

The C sizeof unary operator if used The C sizeof unary operator if used to find the number of bytes needed to find the number of bytes needed to store an object.to store an object.• sizeof(object) returns an integer that sizeof(object) returns an integer that

represents the number of bytes needed represents the number of bytes needed to store the object in memory.to store the object in memory.

Page 16: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

printf()printf()printfprintf((control stringcontrol string, , other argumentsother arguments););

The expressions in The expressions in other_argumentsother_arguments are evaluated and are evaluated and converted according to the converted according to the formatsformats in the in the control stringcontrol string and are then placed in the and are then placed in the output streamoutput stream..

printf(“%-14sprintf(“%-14sPayRate:PayRate: $$%-4.2f\n”, “James Smith”, 8.95);%-4.2f\n”, “James Smith”, 8.95);

James Smith James Smith Pay Rate: $Pay Rate: $8.958.95

Characters in the control string that are Characters in the control string that are not part of a not part of a formatformat are placed are placed directlydirectly in the output stream. in the output stream.

Page 17: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

The Formats in the Control The Formats in the Control StringString

printf(“Get set: printf(“Get set: %d %s %f %c%c%d %s %f %c%c\n”,\n”,

11, , “two”“two”, , 3.333.33, , ‘G’‘G’, , ‘O’‘O’);); %d%d Print Print 11 as a decimal number as a decimal number %s %s Print Print “two”“two” as a string as a string

– ““string” means a sequence of characters.string” means a sequence of characters.

%f%f Print Print 3.333.33 as a float as a float– decimal or floating-point numberdecimal or floating-point number

%c%c Print Print ‘G’‘G’ & & ‘0’‘0’ as characters. as characters.

Page 18: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

printf() Conversion printf() Conversion CharactersCharacters

ConversionConversioncharactercharacter How the corresponding argument is printedHow the corresponding argument is printedcc as a characteras a characterd,id,i as a decimal integeras a decimal integeruu as an unsigned decimal integeras an unsigned decimal integeroo as an unsigned octal integeras an unsigned octal integerx,Xx,X as an unsigned hexadecimal integeras an unsigned hexadecimal integeree as a floating-point number: 7.123000as a floating-point number: 7.123000ee+00+00EE as a floating-point number: 7.123000as a floating-point number: 7.123000EE+00+00gg in the shorter of the in the shorter of the ee-format or f-format-format or f-formatGG in the shorter of the in the shorter of the EE-format or f-format-format or f-formatss as a stringas a stringpp the corresponding argument is a pointer tothe corresponding argument is a pointer to

void; it prints as a hexadecimal number.void; it prints as a hexadecimal number.nn argument is a pointer to an integer intoargument is a pointer to an integer into

which the number of characters written so far is which the number of characters written so far is printed; the argument is not converted.printed; the argument is not converted.

%% with the format with the format %%%% a single % is written; there a single % is written; thereis no corresponding argument to be converted.is no corresponding argument to be converted.

Page 19: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

printf( ) Conversion printf( ) Conversion SpecificationsSpecifications

field width (optional)field width (optional)• An optional positive integerAn optional positive integer• If the converted argument has If the converted argument has fewer charactersfewer characters than than

the specified width, it will be padded with spaces on the the specified width, it will be padded with spaces on the left or right depending on the left or right justification.left or right depending on the left or right justification.

• If the converted argument has If the converted argument has more charactersmore characters, the , the field width will be field width will be extendedextended to whatever is required. to whatever is required.

precision (optional)precision (optional)• Specified by a period followed by a nonnegative Specified by a period followed by a nonnegative

integer.integer.• MinMinimum number of digits to be printed for imum number of digits to be printed for dd, , ii, , oo, , uu, ,

xx, and , and XX conversions. conversions.• MinMinimum number of digits to the right of the decimal imum number of digits to the right of the decimal

point for point for ee, , EE, and , and ff conversions. conversions.• MaxMaximum number of significant digits for imum number of significant digits for GG and and gg

conversions.conversions.• MaxMaximum number of characters to be printed for an imum number of characters to be printed for an ss

conversion.conversion.

Page 20: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

printf ( ) Exampleprintf ( ) Exampleprintfprintf((““Get set: Get set: %d %s %f %c%c%d %s %f %c%c\n\n””,, 11, , “two”“two”, , 3.333.33, , ‘G’‘G’, , ‘O’‘O’));;

The first argument is the control string The first argument is the control string ““Get set: %d %s %f %c%c\n”Get set: %d %s %f %c%c\n”

The The formatsformats in the control string are matched in the control string are matched (in order of occurrence) with the (in order of occurrence) with the other other argumentsarguments..

Page 21: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Use of printf ( )Use of printf ( )

printfprintf( ) is used for printing output. is used for printing output. When When printf( )printf( ) is called it is passed a list is called it is passed a list of arguments of the form:of arguments of the form:

control stringcontrol string & & other argumentsother arguments

The arguments to The arguments to printf( )printf( ) are separated are separated by commas.by commas.

Page 22: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Errors in printf ( ) FormatsErrors in printf ( ) Formats

A floating point format in a A floating point format in a printf ( ) statement is of the printf ( ) statement is of the form form %m.nf%m.nf• The value of The value of mm specifies the specifies the field field widthwidth, , notnot the number of digits to the number of digits to the left of the decimal point.the left of the decimal point.

• The value of The value of nn specifies the number specifies the number of digits to the of digits to the rightright of the of the decimal point.decimal point.

To specify To specify twotwo decimal digits to the decimal digits to the left of the decimal point and left of the decimal point and threethree to to the right, use the right, use %6.3f%6.3f..

Page 23: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Use of scanf()Use of scanf()

scanf() is analogous to printf(), but is scanf() is analogous to printf(), but is used for input rather than output.used for input rather than output.• scanf()in a program stops the execution scanf()in a program stops the execution

of the program while you type of the program while you type something in from the keyboard.something in from the keyboard.

Page 24: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

scanf ( ) Argumentsscanf ( ) Arguments

The first argument is a The first argument is a control stringcontrol string with with formatsformats similar to those used with similar to those used with printf().printf().• The The formatsformats determine how characters in the determine how characters in the

input stream (what you are typing) will be input stream (what you are typing) will be interpreted so they can be properly stored in interpreted so they can be properly stored in memory.memory.

Page 25: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Scanf Scanf ( )( )’s Other ’s Other ArgumentsArguments

After the control string, the other After the control string, the other arguments are arguments are addressesaddresses..

Example: assume Example: assume xx is declared as an is declared as an integer variable.integer variable.scanf(“%d”,scanf(“%d”, &&xx););The The & & is the address operator. It says “store is the address operator. It says “store

the value entered at the the value entered at the addressaddress of the of the memory location named xmemory location named x”.”.

Page 26: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

scanf ( ) Conversionscanf ( ) ConversionConversion How characters in theConversion How characters in theCharacter input stream are converted. Character input stream are converted.

c

d

f

lf

Lf

s

Character

decimal integer

floating-pint number (float)

floating-point number (double)

floating-point number (long double)

string

Page 27: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

A Peculiarity of scanf A Peculiarity of scanf ( )( )

With printf() the With printf() the %f%f format is used to format is used to print print eithereither a float or a double. a float or a double.

With scanf() the format With scanf() the format %f%f is used to is used to read in a read in a floatfloat, and , and %lf%lf is used to read in is used to read in a a doubledouble..

Page 28: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Another scanf() PeculiarityAnother scanf() Peculiarity

When reading in When reading in numbersnumbers, scanf() will , scanf() will skip white space characters (blanks, skip white space characters (blanks, newlines, and tabs).newlines, and tabs).

When reading When reading characterscharacters, white space is , white space is not skipped.not skipped.

Page 29: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

The Return Value of The Return Value of scanf()scanf()

When the scanf() function reads in data When the scanf() function reads in data typed by a user, it returns the typed by a user, it returns the number number of successful conversionsof successful conversions..• scanf(“%d%d%d”, &first, &second, &third);scanf(“%d%d%d”, &first, &second, &third);

– Should return a value 3 if the user Should return a value 3 if the user correctly types three integers.correctly types three integers.

– Suppose the user enters 2 integers Suppose the user enters 2 integers followed by a string -- what happens?followed by a string -- what happens?– What does our system do?What does our system do?

Page 30: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Common Programming Common Programming ErrorsErrors

Failure to correctly terminate a Failure to correctly terminate a comment.comment.

Leaving off a closing double quote Leaving off a closing double quote character at the end of a string.character at the end of a string.

Misspelling or not declaring a variable.Misspelling or not declaring a variable. Misspelling a function name.Misspelling a function name. Omitting the ampersand (Omitting the ampersand (&&) with ) with

scanf( ).scanf( ).

Page 31: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

Interrupting Program Interrupting Program ExecutionExecution

An executing program on a UNIX system An executing program on a UNIX system can often be interrupted by entering a can often be interrupted by entering a ^c^c from the keyboard.from the keyboard.

The The killkill command is another way of command is another way of ending program execution.ending program execution.

If your program is in an If your program is in an infinite loopinfinite loop you you will have to use one of these methods to will have to use one of these methods to interrupt its execution.interrupt its execution.

Page 32: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

How the Compiler How the Compiler Handles CommentsHandles Comments

/* This is a comment *//* This is a comment */

The compiler first replaces each The compiler first replaces each comment with a comment with a single blanksingle blank..

Thereafter, the compiler either Thereafter, the compiler either disregards white space or uses it to disregards white space or uses it to separate tokens.separate tokens.

Page 33: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

System ConsiderationsSystem Considerations

Syntax (Compile -Time) ErrorsSyntax (Compile -Time) Errors• Syntax errors are caught by the compiler.Syntax errors are caught by the compiler.• The compiler attempts to identify the error The compiler attempts to identify the error

and display a helpful error message.and display a helpful error message. Run-Time ErrorsRun-Time Errors

• Errors that occur during program execution. Errors that occur during program execution. • Memory errors caused by not using the Memory errors caused by not using the

address operator address operator && with a scanf ( ) with a scanf ( ) argument.argument.

Page 34: C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer

StyleStyle Use white space and comments to make Use white space and comments to make

your code easier to read and understand.your code easier to read and understand.• Indent logical subgroups of code by Indent logical subgroups of code by 3 spaces3 spaces..

Choose variable names that convey their Choose variable names that convey their use in the program.use in the program.

Place all Place all #include#includes, s, #define#defines, s, main()main()s, and s, and braces braces { }{ } -- that begin and end the body -- that begin and end the body of a function -- in of a function -- in column 1column 1..