Summer May 11, 2010

Similar documents
Your Instructor. CSE Content. Notes. Notes. Notes. Summer May 4, 2010

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

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

OBJECT ORIENTED PROGRAMMING USING C++

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

QUIZ. What are 3 differences between C and C++ const variables?

Programming for Engineers C Preprocessor

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

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

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

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

Software Testing Lecture 1. Justin Pearson

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

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

Conditional Compilation

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS240: Programming in C

QUIZ. What is wrong with this code that uses default arguments?

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CS Programming In C

Chapter 7: Preprocessing Directives

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Important From Last Time

Axivion Bauhaus Suite Technical Factsheet MISRA

Short Notes of CS201

MISRA-C. Subset of the C language for critical systems

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

Introduction to Programming (Java) 4/12

CS201 - Introduction to Programming Glossary By

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Presented By : Gaurav Juneja

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CSE 374 Programming Concepts & Tools

CSCI 171 Chapter Outlines

Key Differences Between Python and Java

LESSON 6 FLOW OF CONTROL

Testing. UW CSE 160 Winter 2016

UNIT- 3 Introduction to C++

EL6483: Brief Overview of C Programming Language

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

DEPARTMENT OF MATHS, MJ COLLEGE

The SPL Programming Language Reference Manual

Chapter 4 Defining Classes I

Contents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

Basic Types, Variables, Literals, Constants

Guidelines for Writing C Code

CS240: Programming in C

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

C Programming Language (Chapter 2 of K&R) Variables and Constants

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Visual Analyzer V2.1 User s Guide

CSI33 Data Structures

MISRA-C:2012 Standards Model Summary for C / C++

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

C Language, Token, Keywords, Constant, variable

Writing better code Loop invariants Correctness. John Edgar 2

fpp: Fortran preprocessor March 9, 2009

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

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

COMP 202 Java in one week

Programming in the Real World. Dr. Baldassano Yu s Elite Education

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

A Fast Review of C Essentials Part I

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

C-LANGUAGE CURRICULAM

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

Test Driven Development Building a fortress in a greenfield (or fortifying an existing one) Dr. Hale University of Nebraska at Omaha

TDDE18 & 726G77. Functions

Type Conversion. and. Statements

CSE 403: Software Engineering, Fall courses.cs.washington.edu/courses/cse403/16au/ Unit Testing. Emina Torlak

Who is our rival? Upcoming. Testing. Ariane 5 rocket (1996) Ariane 5 rocket 3/8/18. Real programmers need no testing!

Lexical Considerations

G52CPP C++ Programming Lecture 9

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Accelerating Information Technology Innovation

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

Syntax and Variables

Program Correctness and Efficiency. Chapter 2

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

6.096 Introduction to C++ January (IAP) 2009

A Shotgun Introduction to C

Topic 6: A Quick Intro To C

1 Lexical Considerations

COMP322 - Introduction to C++

A Crash Course in C. Steven Reeves

Lexical Considerations

Lecture 9 Assertions and Error Handling CS240

Appendix G C/C++ Notes. C/C++ Coding Style Guidelines Ray Mitchell 475

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

Transcription:

Summer 2010 Department of Computer Science and Engineering York University Toronto May 11, 2010 1 / 40

What we did last time Overview of C to the language Program structure Types in C Operators in C IO and Files in C Overview of UNIX Why UNIX? Philosophy of UNIX Structure of UNIX 2 / 40

What we will do today? 3 / 40 1 Post-condition 2

Plan 4 / 40 1 Post-condition 2

Why Tests? 1990 AT&T long distance calls fail for 9 hours Wrong location for C break statement 1996 Ariane rocket explodes on launch Overflow converting 64-bit float to 16-bit integer 1999 Mars Climate Orbiter crashes on Mars Missing conversion of English units to metric units Therac: A radiation therapy machine that delivered massive amount of radiations killing at leaset 5 people Among many others, the reuse of software written for a machine with hardware interlock. Therac did not have hardware interlock. 5 / 40

Idea of Testing is getting sure your code is correct (no bugs). In reality, you can only detect the existence of bugs, not their absence (Dijkstra). Multiple runs of code using different inputs. 6 / 40

Unit Do not wait till you complete the program to test it, test every piece that you write (function, block, if, ) If you wait until something breaks, you probably have forgotten what the code does. It takes time because sometimes additional work has to be done i.e. stub 7 / 40

Testing What do you need for? The code you want to test Some inputs What is the correct output of the above inputs, so you can compare. Test Coverage Did you cover every statement in the code? 8 / 40

input inputs to the program Easy to do Without a statistical framework, the results are meaningless. 9 / 40

No knowledge of the implementation (code) Test based on the specifications. Tests prepared before implementation. Tests prepared by some one else other than the person who will write (wrote) the code. May not test every path in the program! 10 / 40

Full knowledge of the program. Test cases should test (cover) all different paths in the program. i f ( a> b ) { x =... ; i f ( c>=d ) { x =... ; y =... ; } e l s e {.... } e l s e {..... } } 11 / 40

Over and over again When you fix a bug, you may introduce another bug. When you fix a bug, you may break another fix When you create a test, keep it When you fix a bug, apply all previous 12 / 40

What is the boundary condition? How can we identify boundary? 13 / 40

Post- Pre Check if the input is correct Post Check if your output is correct 14 / 40

You can use assertion facilities in <assert.h> Use it only when the failure is really unexpected and there is no way to recover (however you may use it to test pre-, post- and loops invariants) assert (n>0); If that is not true, the program terminates with a message saying the assertion failed. 15 / 40

Lets consider the GCD 16 / 40

Plan 17 / 40 1 Post-condition 2

and Brake large computing tasks into smaller Can be reused Where the name can be used/visible? 18 / 40

Function basics Limitations of human perception Human usually can focus on 5+/-2 elements Brake complex tasks into smaller During the design stage try to separate small tasks that may be implemented as single function. Simple rule Try to fit the function on one screen 19 / 40

Definition and Declaration Declaration r e t u r n e d t y p e f u n c t i o n n a m e ( l i s t o f a r g u m e n t s ) ; Definition r e t u r n e d t y p e f u n c t i o n n a m e ( l i s t o f a r g u m e n t s ) { d e c l a r a t i o n s and s t a t e m e n t s } Return statement return e x p r e s s i o n ; 20 / 40

.c,.h,.o 21 / 40 Header.h System header files declare the interfaces to parts of the operating system. Your own header files contain declarations for interfaces between the source files of your program. Including a header file produces the same results as copying the header file into each source file that needs it. In C, header files names end with.h. It is most portable to use only letters, digits, dashes, and underscores in header file names, and at most one dot. Body.c Contains includes of headers and definitions of functions. Object.o Object files are compiled from the source+header files. We can have program divided into several modules. All modules then can be compiled separately and linked later into the one executable.

Return Function uses return statement to return the result to the caller can return arbitrary type: void, int, double, pointer (to the variable or function) etc Inconsistent expression will be casted to the returned type of the function 22 / 40

External variables Internal variables Defined inside of the function body and exists only when the function is executed External objects External variables and function are defined outside of any function. External variables may be used as a tool to communicate between functions 23 / 40

Too many externals is not good Important! Do not misuse external definitions (global variables)! Problem with externals Everyone can access the variable (like public member in Java) Low level of control Too many externals leads to bad program structure with too many data connections between functions (problem with reusing) 24 / 40

- Definition Following questions should be answered: How to write declarations so that variables are properly declared during compilation? How are declarations arranged so that all the pieces will be properly connected when program is loaded? How are declarations organized so there is only one copy? How external variables are initialized (so that all of them are initialize once)? Definition is a part of the program within which declared name can be used 25 / 40

- Definition Following questions should be answered: How to write declarations so that variables are properly declared during compilation? How are declarations arranged so that all the pieces will be properly connected when program is loaded? How are declarations organized so there is only one copy? How external variables are initialized (so that all of them are initialize once)? Definition is a part of the program within which declared name can be used 25 / 40

Extern declaration If we want to use the variable before it s definition or in other file then extern declaration is required. file1.c extern i n t s i z e ; extern char buf [ ] ; file2.c i n t s i z e = SIZE ; char buf [ SIZE ] ; 26 / 40

Static Variables Declaration static allows us to restrict the visibility (scope) of the variable (hide). file1.c s t a t i c i n t s i z e = SIZE ; s t a t i c char buf [ SIZE ] ; file2.c s t a t i c i n t s i z e = SIZE ; s t a t i c char buf [ SIZE ] ; 27 / 40

Static variables 28 / 40

Register Just a suggestion Declaration register is a suggestion or advice addressed to the compiler that the variable will be often used and should be placed in the machine register (fast access). Restrictions Size of registers Number of registers Size of type 29 / 40

Blocks and Declarations Variables can be defined in the beginning of the block: function compound statement (just after the left brace {) inner declaration hides outer declaration automatic variables (i.e. parameters) hides external variables i n t i =1; i f ( i >0) { i n t i ; f o r ( i =0; i <MAX; i ++) {... } } 30 / 40

Initialization In absence of explicit initialization: external and static variables are initialized to be zero; automatic and register variables have undefined value (garbage!!!) How to do it? external and static needs constant expression, done once before program runs; automatic and register initializer not restricted, done each time the block is entered; arrays are initialized by the list of members i n t x = 0 ; i n t a r r a y x []={1, 3, 10, 11, 15}; char t x t 1 [ ] = t e x t ; char t x t 2 [ ] = { t, e, x, t, \0 } ; 31 / 40

Recursion Definition Calling the function by itself directly or indirectly / r e c u r s i v e GCD / i n t gcd ( i n t m, i n t n ) { / base c a s e ( s ) m or n e q u a l s 0 / i f (m == 0) return ( n ) ; i f ( n == 0) return (m) ; } / now r e c u r s e / return ( gcd ( n, m % n ) ) ; 32 / 40

Recursion 33 / 40

Include #include "file" or #include <file> includes content of file during the compilation when file is quoted ( ) searching for the file begins in the dir where source program is if it is not found there or it is surrounded by < and > implementation defined rule is used to find included file (in in specified directory) includes may be cascade (included file may contain another includes) when included file changes all dependent source files (that have included it) should be recompiled 34 / 40

Define #define name replacement_text How it works? Subsequent occurrences of name will be replaced by the replacement_text. How to build it? Name has the same form as variable name replacement text usually is the rest of the line, but long texts can be continued in multiple lines with \ at the end of the line it is done for tokens quoted strings are not processed name can have parameters 35 / 40

Define #define MAX SIZE 100 #define MAX(A, B) ( (A)>(B)? (A ) : ( B) ) #define FOREVER f o r ( ; ; ) / i n f i n i t e l o o p / #define d p r i n t ( e x p r ) p r i n t f (#expr = %g\n, e x p r ) 36 / 40

Conditional inclusion Conditional inclusion provides us a way to control preprocessing and to include code selectively. #if, #elif, #else and #endif #if evaluates constant integer expression (except sizeof, cast and enum constants) if this expression is non-zero then subsequent lines are included until #elif, #else or #endif. defined(name) This expression has a value 1 if the name has been defined or 0 otherwise #ifdef name and #ifndef name can be used instead of #if defined(name) and #if!defined(name) respectively 37 / 40

Conditional inclusion 38 / 40

What have we done today? 39 / 40 1 Post-condition 2

Next time [KR] Chapter 5 Arrays Pointers 40 / 40