Course organization Course introduction ( Week 1) Part I: Introduction to C programming language (Week 3-12)

Similar documents
Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4)

Course organization. Course introduction ( Week 1)

Chapter 3. Functions

Chapter 3. Functions

Functions Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay

CS240: Programming in C

Course organization. Course introduction ( Week 1)

Course organization. Course introduction ( Week 1)

Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4)

CMPE-13/L: COMPUTER SYSTEMS AND C PROGRAMMING WINTER 2015

Introduction to C. CS2023 Winter 2004

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

C and C++ 2. Functions Preprocessor. Alan Mycroft

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Summer May 11, 2010


The C standard library

#include. Practical C Issues: #define. #define Macros. Example. #if

COMsW Introduction to Computer Programming in C

Functions BCA-105. Few Facts About Functions:

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Technical Questions. Q 1) What are the key features in C programming language?

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Chapter 10. Programming in C

Lecture 3: C Programm

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Programming in C in a UNIX environment

Functions. Cedric Saule

OBJECTIVE QUESTIONS: Choose the correct alternative:

C Programming Review CSC 4320/6320

Intermediate Programming, Spring 2017*

Unit 4 Preprocessor Directives

This lists all known errors in The C Programming Language, Second Edition, by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1988).

A Crash Course in C. Steven Reeves

(T) x. Casts. A cast converts the value held in variable x to type T

CSE2301. Functions. Functions and Compiler Directives

Topic 6: A Quick Intro To C

Have examined process Creating program Have developed program Written in C Source code

Lecture 2: C Programming Basic

Decision Making -Branching. Class Incharge: S. Sasirekha

G Programming Languages - Fall 2012

Programming for Engineers C Preprocessor

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS240: Programming in C

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

CSCI 171 Chapter Outlines

EL6483: Brief Overview of C Programming Language

Week 5 Lecture 3. Compiler Errors

Chapter 7: Preprocessing Directives

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

ECE264 Spring 2013 Final Exam, April 30, 2013

CSE 333 Lecture 7 - final C details

PRINCIPLES OF OPERATING SYSTEMS

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Computer Systems Lecture 9

ECE264 Spring 2013 Exam 1, February 14, 2013

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

COMP327 Mobile Computing Session: Tutorial 2 - Operators and Control Flow

Subject: Fundamental of Computer Programming 2068

CS3157: Advanced Programming. Outline

ECE264 Fall 2013 Exam 1, September 24, 2013

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Function Call Stack and Activation Records

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

File Access. FILE * fopen(const char *name, const char * mode);

Fundamentals of Programming

The C Preprocessor Compiling and Linking Using MAKE

More on C programming

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Programming in C week 1 meeting Tiina Niklander

A Fast Review of C Essentials Part II

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Chapter 6: The C Preprocessor

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

CS 241 Data Organization Pointers and Arrays

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Part 1: Introduction to the C Language

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Object Oriented Design

CSE 230 Intermediate Programming in C and C++ Functions

Unit 7. Functions. Need of User Defined Functions

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Creating, Compiling and Executing

Informatica e Sistemi in Tempo Reale

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Introduction to C. Systems Programming Concepts

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Lecture 03 Bits, Bytes and Data Types

In addition to name. Each variable in C program is characterized by its Type Scope Storage Class. Type Have already discussed

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Transcription:

1 Course organization Course introduction ( Week 1) Code editor: Emacs (Week 2) Part I: Introduction to C programming language (Week 3-12) Chapter 1: Overall Introduction (Week 3-4) Chapter 2: Types, operators and expressions (Week 5) Chapter 3: Control flow (Week 7) Chapter 4: Functions and program structure (Week 8) Chapter 5: Pointers and arrays (Week 9) Chapter 6: Structures (Week 10) Chapter 7: Input and Output (Week 11) Part II: Skills others than programming languages (Week 12-13) Debugging tools(week 12) Keeping projects documented and manageable (Week 13) Source code managing (Week 13) Part III: Reports from the battle field (student forum) (Week 14 16) Presentation (week 14-15) Demo (week 16)

1896 1920 1987 2006 Chapter 4. Function and Program Structure Chaochun Wei Shanghai Jiao Tong University Spring 2018

Functions Break large program into smaller ones Enable people to build on existing codes Hide details of operation Clarify the whole program Make it easier to modify a program

4.1 Basics of functions Function definition return-type function-name(argument declarations) { declarations and statements E.g.: A minimal function dummy () { /* this is a do-nothing and return-nothing function. return type int is assumed */

4.1 Basics of functions Eg: chpapt4_1_functions.c /* print lines containing a certain text pattern */ #include <stdio.h> #define MAXLINE 1000 /* maximum input line length */ int getline_ ( char line[], int max); int strindex (char source[], char searchfor[]); char pattern[] = "ould"; /* pattern to search for */ /* getline: get line into s, return length */ int getline_ (char s[], int lim) { int c, i; i = 0; while (--lim > 0 && (c=getchar())!= EOF && c!= '\n' && c!= ';') s[i++] = c; if (c == '\n' ) s[i++] = c; else if (c == ';') return i; /* find all lines matching pattern */ main () { char line[maxline]; int found = 0; while (getline_ (line, MAXLINE) > 0) if (strindex(line, pattern) >= 0 ) { printf("%s", line); found ++; return found; s[i] = '\0'; return i; /* strindex: return index of t in s, -1 if one */ int strindex (char s[], char t[]) { int i, j, k; for (i = 0; s[i]!= '\0'; i ++ ) { for (j = i, k= 0; t[k]!= '\0' && s[j] == t[k]; j ++, k++) ; if (k > 0 && t[k] == '\0') return i; return -1;

4.1 Basics of functions Compile and load a C program from multiple source files gcc -c main.c getline.c strindex.c or gcc main.c getline.o strindex.o will create an executable called a.out Please divide the chpat4_1_functions.c into three files, compile, and run as above.

4.2 Functions returning non-integers Functions other than void and integer type Example: /* atof: convert string s to double */ double atof(char s[ ]) { double val, power; int I,k, sign; for (i = 0; isspace(s[i]); i ++) /* skip white spaces */ ; sign = (s[i] == '-')? -1: 1; if (s[i] == '+' s[i] === '-') i ++; for ( val = 0.0; isdigit(s[i]); i ++) val = 10.0 * val + (s[i] - '0'); if ( s[i] == '.') i ++; for (power = 1.0; isdigit(s[i]); i ++) { val = 10.0 * val + ( s[i] - '0'); power *= 10.0; return sign * val/power; Eg:chapt4_2_atof_main.c

4.3 External variables ( 外部变量 ) Are globally accessible and they can be referenced by Many functions With the same name Provide a communication between functions Can have a bad effect leading to too many data connections between functions Eg: chapt4_3_polish.c

4.4 Scope rules Source codes can be in different files Variable declaration organization Variable initialization Declaration and definition of an external variable extern int sp; extern double val []; /* this is a declaration */ Eg:polish_cal_main.c Initialization goes with the definition

main.c #include <stdio.h> #include <stdlib.h> #include calc.h #define MAXOP 100 Main ( ) { Eg: header_file 4.5 Header files ( 头文件 ) calc.h #define NUMBER 0 void push (double); double pop (void); int getop (char [ ]); int getch ( void ); void ungetch (int); getop.c #include <stdio.h> #include <ctype.h> #include calc.h getop() { getch.c #include <stdio.h> #define BUFSIZE 100 char buf[bufsize] int bufp = 0; int getch (void) {. void ungetch (int) { stack.c #include <stdio.h> #include calc.h #define MAXVAL 100 int sp = 0; double val[maxval]; void push (double) { double pop(void) {

4.6 Static variables( 静态变量 ) Stored in the data segment Exist across the whole run of the program No routines in other files will be able to access static variables

4.7 Register variables ( 寄存器变量 ) If variables will be heavily used, define them as register variables

4.8 Block structure( 模块结构 ) Variables can be defined after a left brace if ( n > 0) { int i; /* declare a new I */ for ( i = 0; i < n; i++). The variable i is initialized each time the block is entered

4.9 Initialization ( 初始化 ) Without explicit initialization External variables Static variables are initialized to 0 Other variables have undefined initial values Explicit initialization Character array can be initialized with a string char pattern[] = ould

4.10 Recursion ( 递归 ) A function may call itself Eg:recursion 1. /* qsort sort v[left]...v[right] into increasing order */ 2. void qsort (int v[], int left, int right) { 3. int i, last; 4. void swap ( int v[], int i, int j); 5. if (left >= right) /* do nothing if array contains */ 6. return; /* fewer than two elements */ 7. swap(v, left, (left + right)/2); /* move partition element to v[0] */ 8. last = left; 9. for ( i = left + 1; i <= right; i++) /* partition */ 10. if (v[i] < v[left]) swap(v, ++last, i); 11. swap(v, left, last); /* restore partition elem */ 12. qsort ( v, left, last - 1); 13. qsort (v, last +1, right); 14.

4.11 The C preprocessor The first step in compilation Most frequently used features #include #include <file> or #include file #define #define name replacement_text Conditional compilation Macros with arguments #define max(a, B) ((A)>(B)? (A): (B))

4.11 The C preprocessor Conditional compilation #ifndef HDR #define HDR /* contents of hdr.h go here */ #endif

4.11 The C preprocessor Conditional compilation #if SYSTEM == SYSV #define HDR sysv.h #elif SYSTEM == BSD #define HDR bsd.h #elif SYSTEM == MSDOS #define HDR msdos.h #else #define HDR default.h #endif #include HDR