Lecture03 OverviewOfC
Lecture03 OverviewOfC
Objectives
‣ to become familiar with the general form of a C program and the basic
elements in a program
‣ to learn how to read data values into a program and display results
‣ to understand how to write format strings for data entry and display
2
RECALL
Software Development Method
1. Problem Definition
2. Problem Analysis
3
APPLYING THE SOFTWARE DEVELOPMENT METHOD
Case Study
Problem
4
APPLYING THE SOFTWARE DEVELOPMENT METHOD
Case Study
Analysis
‣ Convert from kilometers to miles? or miles to kilometers?
5
APPLYING THE SOFTWARE DEVELOPMENT METHOD
Case Study
Analysis
‣ Convert from Miles to Kilometers
‣ Data of requirements
6
APPLYING THE SOFTWARE DEVELOPMENT METHOD
Case Study
Algorithm
1. Get the distance in miles
2. Convert the distance to kilometers
3. Display the distance in kilometers
7
APPLYING THE SOFTWARE DEVELOPMENT METHOD
Case Study
8
Coding and
Debugging
9
C
‣ high-level programming language developed in
1972 by Dennis Ritchie at AT&T Bell Laboratories
10
11
C LANGUAGE ELEMENTS
Preprocessor Directives
Preprocessor
Preprocessor Directive
Library
‣ has a standard header file whose name ends with the symbols .h
12
C LANGUAGE ELEMENTS
Preprocessor Directives
#include Directive
Example: #include<stdio.h>
#include<math.h>
13
C LANGUAGE ELEMENTS
Preprocessor Directives
#define Directive
‣ C program statements cannot change the value associated with the NAME
constant macro
‣ name that is replaced by a particular constant value before the program is sent to
the compiler
14
C LANGUAGE ELEMENTS
Function main
‣ every C program has a main function
Function Body
int main(void){
function body
}
15
C LANGUAGE ELEMENTS
Reserved Words
‣ a word that has special meaning in C and cannot be
used for other purposes
16
C LANGUAGE ELEMENTS
Identifiers
Standard Identifiers
Example: printf
scanf
17
C LANGUAGE ELEMENTS
Identifiers
User-Defined Identifiers
Example: KMS_PER_MILE
miles
kms
18
C LANGUAGE ELEMENTS
Identifiers
Syntax Rules
19
C LANGUAGE ELEMENTS
Identifiers
Good programming practice when choosing identifier names,
20
C LANGUAGE ELEMENTS
Identifiers
Which of the following are valid identifiers?
1Letter
MAX_ENTRIES
TWO*FOUR
_variable2
joe’s
double
xYZ123
part#2
XyZ123
this_is_a_long_one
21
C LANGUAGE ELEMENTS
Identifiers
Which of the following are valid identifiers?
1Letter invalid, cannot begin with a digit
MAX_ENTRIES valid
TWO*FOUR invalid, char * is not allowed
_variable2 valid, not recommended
joe’s invalid, char ‘ is not allowed
double invalid, reserved word
xYZ123 valid
part#2 invalid, char # is not allowed
XyZ123 valid
this_is_a_long_one valid
22
C LANGUAGE ELEMENTS
Identifiers
Which of the following are valid identifiers?
4score
color12
CONST_TIME
helloworld
two!s
for
static
@twitter
Gender
_x2y1z0
23
C LANGUAGE ELEMENTS
Identifiers
Which of the following are valid identifiers?
4score invalid, cannot begin with a digit
color12 valid
CONST_TIME valid
helloworld valid
two!s invalid, char ! is not allowed
for invalid, reserved word
static invalid, reserved word
@twitter invalid, char @ is not allowed
Gender valid
24
Variable Declarations and Data Types
Variable
Variable Declarations
25
Variable Declarations and Data Types
Syntax:
<data_type> <variable_list>;
Examples:
char middle_initial;
char answer;
int age;
26
Variable Declarations and Data Types
Data Type
e.g int
char
double
27
Variable Declarations and Data Types
‣ ANSI C specifies that that the range must include atleast the
values -32767 through 32767
28
Variable Declarations and Data Types
‣ real numbers has an integral part and a fractional part that are
separated by a decimal point
e.g 123000.0
1.23 x 105 Normal Scientific Notation
*1.23e5 or *1.23E5 C Scientific Notation
29
Variable Declarations and Data Types
30
Variable Declarations and Data Types
31
Variable Declarations and Data Types
Indicate which of the following are valid type int, double, or char constants
in C and which are not. Identify the data type of each valid constant.
‘PQR'
15E-2
35
‘h'
-37.491
.912
4,719
‘true’
‘$’
&
4.5e3
32
Variable Declarations and Data Types
Indicate which of the following are valid type int, double, or char constants
in C and which are not. Identify the data type of each valid constant.
‘PQR' invalid, single letter
15E-2 valid, double
35 valid, int
‘h' valid, char
-37.491 valid, double
.912 valid, double
4,719 invalid, comma not allowed
‘true’ invalid, single letter
‘$’ valid, char
& invalid, should be enclosed with single quotes
4.5e3 valid, double
33
Variable Declarations and Data Types
What data types would you use to represent the following items:
34
Variable Declarations and Data Types
What data types would you use to represent the following items:
35
Executable Statements
‣ follow after the declarations in a function
36
Executable Statements
Assignment Statement
37
Executable Statements
Assignment Statement
variable = expression;
38
Executable Statements
Assignment Statement
39
Executable Statements
Assignment Statement
40
Executable Statements
Assignment Statement
next_letter = ‘A’;
41
Executable Statements
I/O Operations and Functions
Input Operation
Output Operation
42
Executable Statements
I/O Functions
‣ the most common functions are supplied as part of the C standard input/
output library to which we gain access through the preprocessor directive
Function Call
‣ activating a function
‣ analogous to asking a friend to perform an urgent task. You tell them what
to do (not how to do it) and wait for your friend to report back that the task
is finished. After hearing from your friend, you can go and do something
else.
43
Executable Statements
Calling the printf Function
function arguments
print list
‣ if the print list has several variables, the format string should contain the same
number of placeholders
44
Executable Statements
Calling the printf Function
Example:
printf(“I am %d years old, and my gpa is %f\n", age, gpa);
printf(“Enter the object mass in grams>” );
format string
placeholder
‣ copies into memory data typed at the keyboard by the program user
during program execution
‣ Each int, double or char variable in the input list should be preceded by
the address-of operator - &.
46
Executable Statements
Multiple Placeholders
47
Executable Statements
Formatting Numbers in Program Output
48
Executable Statements
Formatting Values of Type double
‣ must indicate both the total field width needed and the
number of decimal places desired
49
Executable Statements
return Statement
Example: return 0;
50
Executable Statements
1. Show the output displayed by the following program lines when the data
entered are 5 and 7:
2. Write a statement that asks the user to type three integer and another
statement that stores the user responses into first, second, third
3. Write a program that asks the user to enter the radius of a circle and then
computes and displays the circle’s area. Use the formula
51
Executable Statements
52
General Form of a C Program
preprocessor directives
declarations
executable statements
53
Arithmetic Expressions
Arithmetic Operators
54
Arithmetic Expressions
Division Operator ( / )
‣ will compute the integral part of the result when applied to two positive integer
operands
‣ when used with a positive and negative operands, the result may vary from one
C implementation to another. Thus, avoid using division with negative integers
55
Arithmetic Expressions
Remainder Operator ( % )
‣ returns the integer remainder of the result of dividing its first operand by its second
operand
56
Arithmetic Expressions
Unary Operator
e.g. negation ( - )
plus ( + )
Binary Operator
57
Arithmetic Expressions
Data Type of an Expression
Mixed-Type Expression
Mixed-Type Assignment
‣ the expression being evaluated and the variable to which it is assigned have
different data types
m = 3;
n = 2;
p = 2.0;
x = m / p;
y = m / n;
58
Arithmetic Expressions
Rules for Evaluating Expressions
1. Parentheses rule
‣ nested parenthesized expressions must be evaluated from inside out, with the innermost
expression evaluated first
3. Associativity rule
‣ Unary operators in the same subexpression and at the same precedence level are
evaluated right to left (right associativity)
‣ Binary operators in the same subexpression and at the same precedence level are
evaluated left to right (left associativity)
59
Arithmetic Expressions
z - (a + b / 2) + w * -y
60
Arithmetic Expressions
61
Arithmetic Expressions
62
Arithmetic Expressions
63
Arithmetic Expressions
Given the following declarations,
#define PI 3.14159
#define MAX_I 1000
double x, y;
int a, b, i;
64
Arithmetic Expressions
1. i=a%b; 10. i=(MAX_I−990)/a;
4. x=PI*y; 13. x = PI / y;
65
Programming Exercises
1. Write a program to compute the average of three given
numbers
66
Write pseudocode versions of the following:
1. An algorithm that gets three data values x, y, and z as
input and outputs the average of those three values
68