cis 1057 computer programming in c

37
Introduction CIS 1057 Fall 2013 1 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, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Upload: iokina

Post on 14-Jan-2016

21 views

Category:

Documents


0 download

DESCRIPTION

CIS 1057 Computer Programming in C. Dr. Anwar Mamat Fall 2013 Acknowledgement: Many slides based on/ borrowed from Professor Hugh C. Lauer - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 1

CIS 1057 Computer Programming in C

Dr. Anwar MamatFall 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)

Page 2: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 2

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”

Page 3: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 3

Why?

• Computing and Programming: a life skill for all technical professionals– http://www.youtube.com/watch?

v=dmM_xDzy2nU

• Thinking Computationally: organizing your engineering/scientific/technical thoughts to be amenable to computational solutions

Page 4: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 4

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

Page 5: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 5

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

Page 6: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 6

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.

Page 7: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 7

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

Closed book, one 8½-by-11 sheet of prepared notes, no calculators or electronics

Page 8: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 8

Prerequisites

• First-level gen-ed math course

Page 9: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 9

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)

Page 10: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 10

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

Page 11: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 11

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!

Page 12: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 12

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.

Page 13: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 13

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).

Page 14: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 14

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

Page 15: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 15

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

Page 16: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 16

Additional Help

• Academic Resource Center has Tutors available to assist in Wachman 200.

Page 17: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 17

Questions?

Page 18: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 18

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

Page 19: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 19

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

Page 20: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 20

Your First C Program

#include <stdio.h>

int main () {

printf(″Hello, World!\n″);

return 0;

}

Page 21: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 21

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

Page 22: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 22

So where is printf declared?

#include <stdio.h>

int main () {

printf(″Hello, World!\n″);

return 0;

}

Page 23: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 23

So where is printf declared?

#include <stdio.h>

int main () {

printf(″Hello, World!\n″);

return 0;

}

Answer: in this file!

Page 24: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 24

Your First C Program

#include <stdio.h>

int main () {

printf(″Hello, World!\n″);

return 0;

}

• A header file• Contains declarations

of names, functions, data, of things defined elsewhere

• E.g., by the system

• Text of the header file is inserted by compiler into your program

• As if you wrote it yourself!

Page 25: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 25

Your First C Program

#include <stdio.h>

int main () {

printf(″Hello, World!\n″);

return 0;

}

• A function declaration• Declares the name and

defines the body of your function

• May take arguments, returns an integer

• main is a special name to the system

• The place where a program “starts”

Page 26: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 26

Your First C Program

#include <stdio.h>

int main () {

printf(″Hello, World!\n″);

return 0;

}

• Body of the function• Defines what the

function “does”• Sequence of

statements• Each does a step of the

function

• Enclosed in curly brackets

•{ }

Page 27: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 27

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

Page 28: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 28

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

Page 29: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 29

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

Page 30: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 30

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

Page 31: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 31

Questions?

Write, compile, and execute this program in Lab session

Page 32: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 32

What happens to your program …

…after it is compiled, but before it can be run?

Page 33: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 33

Example

#include <stdio.h>

int main () {

printf (″Hello, world\n″);

}

• Symbol defined in your program and used elsewhere

•main

• Symbol defined elsewhere and used by your program

•printf

Page 34: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 34

Static Linking and LoadingPrintf.c

Printf.o

Librarygcc

ar

Linker

Memory

HelloWorld.c

gcc

HelloWorld.o

Loader

a.out(or name of

your command)

Page 35: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 35

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)

Page 36: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 36

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

Page 37: CIS 1057 Computer Programming in C

IntroductionCIS 1057 Fall 2013 37

Questions?