CMPE-013/L. Introduction to C Programming. Unit testing

Similar documents
Storage Class Specifiers Scope and Lifetime of Variables CMPE-013/L. Modules and Scope. Storage Class Specifiers Automatic Variables

Functions CMPE-013/L. Functions. Functions What is a function? Functions Remember Algebra Class? Gabriel Hugh Elkaim Winter 2014.

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

A Fast Review of C Essentials Part II

Pointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Fundamentals of Programming Session 12

CSE 230 Intermediate Programming in C and C++ Functions

University of Kelaniya Sri Lanka

CMPE-013/L. Expressions and Control

Tutorial 6. Memory Addresses and Pointers. scanf() function. Perils of Pointers. Function Pointers. Declarations vs. Definitions

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

Introduction to Programming Using Java (98-388)

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Functions and Recursion

Object Oriented Design

Protection Levels and Constructors The 'const' Keyword

Wednesday, October 15, 14. Functions

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

CSE 333 Lecture 7 - final C details

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Final thoughts on functions F E B 2 5 T H

Function Call Stack and Activation Records

M.EC201 Programming language

CSE 333 Lecture 2 Memory

Part 1: Introduction to the C Language

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CS201 Some Important Definitions

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Functions. Chapter 5

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

An Introduction to C++

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

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

Deep C (and C++) by Olve Maudal

CMPE-013/L. Introduction to C Programming

Lecture 8 Tao Wang 1

Lecture 03 Bits, Bytes and Data Types

Constructors for classes

M1-R4: Programing and Problem Solving using C (JAN 2019)

User Defined Functions

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

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

Storage class in C. Automatic variables External variables Static variables Register variables Scopes and longevity of above types of variables.

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

CMPE-013/L. Introduction to C Programming

Review of the C Programming Language for Principles of Operating Systems

Scope: Global and Local. Concept of Scope of Variable

Supplement: Visual C++ Debugging

Summer 2003 Lecture 14 07/02/03

COMP 401 Spring 2014 Midterm 1

Lecture 18 Tao Wang 1

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

CS 11 C track: lecture 5

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

C: How to Program. Week /Mar/05

Deep C by Olve Maudal

Introduction to C Programming

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

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Advances in Programming Languages: Regions

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Chapter 7 Functions. Now consider a more advanced example:

Recharge (int, int, int); //constructor declared void disply();

Tutorial 5. Call Stack and Stack Frames. Memory Addresses and Pointers. Content vs Address of Pointers. scanf() function. Perils of Pointers

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Functions and Recursion

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

Lecture 2, September 4

Chapter 2 - Introduction to C Programming

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Winter 2012 MID-SESSION TEST Tuesday, March 6 6:30pm to 8:15pm. Please do not write your U of C ID number on this cover page.

Fundamentals of Programming

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Contents. 8-1 Copyright (c) N. Afshartous

Unit 7. Functions. Need of User Defined Functions

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Informatica e Sistemi in Tempo Reale

CS313D: ADVANCED PROGRAMMING LANGUAGE

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CS 11 C track: lecture 6

Transcription:

CMPE-013/L Introduction to C Programming Gabriel Hugh Elkaim Winter 2015 Example Unit testing Testing architecture // Declare test constants testinput some input testexpoutput precalculated output // Calculate result testactoutput function result // Output test results if testactoutput equals testexpoutput output "Test passed" else output "Test failed!" 1

Unit testing Trivial example ExampleLib.c int AddFive(int x) return x + 5; main.c #include "ExampleLib.h" // Declare test constants int test1input = 0; int test1expoutput = 5; // Calculate result int test1actoutput; test1actoutput = AddFive(test1Input); // Output test results if (test1actoutput == test1expoutput) printf("test1 passed.\n"); else printf("test1 failed!\n"); Write multiple tests Unit testing Writing tests At least 1 for every group of inputs Each edge case should have their own test Each test should check one part of the total functionality One function or logical block of code at a time Try to break the code you're testing! 2

Unit testing Testing framework Track how many tests passed/failed Per function Track how many functions passed/failed With all tests must pass for the function to pass Each test cleanly separated from other tests Both in code and in logic Output results Per function/per test results Unit testing example 3

Parameter passing Pass by value Pass by reference Parameter Passing By Value Parameters passed to a function are generally passed by value Values passed to a function are copied into the local parameter variables The original variable that is passed to a function cannot be modified by the function since the function has a duplicate of the variable, not the original 4

Example int a, b, c; int Foo(int x, int y) x = x + (++y); return x; a = 5; b = 10; c = Foo(a, b); Parameter Passing By Value The value of a is copied into x. The value of b is copied into y. The function does not change the value of a or b. main() Foo() z Parameter Passing By Value Stack (top) Return address x y b a Return address Example function int Foo(int x, int y) int z = x + (++y); return z; Example main int a = 6, b = 19; Foo(a, b); while (1); 5

Parameter Passing By Reference Parameters can be passed to a function by reference Entails passing around memory address The original variable that is passed to a function can be modified by the function since the function knows where the data "lives" in memory Parameter Passing By Reference main() Foo() Stack (top) int*x Return address intz int a[0] int a[1] int a[2] Return address Example function int Foo(int x[3]) int z = x[2]; x[1] = 0; return z; Example main int a[3] = 6, 19, -1; Foo(a); while (1); 6

Scope Example Scope Variables Declared Within a Function Variables declared within a code block are local to that block. int x, y, z; int Foo(int n) int a; The n refers to the function parameter n a += n; The a refers to the a declared locally within the function body 7

Example Scope Variables Declared Within a Function Variables declared within a block are not accessible outside that block int x; int Foo(int n) int a; return (a += n); x = Foo(5); x = a; This will generate an error. a may not be accessed outside of the scope where it was declared. Example Scope Variables Declared Within a Function Variables declared within a block are not accessible outside that block int x; int a = 6; x = Foo(5); x = a; This will generate an error. a may not be accessed outside of the scope where it was declared. 8

Scope And the stack main() Foo() Stack (top) z Return address x y b a Return address Example function int Foo(int x, int y) int z = x + (++y); return z; Example main int a = 6, b = 19; Foo(a, b); while (1); Scope And the stack main() Stack (top) b a Return address Example function int Foo(int x, int y) int z = x + (++y); return z; Example main int a = 6, b = 19; Foo(a, b); while (1); 9

Scope Global versus Local Variables Example int x = 5; int Foo(int y) int z = 1; return (x + y + z); int a = 2; x = foo(a); a = foo(x); x can be seen by everybody foo's local parameter is y foo's local variable is z foo cannot see main's a foo can see x main's local variable is a main cannot see foo's y or z main can see x Scope Parameters "Overloading" variable names: n Declared Locally and Globally int n; n Declared Globally Only int n; int Foo(int n) y += n; local n hides global n int Foo(int x) y += n; A locally defined identifier takes precedence 10

Scope Parameters Example int n; int Foo(int n) y += n; int Bar(int n) z *= n; Different functions may use the same parameter names The function will only use its own parameter by that name Scope Preprocessor and scoping Example #define x 2 void Test(void) #define x 5 printf("%d\n", x); Result: 5 5 void main(void) printf("%d\n", x); Test(); 11

Storage Class Specifiers Scope and Lifetime of Variables Scope and lifetime of a variable depends on its storage class: Automatic Variables Static Variables External Variables Register Variables Scope refers to where in a program a variable may be accessed Lifetime refers to how long a variable will exist or retain its value Storage Class Specifiers Automatic Variables Local variables declared inside a function Created when function called Destroyed when exiting from function auto keyword usually not required local variables are automatically auto * Typically created on the stack int Foo(int x, int y) int a, b;... Automatic Variables *Except when the compiler provides an option to make parameters and locals static by default. 12

Storage Class Specifiers auto Keyword with Variables int Foo(auto int x, auto int y)... auto is almost never used Many books claim it has no use at all Some compilers still use auto to explicitly specify that a variable should be allocated on the stack when a different method of parameter passing is used by default Storage Class Specifiers Static Variables Given a permanent address in memory Exist for the entire life of the program Created when program starts Destroyed when program ends Global variables are always static (cannot be made automatic using auto) int x; Global variable is always static... 13

Storage Class Specifiers static Keyword with Variables A variable declared as static inside a function retains its value between function calls (not destroyed when exiting function) Function parameters cannot be static with some compilers (XC32) int Foo(int x) static int a = 0;... a += x; return a; a will remember its value from the last time the function was called. If given an initial value, it is only initialized when first created not during each function call Storage Class Specifiers External Variables Variables that are defined outside the scope where they are used Still need to be declared within the scope where they are used extern keyword used to tell compiler that a variable defined elsewhere will be used within the current scope External Variable Declaration Syntax: extern type identifier; External Variable Declaration Example: extern int x; 14

Example Storage Class Specifiers External Variables A variable declared as extern within a function is analogous to a function prototype the variable may be defined outside the function after it is used int Foo(int x) extern int a;... return a; int a; Storage Class Specifiers External Variables A variable declared as extern outside of any function is used to indicate that the variable is defined in another source file memory only allocated when it's defined Main.c extern int x; x = 5;... SomeFileInProject.c int x; int Foo(void)... 15

16

17