Programming with C - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Programming with C

Description:

int integer1, integer2, sum; // declaration. std:: cout 'Enter first integern'; // prompt ... sum = integer1 integer2; // assignment of sum ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 40
Provided by: jun6
Category:

less

Transcript and Presenter's Notes

Title: Programming with C


1
Programming with C
Jun Ni, Ph.D.M.E. Department of Computer
Science The University of Iowa Fall, 2002
2
Introduction
3
Introduction
  • Introduction

4
Introduction
  • 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

5
Introduction
  • 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, )

6
Introduction
  • 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.

7
Introduction
  • 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

8
Introduction
  • 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, ...

9
Introduction
  • 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.

10
Introduction
  • 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
11
Introduction
  • First C program

// 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
12
Introduction
  • result

Welcome to C!
13
Introduction
  • Escape sequence characters

\n \t \r \a \\ \" \v \b \r \? \'
14
Introduction
  • Revised First C program

// 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!
15
Introduction
  • 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
16
Introduction
  • Result

Welcome to C!
17
Introduction
  • 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
18
Introduction
  • 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
19
Introduction
  • Arithmetic operators
  • Examples

- /
f7 p-c bm x/y rs
20
Introduction
  • Rules of arithmetic operator precedence

( ) inside to outside / left to
right - left to right
Example (34) 5 2 /(32-1) 83
21
Introduction
  • 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
22
Introduction
  • Increment/decrement operators --
  • count i
  • j-- --k
  • (pre-increment/decrement)
  • (post-increment/decrement)

23
Introduction
  • Example

// 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
24
Introduction
  • Example

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
25
Introduction
  • 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)

26
Introduction
( 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
27
Introduction
( 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
28
Introduction
! ( relational test )
relational test (false) relational test (true)
true
false
29
Introduction
  • 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
30
int 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
31
if ( 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
32
Result 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
33
Introduction
  • 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
34
Introduction
  • 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

35
Introduction
36
Introduction
  • 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

37
Introduction
  • Object-Oriented Programming (cont.)
  • objects are declared based on class type
  • and program directly makes objects "alive" or in
    action

38
Introduction
39
Introduction
  • 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
Write a Comment
User Comments (0)
About PowerShow.com