CS240: Programming in C

Similar documents
CS240: Programming in C

CS240: Programming in C

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

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

CS240: Programming in C

Course organization. Course introduction ( Week 1)

MIPS Procedure Calls. Lecture 6 CS301

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Topic 7: Activation Records

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)

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

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

MODULE 5: Pointers, Preprocessor Directives and Data Structures

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Module 27 Switch-case statements and Run-time storage management

Systems I. Machine-Level Programming V: Procedures

A Fast Review of C Essentials Part II

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

CSE Lecture In Class Example Handout

Programs in memory. The layout of memory is roughly:

22c:111 Programming Language Concepts. Fall Functions

Object-Oriented Programming

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

CS240: Programming in C

Memory Allocation in C

ECE260: Fundamentals of Computer Engineering

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

CSCI 171 Chapter Outlines

Chapter 7: Preprocessing Directives

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables


Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

CSC 2400: Computer Systems. Using the Stack for Function Calls

Scope, Functions, and Storage Management

Lecture 3: C Programm

Procedures and Stacks

Topic 6: A Quick Intro To C

Subprograms, Subroutines, and Functions

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

Announcements. Assignment 2 due next class in CSIL assignment boxes

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

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

Come and join us at WebLyceum

Computer Systems Lecture 9

CS3157: Advanced Programming. Outline

Run-time Environments

EL6483: Brief Overview of C Programming Language

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Run-time Environments

Unit 4 Preprocessor Directives

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

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

Fundamentals of Programming

CIT Week13 Lecture

CS558 Programming Languages

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

ECE251: Tuesday September 11

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

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

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

G Programming Languages - Fall 2012

Programming Tools. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore

Summer May 11, 2010

COMsW Introduction to Computer Programming in C

Conditional Compilation

Call Stacks + On Writing Good Code

CS 161 Intro to CS I. Finish Pointers/Start Recursion

Introduction to Programming (Java) 4/12

x86 assembly CS449 Fall 2017

CS240: Programming in C

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

Course Administration

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

ECE 190 Midterm Exam 3 Spring 2011

An Experience Like No Other. Stack Discipline Aug. 30, 2006

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

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS61C : Machine Structures

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

Chapter 9 :: Subroutines and Control Abstraction

CS 320: Concepts of Programming Languages

Problem Solving in C. Stepwise Refinement. ...but can stop refining at a higher level of abstraction. Same basic constructs. as covered in Chapter 6

Informatica e Sistemi in Tempo Reale

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

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

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

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

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

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

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Run-time Environment

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

Dynamic memory allocation

ECE232: Hardware Organization and Design

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

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

Subroutines. we jump to a new location in the code

CS 314 Principles of Programming Languages. Lecture 11

Transcription:

CS240: Programming in C Lecture 6: Recursive Functions. C Pre-processor. Cristina Nita-Rotaru Lecture 6/ Fall 2013 1

Functions: extern and static Functions can be used before they are declared static for a function means the function is local only to that file extern, means that the function was declared in another file or the same file but later Always put prototype before definition to avoid any problems Cristina Nita-Rotaru Lecture 6/ Fall 2013 2

Variables All variables must be declared before use extern has the same meaning as for functions static the same when declared outside functions static declared within a function has memory, i.e is initialized only the first time the function is called Do not use the same names for global and local variables Cristina Nita-Rotaru Lecture 6/ Fall 2013 3

Static modifier: Example int good_memory(void) { } static int val = 10; printf("val %d\n", val++); int bad_memory(void) { } int val = 10; printf("val %d\n", val++); Cristina Nita-Rotaru Lecture 6/ Fall 2013 4

Passing Parameters In C, parameters are passed to functions BY VALUE Functions create local copies of those variables Modifications are not preserved outside the functions unless the function is passed references to variables int swap(int[]) Cristina Nita-Rotaru Lecture 6/ Fall 2013 5

void swap2(int vec[]) { int tmp; tmp = vec[0]; vec[0] = vec[1]; vec[1] = tmp; } int main() { int vec[2] = {10, 20}; swap2(vec); return 0; } Cristina Nita-Rotaru Lecture 6/ Fall 2013 6

Recursive functions in C A function can call itself Recursive expression of the function Needs a stop condition Example: compute n! int fact(n) { if(n<=1) return 1; } else return n*fact(n-1); Why does it work? It s magic! Cristina Nita-Rotaru Lecture 6/ Fall 2013 7

actually it s all about the Stack The operating system creates a process by assigning memory and other resources" Stack: keeps track of the point to which each active subroutine should return control when it finishes executing; stores variables that are local to functions Heap: dynamic memory for variables that are created with malloc, calloc, realloc and disposed of with free Data: initialized variables including global and static variables, un-initialized variables Code: the program instructions to be executed Virtual Memory Stack Heap Data Code Cristina Nita-Rotaru Lecture 6/ Fall 2013 8

Stack Logically it s a LIFO structure Two operations: push and pop Grows down Operations always happen at the top: push and pop, organized It provides support for recursive functions It stores not only the local variables but also the address of the function that needs to be executed next Cristina Nita-Rotaru Lecture 6/ Fall 2013 9

Example: Linux Process Memory Layout Kernel virtual memory User stack 0xFFFFFFFF 0xC0000000 Shared libraries 0x40000000 Loaded from exec Run time heap Unused 0x08048000 Cristina Nita-Rotaru Lecture 6/ Fall 2013 10

C Program execution PC (program counter or instruction pointer) points to next machine instruction to be executed Procedure call: Prepare parameters Save state (SP (stack pointer) and PC) and allocate on stack local variables Jumps to the beginning of procedure being called Procedure return: Recover state (SP and PC (this is return address)) from stack and adjust stack Execution continues from return address Cristina Nita-Rotaru Lecture 6/ Fall 2013 11

Stack frame Parameters for the procedure Save current PC onto stack (return address) Save current SP value onto stack Allocates stack space for local variables by decrementing SP by appropriate amount SP Parameters Return address Stack Frame Pointer Local variables Stack Growth Cristina Nita-Rotaru Lecture 6/ Fall 2013 12

Example: N! Observation: n! = n*(n-1)! and 1! = 1 int fact(n) { if(n<=1) return 1; else return n*fact(n-1); } Cristina Nita-Rotaru Lecture 6/ Fall 2013 13

Zooming in int factorial(int i) { } Stack bottom if(i<=1) return 1; else return i*factorial(i-1); factorial(3)=? 1. Call factorial(3) Local variables Return address of the caller 3 (argument) 2. Call factorial(2) in factorial(3) Local Variables Return address of factorial(3) 2 (argument) Local Variables Return address of main() 3 (argument) 3. Call factorial(1) in factorial(2) Local variables Return address of factorial(2) 1 (argument) Local Variables Return address of factorial(3) 2 (argument) Local Variables Return address of main() 3 (argument) Cristina Nita-Rotaru Lecture 6/ Fall 2013 14

4. factorial(1) returns 1. The return value is stored in register. Control flow returns to factorial(2) 5. factorial(2) returns 2*1 6. factorial(3) returns 3*2*1 Stack is empty Local Variables Return address of factorial(3) 2 (argument) Local Variables Return address of main() 3 (argument) Local Variables Return address of main() 3 (argument) Cristina Nita-Rotaru Lecture 6/ Fall 2013 15

Exercises Write a recursive implementation to compute the GCD of two numbers What are the advantages of using recursion? Read the two recursive functions from your book and understand in each case what is the recursive relation and what is the stop condition Cristina Nita-Rotaru Lecture 6/ Fall 2013 16

C Pre-processor Additional step before compilation Provides two operations #include #define Cristina Nita-Rotaru Lecture 6/ Fall 2013 17

#include "" starts searching at source program location; <> follows implementation dependent rules; e.g., /usr/include and -I option in gcc specified at compilation time included file is usually a header (.h) file, but can also be a.c file or any other file #include "filename" #include <filename> Cristina Nita-Rotaru Lecture 6/ Fall 2013 18

Example You have implemented a program package with a set of functions for other programmers to call You distribute the implementation of your code as a library You distribute the interface of your code as a header file for users of your code to #include, like <stdio.h> for the standard I/O library of the libc.a C library The.h file contains, say, prototypes of functions that the users will call, and external variables that the users can set to control your program s behavior. Cristina Nita-Rotaru Lecture 6/ Fall 2013 19

Macro substitution scope is from occurrence of #define to corresponding #undef, another #define of the same name, or end of file simple textual substitution, NO LANGUAGE AWARENESS #define name replacement-text #undef name Cristina Nita-Rotaru Lecture 6/ Fall 2013 20

Examples #define STEP 10 #define forever for (;;) #define max(a, B) ((A) > (B)? (A) : (B)) Cristina Nita-Rotaru Lecture 6/ Fall 2013 21

When #defines go wrong What s wrong with #define square(x) x * x How about this #define square(x) ((x) * (x)) Cristina Nita-Rotaru Lecture 6/ Fall 2013 22

Conditional pre-processing #ifdef #ifndef #else #elif #endif Cristina Nita-Rotaru Lecture 6/ Fall 2013 23

Applications of #ifdef: portability #ifdef SYSV #define HDR "sysv.h" #elif defined(bsd) #define HDR "bsd.h" #elif defined(msdos) #define HDR "msdos.h" #else #define HDR "default.h" #endif #include HDR Cristina Nita-Rotaru Lecture 6/ Fall 2013 24

Application of #ifndef: include files To include a include file only once #idndef _MY_INCLUDE_FILE_ #define _MY_INCLUDE_FILE_ header file #endif /* _MY_INCLUDE_FILE_ */ Cristina Nita-Rotaru Lecture 6/ Fall 2013 25

Application of #ifdef: Print debug information #ifdef DEBUG #define DPRINTF(args) printf args #else #define DPRINTF(args) #endif Specify how you want to macro to expand by specifying the DEBUG variable at compilation time in the Makefile gcc -D option Cristina Nita-Rotaru Lecture 6/ Fall 2013 26

Readings for This Lecture K&R Chapter 4 Cristina Nita-Rotaru Lecture 6/ Fall 2013 27