Title: Programming with C
1Programming with C
Jun Ni, Ph.D.M.E. Department of Computer
Science The University of Iowa Fall, 2002
2Introduction
3Introduction
4Introduction
- Structural programming (SP) and object-oriented
programming (OOP) language. - Superset of C.
- One of most popular software development
programming. - ANSI/ISO standard.
- Standard library
- Scalable and potable.
- Distributed computing and grid computing
5Introduction
- What you should prepare before learning C?
- Your commitment.
- Basic computer hardware knowledge (I/O devices,
memory, ALU, CPU, secondary storage, - Computer software knowledge (OS, languages,
compiler and interpreter, )
6Introduction
- History
- C was evolved by Denis Ritchie at Bell Lab in
1972 when he developed UNIX. It becomes standard
in 1990. - C, an extension of C was developed by Bjarne
Stoustrup in the early 1980s at Bell Lab. OOP was
provided in, which is a revolution of computer
programming. - C has standard library.
7Introduction
- The main pieces of learning C is
- to learn C language
- to learn how to use standard library (classes and
functions), and special C library . - The final goal is
- to apply these libraries to your own applications
- to develop your own library for your use or
contribute own library to computer world
8Introduction
- What else we should know or learn? JAVA!
- Java is another popular programming language
- Java is very portable
- Incorporates features of C/C and other OOP
- easy to apply to web applications through Java
applets - How about other popular languages?
- Fortran, COBOL, Pascal, ...
9Introduction
- How to run C programs?
- Use any word processing tools to create a C
code in terms of text file, or ASCII format. - C files are ended with .cpp, .cxx, or .C
- Then compile and link C files
- Finally execute the executable file created
- If find bugs or errors, go back to the first step
and correct them and try again.
10Introduction
- How? How? How?
- On UNIX, use UNIX commands
- On PC, use Visual C (see handout)
CC myfirst.cpp a.out
or
CC -o myfirst myfirst.cpp myfirst
11Introduction
// Fig. 1.2 fig1_02.cpp // A first program in
C include ltiostream.hgt int main()
stdcout ltlt "Welcome to C!\n" // indicate
that program ended successfully return 0
comment
Preprocessor directive
function
body
12Introduction
Welcome to C!
13Introduction
- Escape sequence characters
\n \t \r \a \\ \" \v \b \r \? \'
14Introduction
// Fig. 1.4 fig1_4.cpp // Printing a line with
multiple statements include ltiostream.hgt int
main() stdcout ltlt "Welcome " stdcout
ltlt "to C!\n" // indicate that program ended
successfully return 0 Result
Welcome to C!
15Introduction
- More new lines added in the first C program
// Fig. 1.5 fig1_05.cpp // Printing multiple
lines with a single statement include
ltiostream.hgt int main() stdcout ltlt
"Welcome\nto\n\nC!\n" // indicate that program
ended successfully return 0
16Introduction
Welcome to C!
17Introduction
- Adding two integers (input and output)
// Fig. 1.6 fig1_06.cpp // Addition
program include ltiostream.hgt int main()
int integer1, integer2, sum // declaration
std cout ltlt "Enter first integer\n" //
prompt std cin gtgt integer1 // read
an integer std cout ltlt "Enter second
integer\n" // prompt std cin gtgt integer2
// read an integer
18Introduction
- Adding two integers
- Result
sum integer1 integer2 // assignment of
sum std cout ltlt "Sum is " ltlt sum ltlt endl //
print sum // indicate that program ended
successfully return 0
Enter first integer 45 Enter second
integer 72 Sum is 117
19Introduction
- Arithmetic operators
- Examples
- /
f7 p-c bm x/y rs
20Introduction
- Rules of arithmetic operator precedence
( ) inside to outside / left to
right - left to right
Example (34) 5 2 /(32-1) 83
21Introduction
- Assignment operator (right to left)
- example a 32 a a-2
- Additional assignment operators
- - /
a2
aa2
a-2
aa-2
cc2
c2
cc/2
c/2
cc2
c2
22Introduction
- Increment/decrement operators --
- count i
- j-- --k
- (pre-increment/decrement)
- (post-increment/decrement)
23Introduction
// Preincrementing and postincrementing include
ltiostream.hgt int main() int c c 5
std cout ltlt c ltlt endl // print 5
std cout ltlt c ltlt endl // print 5 then
postincrement std cout ltlt c ltlt endl ltlt endl
// print 6
24Introduction
c 5 cout ltlt c ltlt endl // print
5 cout ltlt c ltlt endl // preincrement
then print 6 cout ltlt c ltlt endl //
print 6 return 0 //
successful termination
25Introduction
- Equality operator
- Example a2 j(i-k)
- relational operators gt gt lt lt
- Example agtb hgt12 clta jltk-1
- logical operators !
- Example a!10 alt2 agt10 bgt4 agt3
- ! (agt3)
26Introduction
( relational test 1) ( relational test2)
relational test 1 relational rest 2
(false) (true)
false
relational test 1 (false) relational test 2
(true)
false
false
true
27Introduction
( relational test 1) ( relational rest2)
relational test 1 relational rest 2
(false) (true)
true
relational test 1 (false) relational test 2
(true)
false
true
true
28Introduction
! ( relational test )
relational test (false) relational test (true)
true
false
29Introduction
- Relational operator example
// Fig. 1.14 fig01_14.cpp // Using if
statements, relational // operators, and equality
operators include ltiostreamgt using stdcout
// program uses cout using stdcin // program
uses cin using stdendl // program uses endl
30int main() int num1, num2 cout ltlt
"Enter two integers, and I will tell you\n"
ltlt "the relationships they satisfy " cin
gtgt num1 gtgt num2 // read two integers if (
num1 num2 ) cout ltlt num1 ltlt " is equal
to " ltlt num2 ltlt endl if ( num1 ! num2 )
cout ltlt num1 ltlt " is not equal to " ltlt num2 ltlt
endl
31if ( num1 lt num2 ) cout ltlt num1 ltlt " is
less than " ltlt num2 ltlt endl if ( num1 gt num2
) cout ltlt num1 ltlt " is greater than " ltlt
num2 ltlt endl if ( num1 lt num2 ) cout
ltlt num1 ltlt " is less than or equal to "
ltlt num2 ltlt endl if ( num1 gt num2 ) cout
ltlt num1 ltlt " is greater than or equal to "
ltlt num2 ltlt endl return 0 // indicate
that program ended successfully
32Result Enter two integers, and I will tell you
the relationships they satisfy 3 7 3 is not
equal to 7 3 is less than 7 3 is less than or
equal to 7
33Introduction
- Header files and namespaces
includeltiostream.hgt or includeltiostreamgt using
namespace std using namespace std then the
built-in object should be used like stdcoutltlt"I
am learning C now"gt using namescape
statistics1 your own class
library
34Introduction
- Procedural Programming
- action-oriented
- functions are grouped to form a program
- data are provided, in order to perform a
function, or the data are generated from a
function
35Introduction
36Introduction
- Object-Oriented Programming
- Vision of objects in cataloging or classification
- Object data (attributes) and functions
(behaviors) - independence of each object (hiding)
- interface communication between objects
- data oriented through whole program
- main focus is on creating classes which contain
data member and function member
37Introduction
- Object-Oriented Programming (cont.)
- objects are declared based on class type
- and program directly makes objects "alive" or in
action
38Introduction
39Introduction
- Why we introduce OOP
- classified data close to real world
- more module and more scalable
- easy to implement
- large available class libraries can be used to
reused - You will love OOP for sure