Course organization. Course introduction ( Week 1)

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) Part I: Introduction to C programming language (Week 3-12)

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

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

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


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

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

Programming in C in a UNIX environment

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

Functions. Cedric Saule

C Programming Review CSC 4320/6320

OBJECTIVE QUESTIONS: Choose the correct alternative:

Intermediate Programming, Spring 2017*

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

Unit 4 Preprocessor Directives

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

Summer May 11, 2010

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

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

Decision Making -Branching. Class Incharge: S. Sasirekha

G Programming Languages - Fall 2012

CS240: Programming in C

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

CSCI 171 Chapter Outlines

EL6483: Brief Overview of C Programming Language

ECE264 Spring 2013 Final Exam, April 30, 2013

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Chapter 7: Preprocessing Directives

Week 5 Lecture 3. Compiler Errors

PRINCIPLES OF OPERATING SYSTEMS

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

CSE 333 Lecture 7 - final C details

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

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

MODULE 5: Pointers, Preprocessor Directives and Data Structures

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

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

A Crash Course in C. Steven Reeves

ECE264 Fall 2013 Exam 1, September 24, 2013

Function Call Stack and Activation Records

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

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

Programming in C week 1 meeting Tiina Niklander

The C Preprocessor Compiling and Linking Using MAKE

Fundamentals of Programming

More on C programming

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

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

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

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

A Fast Review of C Essentials Part II

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; }

Part 1: Introduction to the C Language

CS 241 Data Organization Pointers and Arrays

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

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

CSE 230 Intermediate Programming in C and C++ Functions

Object Oriented Design

Unit 7. Functions. Need of User Defined Functions

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

COMP327 Mobile Computing Session:

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

Lecture 03 Bits, Bytes and Data Types

Introduction to C. Systems Programming Concepts

Informatica e Sistemi in Tempo Reale

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

Creating, Compiling and Executing

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

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

Transcription:

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

Chapter 4. Function and Program Structure Chaochun Wei Shanghai Jiao Tong University Spring 2013

Functions Break large program intro 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 /* print lines containing a certain text pattern */ #include <stdio.h> #define MAXLINE 1000 /* maximum input line length */ int getline_ ( char line[], int max); int strindx (char source[], char searchfor[]); char pattern[] = "ould"; /* pattern to search for */ /* 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; /* 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; 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

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 ik 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;

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

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

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 */ Initialization goes with the definition

main.c #include <stdio.h> #include <stdlib.h> #include calc.h #define MAXOP 100 Main ( ) { 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 No routines in other files will be able to access static variables

4.7 Register variables If variables will be heavily used Defined 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 /* qsort sort v[left]...v[right] into increasing order */ void qsort (int v[], int left, int right) { int i, last; void swap ( int v[], int i, int j); if (left >= right) /* do nothing if array contains */ return; /* fewer than two elements */ swap(v, left, (left + right)/2); /* move partition element to v[0] */ last = left; for ( i = left + 1; i <= right; i++) /* partition */ if (v[i] < v[left]) swap(v, ++last, i); swap(v, left, last); /* restore partition elem */ qsort ( v, left, last - 1); qsort (v, last +1, right);

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