CS133 C Programming Instructor: Jialiang Lu Email: jialiang.lu@sjtu.edu.cn Office: Information Center 703 1
Course Information: Course Page: http://wirelesslab.sjtu.edu.cn/~jlu/teaching/cp2014/ Assignments (HW) Important News Assistant: Fan Yang: xcyangfan@sjtu.edu.c n QQ:445058757 QQ group administrator: 2
Reference books: Programming in C The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie Programming in C (3rd Edition) by Stephen G. Kochan. www.learn-c.org online training Code::Blocks: IDE for C, C++ and Fortain 3
Schedule: 9/17: Introduction to C, Fundamentals 9/24: Type, Operators, Expression 10/1: National Day 10/8: Control Flow & Formatted Input/Output 10/15: TP 10/22: TP 10/29: Functions and Program Structure 11/05: Pointers and Arrays Final exam will take place on Week 10 or 11. 4
WHAT YOU KNOW ABOUT PROGRAMMING? 5
Programming Paradigms 6
Imperative paradigm Programming Paradigms A sequence of commands that, when followed, manipulate data to produce the desired result Declarative paradigm Describe the problem rather than an algorithm Functional paradigm A program is viewed as en entity that accepts inputs and produces outputs Object-Oriented paradigm/programming (OOP) 7
Statements A program consists of a collection of statements Declarative statements int Height, Width; Imperative statements Z = X + Y; if.. else.. Comments //This is an addition /* Declaration of variable*/ 8
Variable and Data Types Variable is used by high-level programming language to be referenced in main memory. Identified by declarative statements int Height, Width; Data type the manner in which the data item is encoded The operations that can be performed on the data. 9
Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely in terms of a set of values and a set of operations on that data type. Each ADT operation is defined by its inputs and outputs. Encapsulation: Hide implementation details. 10
Data Structure A data structure is the physical implementation of an ADT. Each operation associated with the ADT is implemented by one or more subroutines in the implementation. Data structure usually refers to an organization for data in main memory. File structure is an organization for data on peripheral storage, such as a disk drive. 11
Metaphors An ADT manages complexity through abstraction: metaphor. Hierarchies of labels Ex: transistors gates CPU. In a program, implement an ADT, then think only about the ADT, not its implementation. 12
Logical vs. Physical Form Data items have both a logical and a physical form. Logical form: definition of the data item within an ADT. Ex: Integers in mathematical sense: +, - Physical form: implementation of the data item within a data structure. Ex: 16/32 bit integers, overflow. 13
Data Type ADT: Type Operations Data Items: Logical Form Data Structure: Storage Space Subroutines Data Items: Physical Form 14
Common data structure Array A block of elements of the same type One-dimension/two-dimension/etc. Int Scores[?][?][?] for premierleague Aggregate type Different elements can have different types Struct {char Name[25]; int Age; float SkillRating;} Employee; 15
Procedure Units A procedure is a set of instructions for performing a task that can be used as an abstract tool by other program units. 16
An example of Procedure 17
The translation process From source program to object program.c.o.java.class Lexical analyzer find token Parser Group units into statements Generating a parse tree Code generator Code optimization Source Program Lexical analyzer Parser Code generator Object Program tokens parse trees 18
SO YOU THINK YOU KNOW C? 19
A little history: 1970 s Unix C, from BCPL (Thompson and Ritchie) C programming Language Widely used like the others: Fortran, Pascal Main form of language for system programming Available on any machine with C compiler and library 20
Some Characteristics: Scope: Intended: HPC, OS, general programming Typically, long developing cycle very platform dependent Not intended: web, scripting, data processing Features: Close interaction with system Standard library (user-level only), no other fancy stuff: networking, memory, graphics Extensive use of pointers Procedure programming, strictly pass by value 21
Sample 1: /* Hello World! */ #include <stdio.h> int main() { printf( Hello World!\n ); return 0; } 22
Walk through: C program: hello.c emacs, vi, vim, pico, joe But text editors only. No word processor Preprocessing: hello.s, assembly code cc -S hello.c Compilation: hello.o, a binary file cc -c hello.s Linking: a.out or hello, an executable file cc hello.o cc -o hello hello.o Loading (dynamical linking) and execution:./hello./a.out./hello 23
A typical execution of a piece of C program 24
Sample 1 Dissection: What it has? A function definition: main A line of comment A line of preprocessor directive An output statement A return clause What it does? Ask the computer to say hello to the world. What it does not do? It seems not computing!! No real work not taking input does not change any value 25
Sample 2: #include <stdio.h> #define MAGIC 10 int main(void) { int i, fact, quotient; while (i++ < 3) { } printf( Guess a factor of MAGIC larger than 1: "); scanf("%d, &fact); quotient = MAGIC % fact; if (0 == quotient) else printf( You got it!\n ); printf( Sorry, You missed it!\n ); return 0; } 26
Sample 2 Dissection: What more it has? Macro definition Variable declaration Operations represented by operators Conditional computation Input Processing Loops: three chances 27
Syntax Dissection: What is syntax? m-w.com: the way in which linguistic elements (as words) are put together to form constituents (as phrases or clauses) How to put your C elements together into a program? Variable declaration, Identifier: case, keywords, style datatype specification Operators: char, short, int, long, float, double +, -, *, /, %, ++, --; >, <, ==, ; &,, Priority and Precedence Associativity 28
More Syntax Dissection: Preprocessor directive macro header file inclusion Loop constructs for, while, do-while, break, continue, label--go; Conditional statements If, else, else if, switch, break Function definition return type, parameter definition, parameter passing Standard library and function calls 29
A BREAK: CODE::BLOCKS 30