Professional Documents
Culture Documents
H&K Chapter 2
Instructor - Andrew S. OFallon
CptS 121 (January 17, 2014)
Washington State University
Introducing C (1)
C. Hundhausen, A. OFallon
Introducing C (2)
Originally
C. Hundhausen, A. OFallon
Introducing C (3)
Most
C. Hundhausen, A. OFallon
Implementation (in C)
Comment
Reserved
Word
Preprocessor Directive
Punctuation
C. Hundhausen, A. OFallon
Variable Declaration
Standard Identifier /
Library Function
Special Symbol /
Operator
Preprocessor Directives
To use a library, you must include its header file in your code using
#include
A library is a collection of functions and symbols needed to perform
specific tasks or operations
C. Hundhausen, A. OFallon
Function main
Reserved words
C. Hundhausen, A. OFallon
Identifiers
C. Hundhausen, A. OFallon
User-Defined Identifiers
C. Hundhausen, A. OFallon
Notes
Make
10
C. Hundhausen, A. OFallon
Variable Declarations
11
C. Hundhausen, A. OFallon
int
Models
12
C. Hundhausen, A. OFallon
double
models real numbers (must include a decimal point)
not all real numbers can be modeled because of space limitations (64 bits)
Several operations are defined, including +, -, *, and /, and comparisons (>,
<, <=, >=, ==, !=)
Scientific notation can be used to write double values (e.g., 15.0e-3, 314e01)
char
Models individual ASCII characters (8 bits)
Can compare characters (>, <, <=, >=, ==, !=)
When defining char variables in a program, use single quotes: 'c','
','"', etc.
13
C. Hundhausen, A. OFallon
14
score1
60
Machine
language
encoding
of
compute
average
program
C. Hundhausen, A. OFallon
score2
70
score3
80
average
70.0
Machine
language
encoding
of
compute
average
program
Assignment statements
15
C. Hundhausen, A. OFallon
Input/Output Statements
16
C. Hundhausen, A. OFallon
17
C. Hundhausen, A. OFallon
The printf function can be used to output results to the user's display
Example
printf("The student's average is %f.",average);
Notes
Placeholder
Function
Function
%f is a placeholder for double (%lf in scanf); %d is a placeholder for int;
arguments
name
Print list
18
C. Hundhausen, A. OFallon
Another example:
19
C. Hundhausen, A. OFallon
20
C. Hundhausen, A. OFallon
21
Notes
User may separate values with either spaces or returns
If more values are entered than are specified in a scanf,
they are saved ("buffered") for the next call to scanf
C. Hundhausen, A. OFallon
return statement
int
22
C. Hundhausen, A. OFallon
23
C. Hundhausen, A. OFallon
24
C. Hundhausen, A. OFallon
25
C. Hundhausen, A. OFallon
declared:
Begin programs with a header section that indicates
26
Programmer's name
Date of current version
Brief description of what program does
The course ("CptS 121")
Assignment number (e.g., "Programming Assignment #1")
C. Hundhausen, A. OFallon
27
C. Hundhausen, A. OFallon
Next Lecture
28
C. Hundhausen, A. OFallon
References
J.R.
29
C. Hundhausen, A. OFallon
Collaborators
Chris
30
Hundhausen
C. Hundhausen, A. OFallon