introduction

advertisement
CIS 1057 Computer
Programming in C
Dr. Anwar Mamat
Fall 2013
Acknowledgement: Many slides based on/borrowed from Professor Hugh C. Lauer
Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and
from C: How to Program, 5th and 6th editions, by Deitel and Deitel)
CIS 1057 Fall 2013
Introduction
1
Course Objectives
• C language programming
• Designing, implementing, debugging, etc.
• How the hardware executes C programs
• … and other kinds of programs
• Preparation for computational challenges of
engineering, scientific, and other professions
• Preparation for upper-level courses in computing
• “Thinking computationally”
CIS 1057 Fall 2013
Introduction
2
Why?
• Computing and Programming: a life skill
for all technical professionals
– http://www.youtube.com/watch?v=dmM_xDzy
2nU
• Thinking Computationally: organizing your
engineering/scientific/technical thoughts to
be amenable to computational solutions
CIS 1057 Fall 2013
Introduction
3
Why C?
• Because we have to!
• Many situations outside of CS where it is
only language or system available
• Small, embedded systems, instrumentation, etc.
• Many “low-level” situations that don’t
have support for “high-level” languages
• Operating systems, real-time systems, drivers
CIS 1057 Fall 2013
Introduction
4
Why not C?
• C is very low-level
•
•
•
•
Data structures must be programmed “by hand”
Operations must be done out in “long hand”
No support for “object oriented” design
Marginal support for higher-level thought processes
• Better alternatives available for technical
applications
•
•
•
•
•
•
Verilog, VHDL, System C – semiconductor design
Matlab, SimuLink – physical modeling
LabView – instrumentation and control
Excel – accounting and statistics
SQL – billing and transactions
…
CIS 1057 Fall 2013
Introduction
5
This Course
• Lectures
• C programming language, syntax, semantics, etc.
• Common data structures that technical professionals
are likely to need or encounter in C
• Programming Assignments
• Write programs to exercise various language
constructs and data structures
• Mandatory for passing this course
• Lab Sessions
• How to use the system, tools, debuggers, etc.
• Practical help from TAs, etc.
CIS 1057 Fall 2013
Introduction
6
This Course
(continued)
• Wednesdays and Fridays, 9:30–10:50 AM
• Tuttleman 401B
• Weekly Lab Sessions
• Mondays: 8:00 AM - 9:50 AM WCHMAN 104
• Midterm exam
• Oct. 18
• Review session prior to exam on Oct. 16
CIS 1057 Fall 2013
Closed book, one 8½-by-11
sheet of prepared notes, no
Introduction
7
calculators or electronics
Prerequisites
• First-level gen-ed math course
CIS 1057 Fall 2013
Introduction
8
Logistics
• Course web site
– http://cis.temple.edu/~anwar/CIS1057Fall2013.html
– Blackboard
• Professor’s office Hours
– Mondays, 10:00-12:00 AM, or by appointment
(additional hours TBD)
– Wachman 414 (215) 204-4207
• Contacts
– anwar@ temple.edu
• Teaching assistant
– Li, Dawei (TA)
CIS 1057 Fall 2013
Introduction
9
Required Textbook
• Problem Solving and Program Design in
C, 7th Edition, Hanly and Koffman
• Reference book:
– The C Programming Language, 2nd edition, by
Brian Kernighan and Dennis Ritchie, Prentice
Hall, 1988
Bring to all classes and all lab sessions
CIS 1057 Fall 2013
Introduction
10
Textbook Outline
•
•
•
•
•
•
•
•
•
•
•
•
•
•
1. Overview of Computers and Programming
2. Overview of C
3. Top-Down Design with Functions
4. Selection Structures: if and switch Statements
5. Repetition and Loop Statements
6. Pointers and Modular Programming
7. Arrays
8. Strings
9. Recursion
10. Structure and Union Types
11. Text and Binary File Processing
12. Programming in the Large
13. Dynamic Data Structures
Appendix A,B,C,D,E: the C language reference, the standard libraries
• You will use these a lot!
CIS 1057 Fall 2013
Introduction
11
Ground Rule #1
• There are no “stupid” questions.
• It is a waste of your time and the class’s
time to proceed when you don’t understand
the basic terms.
• If you don’t understand it, someone else
probably doesn’t it, either.
CIS 1057 Fall 2013
Introduction
12
Ground Rule #2
• Help each other!
• Even when a project or assignment is specified as
individual, ask your friends or classmates about
stuff you don’t understand.
• It is a waste of your time try to figure out some
obscure detail on your own when there are lots of
resources around.
• When you have the answer, write it in your own
words (or own coding style).
CIS 1057 Fall 2013
Introduction
13
Names and Faces
• It is in your own interest that I know who
you are.
• Students who speak up in class usually get
more favorable grades than those who don’t
• When speaking in class, please identify
yourselves
CIS 1057 Fall 2013
Introduction
14
Temple Academic Honesty Policy
• It is a violation of the Temple Academic
Honesty Policy to submit someone else’s
work as your own.
• It is not a violation of Temple’s Academic
Honesty Policy to ask for help!
• Classmates, TAs, friends, mentors, …
• Explanations of things you don’t understand
CIS 1057 Fall 2013
Introduction
15
Additional Help
• Academic Resource Center has Tutors
available to assist in Wachman 200.
CIS 1057 Fall 2013
Introduction
16
Questions?
CIS 1057 Fall 2013
Introduction
17
The C Language
• First created to develop Unix – late 1960s
• Kernighan & Ritchie, 1st edition – 1978
• ANSI C – 1988
• Kernighan & Ritchie, 2nd edition, 1988
• Implemented by nearly all C compilers
• C95, C99
• Minor additions (to be noted as we get to them)
• Most major C compilers
CIS 1057 Fall 2013
Introduction
18
Successors to C
• C++
• Developed by Bjarne Stroustrup at Bell Labs
• Major extension of C to support object-oriented
programming
• Attempted to preserve syntax and structure of C
• Java
•
•
•
•
Rewrite of C++ at Sun Microsystems
Machine independence, portability
Ability to embed in web pages
Huge libraries of packages for all kinds of stuff
CIS 1057 Fall 2013
Introduction
19
Your First C Program
#include <stdio.h>
int main () {
printf(″Hello, World!\n″);
return 0;
}
CIS 1057 Fall 2013
Introduction
20
Fundamental Rule in C
• Every identifier must be declared before it can
be used in a program
• Definition:– “identifier”
• A sequence of letters, digits, and ‘_’
• Must begin with a letter or ‘_’
• Case is significant
– Upper and lower case letters are different
• Must not be a “reserved word” — see appendix
• Definition:– “declare”
• Introduce an identifier and the kind of entity it refers to
• Optionally, define associated memory or program
CIS 1057 Fall 2013
Introduction
21
So where is printf declared?
#include <stdio.h>
int main () {
printf(″Hello, World!\n″);
return 0;
}
CIS 1057 Fall 2013
Introduction
22
So where is printf declared?
#include <stdio.h>
Answer: in this file!
int main () {
printf(″Hello, World!\n″);
return 0;
}
CIS 1057 Fall 2013
Introduction
23
Your First C Program
#include <stdio.h>
• A header file
• Contains declarations
of names, functions,
int main () {
data, of things defined
printf(″Hello, World!\n″);
elsewhere
return 0;
• E.g., by the system
}
• Text of the header file
is inserted by compiler
into your program
• As if you wrote it
yourself!
CIS 1057 Fall 2013
Introduction
24
Your First C Program
#include <stdio.h>
• A function declaration
• Declares the name and
defines the body of
int main () {
your function
printf(″Hello, World!\n″);
• May take arguments,
return 0;
returns an integer
}
• main is a special
name to the system
• The place where a
program “starts”
CIS 1057 Fall 2013
Introduction
25
Your First C Program
#include <stdio.h>
• Body of the function
• Defines what the
function “does”
int main () {
• Sequence of
printf(″Hello, World!\n″);
statements
return 0;
• Each does a step of the
}
function
• Enclosed in curly
brackets
•{ }
CIS 1057 Fall 2013
Introduction
26
Your First C Program
#include <stdio.h>
int main () {
printf(″Hello, World!\n″);
return 0;
}
• Call to another function
• In this case, a function defined by the system
• Prints some data on standard output
CIS 1057 Fall 2013
Introduction
27
Your First C Program
#include <stdio.h>
int main () {
printf(″Hello, World!\n″);
return 0;
}
• Argument to printf – a constant string
• Enclosed in straight double quotes
• Note the new-line character ′\n′ at the end
CIS 1057 Fall 2013
Introduction
28
Your First C Program
#include <stdio.h>
int main () {
printf(″Hello, World!\n″);
return 0;
}
• A return statement
• return is a reserved word in C
• main should return zero if no error; non-zero if error
CIS 1057 Fall 2013
Introduction
29
Your First C Program
#include <stdio.h>
int main () {
printf(″Hello, World!\n″);
return 0;
}
• Note that statements typically end with semicolons
• So compiler can tell where end of statement is
CIS 1057 Fall 2013
Introduction
30
Questions?
Write, compile, and execute this
program in Lab session
CIS 1057 Fall 2013
Introduction
31
What happens to your program …
…after it is compiled, but before it
can be run?
CIS 1057 Fall 2013
Introduction
32
Example
#include <stdio.h>
• Symbol defined in
your program and
used elsewhere
int main () {
printf (″Hello,
world\n″);
• main
• Symbol defined
elsewhere and used by
your program
• printf
}
CIS 1057 Fall 2013
Introduction
33
Static Linking and Loading
Printf.c
gcc
HelloWorld.c
gcc
Library
Printf.o ar
HelloWorld.o
Linker
a.out
(or name of
your command)
Loader
Memory
CIS 1057 Fall 2013
Introduction
34
Compiling Your Program
• gcc HelloWorld.c
• Compiles the program in HelloWorld.c, links with
any standard libraries, puts executable in a.out
• You should find HelloWorld.o in your directory
• gcc –o hello_world HelloWorld.c
• Same as above, but names the executable file
hello_world instead of a.out
• gcc –lrt HelloWorld.c
• Searches library named rt.a for functions to link
(in addition to standard libraries)
CIS 1057 Fall 2013
Introduction
35
Compiling Your Program
(continued)
• gcc foo.c bar.c help.c
• Compiles the programs foo.c, bar.c, and help.c,
links with standard libraries, executable in a.out
• You should find foo.o, bar.o, and help.o in your
directory
• gcc –o Lab2 foo.c bar.c help.c
• Same as above, but names the executable file Lab2
• gcc –c foo.c bar.c help.c
• Compiles foo.c, bar.c, and help.c to foo.o,
bar.o, and help.o but does not link together
CIS 1057 Fall 2013
Introduction
36
Questions?
CIS 1057 Fall 2013
Introduction
37
Download