Title: Announcements
1Announcements
- Programming assignment 1 is due by midnight.
- Late submissions accepted until Sunday midnight
- Quiz 1 is due by midnight.
- NO late submissions accepted
- Grade posting ID will be emailed to your engr
address (next week). - Programming Assignment 2 is available on the
course web page. - Due Friday, 10/17, by midnight.
2A Correction
- Last time I said that to print a symbol you
can use \ - This is wrong, need to say
- int n 37
- printf("here is 10 of d f", n, n0.10)
3Lexical Analysis - Tokens
- Keywords
- e.g int, return
- Identifiers
- e.g main, square, cube, PI
- Numeric literal constants
- e.g. 3 3.14159 -12
- String literal constants
- e.g. "Hello world\n"
- Operators
- e.g. - / ( )
- Punctuators
- e.g. , ( )
4Identifiers
- Letters, digits, and underscore, '_'
- Must start with letter or underscore.
- Upper or lower case, but identifiers are case
sensitive. - Used for names of variables, symbolic constants,
and functions. - Should be abstract
- Named for what they represent
- Meaningful
5Identifiers
- Correct
- myName, ticket_Count, acctBalance, _code
- Syntactically correct, but not good
- x, xyz, c2, __p
- Incorrect
- Not-good, 1size, my name, __
6Literal and symbolic numeric constants
- Integer literal constants
- e.g. 0 1 -5 320000
- decimal (base 10) 128
- octal (base 8) 0200
- hexadecimal (base 16) 0x80
- Floating point (real) literal constants
- e.g. 1.5 3.14
- Symbolic constants
- e.g. define PI 3.14159
- e.g. define MAX 600
7String literal constants
- Any character you can type inside
- e.g. This is a literal constant
- The backslash character \
- Used to include white space in strings
- \n represents New Line character
- \t represents Tab character
- Used to include " and \
- \" represents "
- \\ represents \
- e.g. \tA \literal\ with a \\ and a \nnew
line. - Which would display as
- A literal with a \ and a
- new line.
8Literal and symbolic string constants
- Literal
- printf("You've made an error\n")
- Symbolic
- define ERRMSG "You've made an error\n"
- printf(ERRMSG)
9Literal and symbolic character constants
- Character literal constants
- e.g. A a 5
- Symbolic constants
- e.g. define AT _at_
- e.g. define NEWLINE \n
10The ASCII Code
- American Standard Code for Information
Interchange. - Each character is assigned a code (number).
- ASCII codes go from 0 to 255
- 0 - 31 are control codes
- 32 - 126 are keyboard characters
- 65 90 are capital letters
- 97 122 are lower-case letters
- 48 57 are digits 0 9
- 127 is delete character
- 128 255 are graphics characters
11Characters are not Strings
- "A" is stored and treated differently from 'A'
- A is stored as two bytes 65 and 0
- A is stored as 65 only.
- "abc" is a legal string
- 'abc' is illegal -- a char variable or constant
is always only ONE character.
12Non-printing and hard-to-print character constants
- '\n' is new line character (two symbols represent
only one character, ASCII 10). - '\t' is tab character (ASCII 9).
- '\"' is double quote character
- '\\' is backslash character ('\' doesnt work,
'\\' must be used)
13Variables
- Memory location to store data.
- Must be declared before used.
- Declaration includes type and name.
- Basic numeric types are int, float (small real),
double (large real), char (small integer).
14Variable Declaration Examples
- int n / integers are stored in 4 bytes /
- / roughly -2 billion to 2 billion /
- int a, b 0 / note initialization along with
declaration / - float x 3.5, y / stored in 4 bytes /
- / 6 or 7 significant digits /
- double z 3.14159265358979 / stored in 8
bytes / - / 15 significant digits /
- char c 'A' / stored in one byte /
- / characters are numbers -- more about this
later /
15Operators Punctuators
- Arithmetic (binary) - /
- e.g. a b, 5 2
- Arithmetic (unary) -
- e.g. 25, -30
- Assignment (binary)
- e.g. n 5
- Grouping, etc. ( ) ,
16Expressions
- Expression -- Any combination of identifiers,
constants, operators and punctuators. - e.g. (a b) 3 - 20 / 3
17Precedence
- Precedence rank or priority.
- Some operators have priority over others (i.e
should be done first) - 1 2 3 same as 1 (2 3) (i.e. 7)
- has higher precedence than binary or -
- Parentheses force precedence.
- (1 2) 3 is equal to 9
18Associativity
- When operators have same precedence we must use
rules of associativity (left to right or right to
left) - - same precedence, left to right assoc.
- 123-45 means (((12)3)-4)5 7
- / same precedence, left to right assoc.
- 4 / 2 3 means (4/2)3 6 not 4/(23) 0
...
19Precedence /Associativity Table
- operation precedence associativity
- ( ) 1 L to R
- unary , unary - 2 R to L
- / 3 L to R
- - 4 L to R
- (assignment) 5 R to L
20Use Parentheses!!!!!!!!
- Best to force precedence and associativity with
parentheses. - Easier to read
- Everyone gets confused about these rules!
21Increment, Decrement
- Unary increment and decrement (highest
precedence) - a --a ('pre' versions)
- a a-- ('post' versions)
- Value in memory is incremented or decremented by
one. - Pre Version - value changed then used.
- Post Version - value used then changed. ...
22Increment, Decrement Examples
- Let b 2 and c 3.
- After assignment a (b) (c)
- the values will be a7, b3, c4
- Let b 2 and c 3.
- After the assignment a (b) (c)
- the values will be a5,b3,c4
- --i is a legal statement.
- Same as i i - 1
- Avoid expressions like
- a c c
23Assignment Operator
- An assignment has a value!
- a (b 0) same as a0 b0
- or a b 0 (because of right to left
associativity). - a (b 2) (c 3) is legal but not very
readable. - Don't do it! (c is 3, b is 2 and a is 5)
24Shortcut Assignment
- a 5 means a a 5
- Similarly
- a - 5 means a a - 5
- a 5 means a a 5
- a / 5 means a a / 5
- a 5 means a a 5
25Statements
- Simple Statement is an expression followed by a
semicolon. - val val - 1 / assignment statement/
- printf("d miles", val) / function call
statement / - Compound Statement block, i.e. multiple
statements surrounded by braces (curly brackets). - int main(void)
- int a 5
- printf("d", a)
- a a - 1
- return 0
-
compound statement
26Inputting Value from KB
- The function scanf() waits for input from
keyboard. - Very important
- the variables that will store the input values
must be designated with - Input can be integer (d), float (f),
double(lf), char (c) or string (s). - scanf(string, var1, var2, )
string contains conversion specifiers for var1,
var2, etc.
27Input Example
include ltstdio.hgt int main(void) int
a float x double z scanf("d f lf", a,
x, z) printf("d\nf\nf\n", a, x,
z) return 0
28printf formatting
- f can be used to print either float or double
variables. - Real number output formatting, n.mf
- m specifies number of digits to right of decimal
- n specifies minimum width of printing field
- if n is negative, then output is left justified
in field
29Formatting Example
- If x 3.1415926 then printf("10.3f",x) will
print - 3.142
- But printf("0.3f", x) prints
- 3.142
m3 printing spaces
n10 printing spaces
30Printing Integers and Strings
- Integers and strings can also use field width
- nd, ns where n is again min field width.
- Example printf("-10d10s", 21,"abcde")
- 21 abcde
8 wide
5 wide
10 wide
10 wide
31Legal vs. Correct Program
- The compiler forces you to make a legal program
-- one that obeys the grammar rules. - It is up to you to make sure the program is
correct -- that it solves the given problem.
32Find the 9 Errors
include ltstdiogt define TOPBOTTOM "III"
define MIDDLE "\nI" void main(void)
printf("/n/n/n") printf(TOPBOTTON) printf(MID
DLE) printf(MIDDLE) printf(\n) prinf(TOPBOTTO
M) return 1
33Some other useful functions
- These all require include ltctype.hgt
- isalpha(c) - returns true if c is letter
- isdigit(c) - returns true if c is a digit (0-9)
- isspace(c) - returns true if c is space, tab or
newline. - islower(c) - returns true if c is lower case.
- isupper(c) - returns true if c is upper case.
34Change to Caps
- Change input letters to caps
- Difference between lower case and upper case is
32 for all letters (in English). - 'a' - 'A' has value 32
- 'z' - 'Z' also has value 32
35ToUpper
- int main(void)
- char c1,c2,c3,c4,c5
- c1 getchar() c2 getchar() / get the chars
/ - c3 getchar() c4 getchar() c5
getchar() - / convert to UC and print /
- printf("\n\nccccc",
- toupper(c1), toupper(c2), toupper(c3),
- toupper(c4), toupper(c5))
- / print as characters /
- return 0
36Questions?