370 likes | 711 Views
CIS 1057 Computer Programming in C. Dr. Anwar Mamat Fall 2013 Acknowledgement: Many slides based on/ borrowed from Professor Hugh C. Lauer
E N D
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) Introduction
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” Introduction
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 Introduction
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 Introduction
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 … Introduction
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. Introduction
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 Introduction
Prerequisites First-level gen-ed math course Introduction
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) Introduction
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 Introduction
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! Introduction
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. Introduction
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). Introduction
Names and Faces Introduction • 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
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 Introduction
Additional Help Introduction • Academic Resource Center has Tutors available to assist in Wachman 200.
Questions? Introduction
The C Language First created to develop Unix – late 1960s Kernighan & Ritchie, 1stedition – 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 Introduction
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 Introduction
Your First C Program #include <stdio.h> int main () { printf(″Hello, World!\n″); return 0; } Introduction
Fundamental Rule in C Introduction • 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
So where is printf declared? #include <stdio.h> int main () { printf(″Hello, World!\n″); return 0; } Introduction
So where is printf declared? #include <stdio.h> int main () { printf(″Hello, World!\n″); return 0; } Answer: in this file! Introduction
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! Introduction
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” Introduction
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 • { } Introduction
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 Introduction
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 Introduction
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 Introduction
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 Introduction
Questions? Write, compile, and execute this program in Lab session Introduction
What happens to your program … …after it is compiled, but before it can be run? Introduction
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 Introduction
Static Linking and Loading Printf.c HelloWorld.c gcc Library gcc Printf.o ar HelloWorld.o Linker a.out(or name ofyour command) Loader Memory Introduction
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) Introduction
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 Introduction
Questions? Introduction