Introduction To Programming in C: Zhiao Shi
Introduction To Programming in C: Zhiao Shi
Lecture 01
Introduction
Zhiao Shi
Course information
2
References
Reference book:
P.J. Deitel and H.M. Deitel, “C How to Program”, Fifth Edition,
Pearson Prentice-Hall, 2007.
Jeri R. Hanly and Elliot B. Koffman, “Problem Solving and Program
Design in C”, Sixth Edition, Addison Wesley, 2009
Brian W. Kernighan and Dennis M. Ritchie, “C Programming
Language”, Second Edition, Prentice Hall, PTR, 1988.
Anany Levitin, “Introduction to The Design and Analysis of
Algorithms”, Pearson Addison Wesley, 2003.
3
Course Outline
Programming Methodologies
Algorithm Design
Control Structures
Pointers
File Processing
Discipline of Programming
Programming Style
4
Outline
5
What is a Computer?
Computer
Device capable of performing computations and making logical decisions
(hardware)
Computers process data under the control of sets of instructions called
6
Computer Organization
7
Hardware
8
Evolution of Operating Systems
Batch processing
Do only one job or task at a time
Multiprogramming
Resource managment and sharing for multiple programs
Quasi-simultaneous program execution
Single user
Multiuser/Timesharing Systems
Management of multiple simultaneous users
interconnected via terminals
Fair resource management: CPU scheduling
9
Operating Systems
10
Personal Computing, Distributed Computing, and Client/Server Computing
Personal computers
Economical enough for individual
Distributed computing
Computing distributed over networks
Client/server computing
Sharing of information across computer networks between file
servers and clients
11
Personal Computing, Distributed Computing, and Client/Server Computing
12
Outline
13
Machine Languages, Assembly Languages, and High-level Languages
14
Outline
15
History of C
C
Evolved by Dennis Ritchie from two previous programming
languages, BCPL and B
Used to develop UNIX
Used to write modern operating systems
Hardware independent (portable)
Standardization
Many slight variations of C existed, and were incompatible
Committee formed to create a "unambiguous, machine-
independent" definition
Standard created in 1989, updated in 1999
16
C Standard Library
17
Outline
Basic computer concepts
Different types of programming languages
C programming language
History
C Standard Library
Object-oriented Programming
Typical C Program Development Environment
Introduction to C programming language
18
C++
C++
Superset of C developed by Bjarne Stroustrup at Bell Labs
"Spruces up" C, and provides object-oriented capabilities
Object-oriented design very powerful
» 10 to 100 fold increase in productivity
Learning C++
Because C++ includes C, some feel it is best to master C, then learn
C++
19
Java
Java is used to
Create Web pages with dynamic and interactive content
Develop large-scale enterprise applications
Enhance the functionality of Web servers
Provide applications for consumer devices (such as cell phones,
pagers and personal digital assistants)
20
Outline
Basic computer concepts
Different types of programming languages
C programming language
History
C Standard Library
Object-oriented Programming
Typical C Program Development Environment
Introduction to C programming language
21
A Typical C Program Development Environment
Phases of C Programs:
Edit (C program file names should
end with the .c extension)
Preprocess
Compile
Link
Load
Execute
22
Outline
Basic computer concepts
Different types of programming languages
C programming language
History
C Standard Library
Object-oriented Programming
Typical C Program Development Environment
Introduction to C programming language
23
Outline
Print a line
Escape sequence
Variables and Data types
Memory concepts
Arithmetic operators
If-then statements
Review
24
A Simple C Program: Printing a Line of Text
Example 01:
Comments
Text surrounded by /* and */ is ignored by computer
Used to describe program
#include <stdio.h>
A directive to the C preprocessor
Tells computer to load contents of a certain file
<stdio.h> allows standard input/output operations
25
A Simple C Program: Printing a Line of Text
int main()
C programs contain one or more functions, exactly one of which
must be main
Parenthesis used to indicate a function
int means that main "returns" an integer value
Braces ({ and }) indicate a block
» The bodies of all functions must be contained in braces
printf( "Welcome to C!\n" );
Instructs computer to perform an action
» Specifically, prints the string of characters within quotes (" ")
Entire line called a statement
» All statements must end with a semicolon (;)
Escape character (\)
» Indicates that printf should do something out of the ordinary
» \n is the newline character
26
A Simple C Program: Printing a Line of Text
return 0;
A way to exit a function
return 0, in this case, means that the program terminated normally
Right brace }
Indicates end of main has been reached
Linker
When a function is called, linker locates it in the library
Inserts it into object program
If function name is misspelled, the linker will produce an error
because it will not be able to find function in the library
27
Outline
Print a line
Escape sequence
Variables and Data types
Memory concepts
Arithmetic operators
If-then statements
Review
28
Some common escape sequences
29
The printf function can print Welcome to C! several different
ways
Example 02:
fig02_03.c
30
The printf function can print Welcome to C!
several different ways
Example 03:
31
Objectives
Print a line
Escape sequence
Variables and Data types
Memory concepts
Arithmetic operators
If-then statements
Review
32
Example 04:
Definitions of variables
As before
Comments, #include <stdio.h> and main
int integer1, integer2, sum;
Definition of variables
» Variables: locations in memory where a value can be stored
int means the variables can hold integers (-1, 3, 0, 47)
Variable names (identifiers)
» integer1, integer2, sum
» Identifiers: consist of letters, digits (cannot begin with a digit) and
underscores( _ )
Case sensitive
Definitions appear before executable statements
» If an executable statement references and undeclared variable it will
produce a syntax (compiler) error
34
Another Simple C Program: Adding Two Integers
35
Outline
Print a line
Escape sequence
Variables and Data types
Memory concepts
Arithmetic operators
If-then statements
Review
36
Memory Concepts
Variables
Variable names correspond to locations in the computer's memory
Every variable has a name, a type, a size and a value
Whenever a new value is placed into a variable (through scanf, for
example), it replaces (and destroys) the previous value
Reading variables from memory does not change them
37
Memory Concepts
38
Outline
Print a line
Escape sequence
Variables and Data types
Memory concepts
Arithmetic operators
If-then statements
Review
39
Another Simple C Program: Adding Two Integers
= (assignment operator)
Assigns a value to a variable
Is a binary operator (has two operands)
» sum = variable1 + variable2;
» sum gets variable1 + variable2;
Variable receiving value on left
printf( "Sum is %d\n", sum );
Similar to scanf
» %d means decimal integer will be printed
» sum specifies what integer will be printed
Calculations can be performed inside printf statements
» printf( "Sum is %d\n", integer1 + integer2 );
40
Arithmetic
Arithmetic calculations
Use * for multiplication and / for division
Integer division truncates remainder
» 7/5 evaluates to 1
Modulus operator(%) returns the remainder
» 7 % 5 evaluates to 2
Operator precedence
Some arithmetic operators act before others (i.e., multiplication
before addition)
» Use parenthesis when needed
Example: Find the average of three variables a, b and c
» Do not use: a + b + c / 3
» Use: (a + b + c ) / 3
41
Arithmetic
Arithmetic operators:
42
Example: operator precedence
43
Outline
Print a line
Escape sequence
Variables and Data types
Memory concepts
Arithmetic operators
If-then statements
Review
44
Decision Making: Equality and Relational Operators
Executable statements
Perform actions (calculations, input/output of data)
Perform decisions
» May want to print "pass" or "fail" given the value of a test grade
if control statement
If a condition is true, then the body of the if statement executed
» 0 is false, non-zero is true
Control always resumes after the if structure
45
Decision Making: Equality and Relational Operators
46
An Example to Use if statements, relational operators, and
equality operators
Example 05:
47
Checks if num1 is greater than num2
fig02_13.c (2
of 3 )
48
fig02_13.c (3
of 3 )
49
Precedence and associativity of the operators discussed so far
50
Outline
Print a line
Escape sequence
Variables and Data types
Memory concepts
Arithmetic operators
If-then statements
Review
51
Review
53
Review
54