|
1
|
|
|
2
|
- C Language characters and tokens
- reserved words
- identifiers
- constants
- string constants
- punctuators
- operators
|
|
3
|
- Structure of a C program
- program comments
- preprocessor directives
- data types and declarations
- named constants
- statements
- functions
|
|
4
|
- How to prepare C source code
- Interactive input/output
- Simple debugging
|
|
5
|
- 92 legal characters
- 0-9
- a-z
- A-Z
- space
- all other punctuation except ` $ @
|
|
6
|
- Combine characters to make tokens
- Basic units of the language
- Six kinds
- keywords
- identifiers
- constants
- string literals
- punctuation
- operators
|
|
7
|
- while
- firstName
- 123.56
- “hello, World!”
- ‘\n’
- (
|
|
8
|
- CASE SENSITIVE!!!
- While, while, and WHILE are not the same
- Only while is correct
- Appendix A in the book has all the reserved words.
|
|
9
|
- Programmer defined words that refer to variables, functions, etc.
- These are case sensitive
- Use identifiers that help the readability of a program.
|
|
10
|
- A-Z, a-z, 0-9, _
- First character must be a letter or _
- Any length (must be less than 32…)
- No blanks
- No reserved words
- Case sensitive
|
|
11
|
- car_color
- person17
- 1dozen
- WHILE
- Number of dogs
- name,last
|
|
12
|
- Fixed values that occur in the program
- const double PI =3D 3.1415
- Four types
- integer
- floating point
- character
- enumeration
|
|
13
|
- Positive or negative whole numbers (no fractions)
- const int NUM_RECORDS =3D 15
- const int DAYS_IN_WEEK =3D 7
- const int END_FLAG =3D -1
- const int HANDICAP =3D -15
|
|
14
|
- Normal or scientific notation
- const double PI =3D 3.1415
- const double PI =3D 0.31415e+1
- const ANUM =3D 6.0217733e+23
- const PLANCK =3D 6.6260755e-34
- f,F,l,L (optional)
|
|
15
|
- A character enclosed in ’ (single quotes)
- ‘n’, ‘x’
- printf(“%c”,’a’);
- Escape sequences are ways to use “unprintable” charaters=
:
- ‘\n’ -- newline
- ‘\t’ -- tab
- ‘\’’ -- single quote
|
|
16
|
- Kinda like character constants
- A sequence of characters surrounded by ”.
- “Hi, my name is bob.”
- printf(“%s”,”What is your name?”);
- A special string literal is the format string for printf.
|
|
17
|
- [], {}, (), , ; : … * #
- We’ll discuss each as they come up.
|
|
18
|
- Tokens that result in some sort of operation occurring.
- An operator acts on an operand:
- operand1 + operand2 (plus is the operator)
- binary operator takes two operands
- unary operator takes one operand
- ternary operator takes three operands
|
|
19
|
- Program comments
- Preprocessor directives
- Type declarations
- Named constants
- Statements
- Function declarations (prototypes)
- Function definitions
- Function calls
|
|
20
|
- The comments describe the purpose of the program, function, or varia=
ble.
- Many uses:
- explain an algorithm
- describe how a variable will be used
- explain expected input
- /* */ or //
|
|
21
|
- Before a C program is compiled the preprocessor runs
- The most common: #include
- Also, #define, #ifdef, …
- Do not end with ;
|
|
22
|
- A header file (*.h) is included in a program with #include.
- Two kinds:
- standard header files <>
- programmer defined header files “”
- Contain function declarations, constants, sometimes variables
|
|
23
|
- Program to ask for a name.
- Constant in one file.
- Change the language.
|
|
24
|
- A statement that associates a data type with a variable.
- In C all variables must be declared with a type.
|
|
25
|
- Internal storage
- char -- 1 byte
- long double -- 8 bytes
- What operations
- How the operations work (/)
|
|
26
|
- A set of values and operations on those values.
- Two kinds:
- built-in
- programmer defined
|
|
27
|
- Int, char, double, float, void
- short, long, signed, unsigned
|
|
28
|
- Int
- a whole number, positive or negative
- char
- double
- a real number (with decimal point)
|
|
29
|
- C variables start with an undefined value
- Compile-time intialization:
- Run-time initialization:
|
|
30
|
- char variables can only hold a single character
- how can we hold a series of characters, like a name?
- using a “string” variable (a string of characters togeth=
er)
|
|
31
|
- However, C doesn’t have a string type.
- Strings are represented as an array of characters.
- We’ll cover arrays in chapter 11 and string processing in chap=
ter
13.
- For now, though, we’ll learn enough basic stuff to use strings=
a
bit.
|
|
32
|
- Declaring a string variable:
- Compile time initialization:
- char firstName [21] =3D “mike”;
- Run time initialization:
- firstName =3D “mike”;&=
nbsp;
NOT LEGAL
|
|
33
|
- To print a string:
- printf(“Hi, my name is %s”, firstName);
- To read in a string:
- scanf(“%s”,firstName);
- no & character
- no spaces allowed in input
|
|
34
|
- A constant value that’s given a name
- const double PI =3D 3.1415
- You can’t reassign PI with a different value later.
- Why use constants?
- Style: use ALL_CAPS
|
|
35
|
- A statement is a specification of an action to be taken by the compu=
ter
as the program executes.
- Statements end in a ;
|
|
36
|
- Expression statements
- Selection statements
- Repetition statements
- Jump statements
- Labeled statements
- Compound statements
|
|
37
|
- A compound statement is a list of statements in { }.
|
|
38
|
- The concept of a function is one of the most important concepts in t=
he C
language.
- So far you’ve seen:
- This is the definition of the function “main”
- Every program must have a “main”.
|
|
39
|
- Every function (except main) must have
- function declaration
- function definition
- function calls
|
|
40
|
- This includes the type, name, arg list
- int bob(…)
- we’ll worry about the arg list later (chap 6)
|
|
41
|
- This includes a copy of the function header (declaration) plus the
statements in the function:
- int bob (…) {
- x =3D x+1;
- }
|
|
42
|
- Some place else in the program that calls the function:
- int main(void) {
- int y=3D5;
- bob(y);
- }
|
|
43
|
- An expression is a combination of operands and operators
- TaxRate * GrossPay
- multiplication expression
- Assignment is an expression
- x =3D y
- assignment operator =3D
|
|
44
|
- printf(“Enter your age: “);
- This is a function call to the printf function.
- Function calls are expressions
- printf(format, values)
- printf(“You are %d years old”,age);
- printf(“You are %d feet and %d inches tall\n”, feet,
inches);
|
|
45
|
|
|
46
|
- Using a variable name returns the contents of that address.
- Using & gets the address itself.
- Needed for scanf
|
|
47
|
- This is used for reading in values
- This also has a format string.
- It also has a list of variables to “fill in”.
- You can get more than one thing:
- scanf(“%d%d”,&feet,&inches)
|
|
48
|
- Everything in the input list must be an address.
- It skips whitespace
- The input must be terminated with an ENTER.
|
|
49
|
|
|
50
|
- Notice that there’s no &.
- Also notice that there’s no []
- That’s because “name” without the []’s is
already an address.
- Also, since scanf ignores whitespace you can’t read in “=
john
smith”.
|
|
51
|
- Use gets() to read in a string with spaces:
|
|
52
|
- Use whitespace (blank lines) to separate program sections (declarati=
ons
and statements).
- Use lots of clear and helpful comments
- Put each statement or declaration on a separate line
- Avoid running statements over more than one line.
|
|
53
|
- Using consistent indentation
|
|
54
|
- Use {} in a consistent manner
- Use whitespace in statements:
- Add a comment to the end of each function
|
|
55
|
- Now let’s put this all together and write a program.
- We want a program that will read in a person’s height in feet =
and
inches and convert that to mm.
|
|
56
|
- Prompt the user for height in feet and inches
- Read in those values
- Convert those values to mm
- Print the mm value
- Give an error if feet or inches < 0
- Give an error if total height <=3D 0
- Give an error if total height > 8 feet
|
|
57
|
- To convert from feet/inches to mm:
|