1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
Chapter 7: User-Defined Functions II
Kernighan/Ritchie: Kelley/Pohl:
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
0 Chap. 4 Functions and Program Structure 4.1 Basics of Functions 4.2 Functions Returning Non-integers 4.3 External Variables 4.4 Scope Rules 4.5 Header.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
C++ for Engineers and Scientists Third Edition
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
chap13 Chapter 13 Programming in the Large.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Engineering Computing I Chapter 4 Functions and Program Structure.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used to group declarations and.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
C++ Functions A bit of review (things we’ve covered so far)
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Introduction to Programming
CSC201: Computer Programming
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
C Programming Tutorial – Part I
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Chapter 5 Conclusion CIS 61.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Pre-processor Directives
Scope, Parameter Passing, Storage Specifiers
Preprocessor C program → Modified C program → Object Code
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
C Preprocessor(CPP).
Classes and Objects.
CSc 352 An Introduction to the C Preprocessor
Function.
Homework Finish up K&R Chapters 3 & 4
C Preprocessor Seema Chandak.
The C Language: Intro.
What Is? function predefined, programmer-defined
Function.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions

2 Review An automatic variable is one normally declared inside the braces of a function: –The scope of the variable is private to the function (accessible within the function braces) –It cannot be referenced from outside the braces –It appears on the stack when the function is called and disappears when the function returns –Undefined (i.e. garbage) value unless explicitly initialized in the source code –It doesn't hold a value between invocations

3 Review A static variable is declared inside the braces of a function (with keyword static): –The scope of the variable is private to the function (accessible within the function braces) –It cannot be referenced from outside the braces –It does NOT appear on the stack –It is guaranteed to be initialized to zero –It keeps its value across function invocations

4 Review An external variable is one declared outside the braces of any function in the file: –It is accessible by any function in the file –It is private to the file unless you declare same variable name with type "extern" in another file. –It does not appear on the stack –It is guaranteed to be initialized to zero –It holds its value across all function invocations This “global” variable can be troublesome!

5 Review An external static variable is declared outside of any function (with keyword static), –It is accessible by any function in the file –It is private to the file and cannot be referenced from another file (not even by using “extern”) –It does not appear on the stack –It is guaranteed to be initialized to zero –It holds its value across all function invocations Safer than global variable (like class variable)

6 Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register, if possible Gives extra speed for frequently modified variables. There are a limited number of registers in machine (where arithmetic is usually performed -- have to load register from memory location of variable, perform increment/decrement, store value back). Undefined (i.e. garbage) value unless explicitly initialized in the source code

7 Register Variables void f(int n)void f(register int n){ int i;register int i; for (… ;… ; i++, n--)for (... ;... ; i++, n--) /* during each pass *//* during each pass */ LD r1, &iINCr1(i held in r1) INC r1DECr2(n held in r2) ST r1, &i LDr1, &n DECr1 STr1, &n

8 Register Variables Advisory only, because number of registers is quite limited, and only in creating the code is it clear what is best. Still, it usually works. A static variable cannot be register type (use register for all time). Can only have automatic variables and formal function parameters (treated like local variables) as register variables. A pointer points to a position in memory. If n is a register variable, can't use “address of”  &n!

9 C Preprocessor Inclusion of other files – usually.h files #include “filename.h” Simple macro substitution #define name substitute text Macro substitution with arguments #define square(A) ((A)*(A)) enclose in ( )s n = square(x) + 1;  n = ((x)*(x)) + 1; Conditional inclusion

10 Macros Macros do not understand C expressions. They are only doing precise character substitution. Macro substitution with arguments – bad example #define square(A) A*A If you write in program: n = square(p+1); Macro will be replaced by: n = p+1*p+1; Not what you expected

11 Macros Macro must be defined on a single line Can continue a long definition to the next line using backslash character (\) #define exchg(t, x, y) {t d; d = x; x = y;\ y = d;} The \ simply tells compiler the following line is a continuation of same logical line

12 Macros This macro invokation exchg (char, u, v) will be turned into the following text string (shown one statement per line for clarity) {char d; /* Note: d is defined locally within block */ d = u; u = v; v = d; }

13 Macros Function calls are CALL BY VALUE (can't modify calling program’s argument values inside function unless pointers are passed) This is NOT true for Macros, because statements within a Macro expansion act like in-line code! Frequently used Macro may take more memory than a function, but does not require call/return and stack frames! (Macro will usually execute faster)

14 Macros Substitutions are not done within quotation marks. If we want to print out the variable makeup of an expression and then the value (e.g., x/y = 17.2), it doesn't work to do it like this: #define dprint(expr) printf("expr = %f\n", expr) dprint(x/y); We want: “x/y = 17.2” printed but, it expands as: printf ("expr = %f\n", x/y);

15 Macros However, using a special convention with the # character. When we define: #define dprint(expr) printf(#expr " = %g\n", expr) The special form #expr means: –Do substitution for the macro “expr” –Put quotes around result Now if we write: dprint(x/y); this expands as: printf("x/y" " = %g\n", x/y); /* two strings concatenated */

16 Conditional Inclusion Gives control of when precompiler directives such as #define or #include are executed It’s done before compilation with conditionals that are meaningful at that time Conditionals work for any C statements in their scope, and can be used to drop unneeded code (and save memory space) under some conditions

17 Conditional Inclusion #if (with conditions such as !defined(SYM) #ifdef SYM (like saying #if defined(SYM) #ifndef SYM (like saying #if !defined(SYM #elif (like else if) #else (like else) #endif (end scope of originating #if)

18 Conditional Inclusion A software release might need different.h header files included for different O/S's Before main() we might define: #define SYSV 100 #define BSD 101 For a specific system (say SYSV) we write: #define SYSTEM SYSV

19 Conditional Inclusion Define.h file symbolic constants conditionally: #if SYSTEM == SYSV #define HDR "sysv.h" #elif SYSTEM == BSD #define HDR "bsd.h" #else #define HDR "default.h“ #endif #include HDR

20 Conditional Inclusion Another reason for using conditionals is that some.h files include other headers recursively. It might happen as a result that we include two header files that both recursively include the same.h file This can lead to a compiler warning, maybe even an error, if the a function prototype appears twice during a compilation We DON’T want to include declarations contained in a.h file twice. These conditionals achieve this, when placed inside a header named xxx.h

21 Conditional Inclusion #ifndef XXX_HDR #define XXX_HDR /* note we don't need to give a value here */ /* next time XXX_HDR will be defined so #ifndef will fail */... (contents of.h file go here) #endif /* XXX_HDR */

22 Recursion, K&R Section 4.10 Any C function may call itself recursively, either directly or after intermediate function calls Each time a call is made, a new frame is placed on the stack, containing passed arguments, a position to return to, and automatic variables (if any) int factorial (int n) { return (n > 1) ? n * factorial (n - 1) : 1 } Programmer must ensure the recursion terminates!

23 Recursion, Stack Frames Stack during recursion: int result = factorial (3); factorial (3) factorial (2) factorial (1) Stack Pointer Stack Pointer Stack Pointer Stack Pointer

24 Recursion, Performance Time relative to while/for loops –Calling/creating stack frame takes a lot of time –Returning/removing stack frame costs time, too Memory relative to while/for loops –Stack frames eat up memory  need large space! –In non-virtual memory system  stack overflow? Recursion is rarely used in hard real-time and/or embedded systems for these reasons