Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation

Similar documents
Introduction to Programming Systems

CSE2301. Functions. Functions and Compiler Directives

C Libraries. Bart Childs Complementary to the text(s)

Computer Programming

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Chapter 8 C Characters and Strings

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Scientific Programming in C V. Strings

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

CS3157: Advanced Programming. Outline

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

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

C Functions. 5.2 Program Modules in C

C: How to Program. Week /May/28

CSE123. Program Design and Modular Programming Functions 1-1

Chapter 8 - Characters and Strings

Characters and Strings

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Programming in C. Part 1: Introduction

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

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

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

Course organization. Course introduction ( Week 1)

Contents. Preface. Introduction. Introduction to C Programming

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Outline. Computer Programming. Structure of a function. Functions. Function prototype. Structure of a function. Functions

Functions. Systems Programming Concepts

C PROGRAMMING. Characters and Strings File Processing Exercise

C mini reference. 5 Binary numbers 12

Review Topics. Final Exam Review Slides

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Princeton University Computer Science 217: Introduction to Programming Systems. Modularity design principles

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Holtek C and ANSI C Feature Comparison User s Guide

ECE 2400 Computer Systems Programming Fall 2017 Topic 12: Transition from C to C++

C Programs: Simple Statements and Expressions

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

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

today cs3157-fall2002-sklar-lect05 1

Index. backslash character, 19 backup, off-site, 11. abs, 72 abstraction, 63, 83, 133, 141, 174, 181 acos, 72

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

C Functions Pearson Education, Inc. All rights reserved.

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

ECET 264 C Programming Language with Applications

Contents. A Review of C language. Visual C Visual C++ 6.0

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Object-Oriented Programming

CSE 333 Lecture 7 - final C details

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

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

1 Pointer Concepts. 1.1 Pointer Examples

Structured programming

CCE1111 Programming for Engineers [C Programming Language]

The C standard library

by Pearson Education, Inc. All Rights Reserved.

Introduction to string

C Review. SWE2004: Principles in Programming Spring 2014 Euiseong Seo

,$5(0%(''(':25.%(1&+ $16,&'(9(/230(17722/6 EMBEDDED WORKBENCH ANSI C COMPILER C-SPY FOR NATIONAL SEMICONDUCTOR CORP. S &RPSDFW5,6& 70 &5

High-performance computing and programming I. Introduction to C programming in *NIX

8. Characters, Strings and Files

Compiling and Running a C Program in Unix

Multiple Choice Questions ( 1 mark)

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

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

Lecture 04 FUNCTIONS AND ARRAYS

Chapter 8: Character & String. In this chapter, you ll learn about;

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

CS 261 Fall Mike Lam, Professor. Structs and I/O

C Characters and Strings

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

Tutorial 5. PDS Lab Section 16 Autumn Functions The C language is termed as function-oriented programming

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

Split up Syllabus (Session )

Library and function of C. Dr. Donald Davendra Ph.D. (Department of ComputingLibrary Science, andfei function VSB-TU of COstrava)

Arrays, Strings, & Pointers

CS167 Programming Assignment 1: Shell

SWEN-250 Personal SE. Introduction to C

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

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

C for C++ Programmers

OBJECTIVE QUESTIONS: Choose the correct alternative:

Princeton University Computer Science 217: Introduction to Programming Systems. Modularity design principles

Strings and Library Functions

Getting Started. Project 1

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

C Programming. Unit 9. Manipulating Strings File Processing.

Motor Industry Software Reliability Association (MISRA) C:2012 Standard Mapping of MISRA C:2012 items to Goanna checks

ECE 2400 Computer Systems Programming Fall 2018 Topic 11: Transition to C++

C programming basics T3-1 -

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Intermediate Programming, Spring 2017*

Lecture 03 Bits, Bytes and Data Types

Transcription:

Review: Constants Modules and s CS 217 C has several ways to define a constant Use #define #define MAX_VALUE 10000 Substitution by preprocessing (will talk about this later) Use const const double x = 1.56; Qualifier to declare that a variable is a constant Declare an enumerate constant type enum color WHITE, YELLOW, BLUE, RED ; enum color c2; Offers the chance of checking 1 2 Modules Programs are made up of many modules Each module is small and does one thing Set, stack, queue, list, etc. String manipulation Mathematical functions Deciding how to break up a program into modules is a key to good software design Clients, s, s s (Application Programming s or APIs) are contracts between clients and implementations Clients must use interface correctly s must do what they advertise Client 3 4

s s An interface defines what the module does Decouple clients from implementation Hide implementation details An interface defines what the module does Decouple clients from implementation Hide implementation details An interface specifies Data types and variables Functions that may be invoked An interface specifies Data types and variables Functions that may be invoked strlist.h: counter1.h extern int counter; extern void counter_init(); extern void typedef struct StrList *entries; int size; StrList; extern StrList *StrList_create(void); extern void StrList_delete(StrList *list); extern void StrList_insert(StrList *list, char *string); extern void StrList_remove(StrList *list, char *string); extern int StrList_write(StrList *list); 5 6 s s An implementation defines how the module does it An implementation defines how the module does it Can have many implementations for one interface Different algorithms for different situations Machine dependencies, efficiency, etc. Can have many implementations for one interface Different algorithms for different situations Machine dependencies, efficiency, etc. counter1.c int counter; void counter_init() counter = 0; void counter_inc() counter++; 7 StrList *StrList_create(void) StrList *list = malloc(sizeof(strlist)); list->entries = NULL; list->size = 0; void StrList_delete(StrList *list) free(list);... 8

Clients Clients A client uses a module via its interface A client uses a module via its interface Clients see only the interface Can use module without knowing its implementation Clients see only the interface Can use module without knowing its implementation Client is unaffected if implementation changes As long as interface stays the same Client is unaffected if implementation changes As long as interface stays the same test1.c #include <stdio.h> main() counter_init(); printf("%d\n", counter); 9 int main() StrList *list = StrList_create(); StrList_insert(list, CS217 ); StrList_insert(list, is ); StrList_insert(list, fun ); StrList_write(list); StrList_delete(list); 10 C Programming Conventions C Programming Conventions s are defined in header files (.h) counter1.h extern int counter; extern void counter_init(); extern void s are described in source files (.c) counter1.c int counter; void counter_init() counter = 0; strlist.c strlist.h typedef struct StrList *entries; int size; StrList; extern StrList *StrList_create(void); extern void StrList_delete(StrList *list); extern void StrList_insert(StrList *list, char *string); extern void StrList_remove(StrList *list, char *string); extern int StrList_write(StrList *list); 11 void counter_inc() counter++; StrList *StrList_create(void) StrList *list = malloc(sizeof(strlist)); list->entries = NULL; list->size = 0; void StrList_delete(StrList *list) free(list);... 12

C Programming Conventions Standard C Libraries Clients include header files test1.c #include <stdio.h> main() counter_init(); printf("%d\n", counter); int main() StrList *list = StrList_create(); StrList_insert(list, CS217 ); StrList_insert(list, is ); StrList_insert(list, fun ); StrList_write(list); StrList_delete(list); main.c 13 assert.h ctype.h errno.h math.h limits.h signal.h stdarg.h stddef.h stdio.h stdlib.h string.h time.h assertions character mappings error numbers math functions metrics for ints signal handling variable length arg lists standard definitions standard I/O standard library functions string functions date/type functions 14 Standard C Libraries, cont d Utility functions stdlib.h atof, atoi, rand, qsort, getenv, calloc, malloc, free, abort, exit String handling string.h strcmp, strncmp, strcpy, strncpy, strcat, strncat, strchr, strlen, memcpy, memcmp Character classifications ctypes.h isdigit, isalpha, isspace, isupper, islower Mathematical functions math.h sin, cos, tan, ceil, floor, exp, log, sqrt Example: Standard I/O Library stdio.h hides the implementation of FILE extern FILE *stdin, *stdout, *stderr; extern FILE *fopen(const char *, const char *); extern int fclose(file *); extern int printf(const char *, ); extern int scanf(const char *, ); extern int fgetc(file *); extern char *fgets(char *, int, FILE *); extern int getc(file *); extern int getchar(void); extern char *gets(char *);... extern int feof(file *); 15 16

Goals of Modularity Decomposability Divide a problem into sub-problems Composability Build a system using (reusable) building blocks Continuity A small spec change affects changes in a small number of modules Understandability Readability by people Protection Error occurs in a local place 17 Decomposability Divide a problem into sub-problems and work on each Use a top-down, layered approach Each layer provides an abstraction (by an interface) layered insensitivity Example: networking Application (FTP, email, etc.) Transport (TCP) Network (IP) Link (device driver and network interface) Avoid circular dependency 18 Decomposability Divide a problem into sub-problems and work on each Use a top-down, layered approach Each layer provides an abstraction (by an interface) layered insensitivity Example: networking Application (FTP, email, etc.) Transport (TCP) Network (IP) Link (device driver and network interface) Avoid circular dependency Composability Build software systems with building blocks (modules and interfaces) API calls are powerful, expressive and yet simple to use Good example Standard I/O redirection and pipes on Unix, and utility programs Bad example Hard-coded I/O device calls in earlier operating systems 19 20

Continuity A small change in the specification leads to small changes in a small number of modules Good examples Add a StrList_Sort call into the interface Add MMX to the Intel x86 processor architecture Bad example Change the definition of data type StrList Increasing the size of an IP address in IPv6 Understandability Understand a module by reading it or a few modules in its neighborhood Good examples Modules providing good abstractions (top-down layered) Layered insensitivity Bad examples An implementation that uses global variables defined and used in multiple modules More ad hoc module relationships 21 22 Protection Effect of an error is limited to one module or a small number of neighboring modules Good examples An error in StrList_insert() Exceptions in Java Bad examples An error occurs in a global variable modified by multiple modules Memory management (malloc/free) in C Separate Compilations Simple case Compile strlist.c to strlist.o Compile test.c and link with strlist.o Typical software product Compile many implementation.c files Link them into a library or build an executable 23 24

Summary A key to good programming is modularity A program is broken up into meaningful modules An interface defines what a module does An implementation defines how the module does it A client sees only the interfaces, not the implementations Modules have great advantages Easier to understand Easier to test and debug Easier to reuse code Easier to make changes 25