Chapter 8: Intraprogram Communication. Lecture 8 1

Similar documents
CSE 230 Intermediate Programming in C and C++ Functions

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

Chapter 2 Control in Programming. lecture 2 1

Fundamentals of Programming Session 12

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

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

Week 3 Lecture 2. Types Constants and Variables

CS240: Programming in C

M.EC201 Programming language

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

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

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

DECLARAING AND INITIALIZING POINTERS

A Fast Review of C Essentials Part I

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

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

CMPE-013/L. Introduction to C Programming

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

BLM2031 Structured Programming. Zeyneb KURT

Lecture 3. Variables. Variables

EL6483: Brief Overview of C Programming Language

Introduction. C provides two styles of flow control:

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

UNIT III (PART-II) & UNIT IV(PART-I)

C Language, Token, Keywords, Constant, variable

A Fast Review of C Essentials Part II

Midterm Review. Short Answer. Short Answer. Practice material from the Winter 2014 midterm. Will cover some (but not all) of the questions.

Short Notes of CS201

Introduction to Computing Lecture 09: Functions (Part II)

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

CS201 - Introduction to Programming Glossary By

Presented By : Gaurav Juneja

We do not teach programming

Storage class and Scope:

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

Unit 7. Functions. Need of User Defined Functions

Fundamentals of Programming

CSE 230 Intermediate Programming in C and C++

QUIZ. Source:

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

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Introduction. This project will focus primarily on processes.

Student Name: (in Capital Letters) CSE Introduction to Programming for Engineers and Scientists. Final Exam

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

15 FUNCTIONS IN C 15.1 INTRODUCTION

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

Fundamental Concepts and Definitions

Chapter 2: Overview of C. Problem Solving & Program Design in C

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

V850 Calling Convention


Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

Procedures, Parameters, Values and Variables. Steven R. Bagley

UNIT 3 FUNCTIONS AND ARRAYS

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

Lecture 2: C Programming Basic

CS113: Lecture 4. Topics: Functions. Function Activation Records

The Three Attributes of an Identifier

CS102: Variables and Expressions

Lecture 03 Bits, Bytes and Data Types

Syntax and Variables

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

EECE.2160: ECE Application Programming Fall 2017 Exam 3 December 16, 2017

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

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

ET156 Introduction to C Programming

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Lecture 04 FUNCTIONS AND ARRAYS

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

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

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Variables and Operators 2/20/01 Lecture #

Data Types and Variables in C language

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

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

Functions in C C Programming and Software Tools

Programming, numerics and optimization

5.Coding for 64-Bit Programs

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

C & Data Structures syllabus

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

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

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

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Character Strings. String-copy Example

Introduction to C An overview of the programming language C, syntax, data types and input/output

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Programming and Data Structures

6.096 Introduction to C++ January (IAP) 2009

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Coding Standards for C

Programming in C++ 4. The lexical basis of C++

Chapter 11 Introduction to Programming in C

CSCI 171 Chapter Outlines

2. First Program Stuff

C Programming Review CSC 4320/6320

Transcription:

Chapter 8: Intraprogram Communication Lecture 8 1

Topics Storage Types Global and Local Variables Modules and External Variables Typedefs: Renaming Types Review: Functions Pointers to Functions Lecture 8 2

Storage Types How the memory as a variable is handled during an execution is called its storage type Lecture 8 3

Storage Types The variable properties can be different in depending on : the time the variable exists in memory the location of the variable in memory the location of the variable in the program structure Lecture 8 4

Storage Types The storage type specification comes before the data type in declaration <storage type> <data type> <identifier> Lecture 8 5

Storage Types Two storage types define the life-span of a variable in C automatic static Lecture 8 6

Automatic Storage The default method of data storage is automatic storage The keyword for this type of storage is auto Lecture 8 7

Automatic Storage Automatic Storage means the variable is created and memory allocated for it at the time a function is called. Example auto int IntVar=0; is equivalent to int IntVar=0; Lecture 8 8

Automatic Storage When a variable has automatic storage, the memory and value of that variable ends when the program leaves the code block where it was defined. Lecture 8 9

Automatic Storage The following function is called 4 times. What is the output? void AutoFunction() { } auto int counter=0; counter++; printf("auto %d\n",counter); Auto 1 Auto 1 Auto 1 Auto 1 Lecture 8 10

Static Storage Another method of storing data is static storage The keyword for this type of storage is static Lecture 8 11

Static Storage Static storage means that the variable exists and retains its value for the entire run of the program. Example static int IntVar; or static int IntVar=5; Lecture 8 12

Static Storage Unless otherwise initialized, static variables automatically are set to 0. Static variables retain their value after a function ends, rather than being erased. Lecture 8 13

Static Storage The following function is called 4 times. What is the output? void StaticFunction1() { } static int counter; counter++; printf("static 1 %d\n", counter ); Static 1 1 Static 1 2 Static 1 3 Static 1 4 Lecture 8 14

Static Storage The following function is called 4 times. What is the output? Static 2 7 Static 2 15 Static 2 31 Static 2 63 void StaticFunction2() { static unsigned long double counter=3; counter=2*counter+1; printf("static 2 %g\n",counter); } Lecture 8 15

Storage Types A storage type that defines where a variable is stored is called register storage Lecture 8 16

Register Storage The keyword for this type of storage is register Example: register int LoopVar; or register int LoopVar=0; Lecture 8 17

Register Storage Register storage is supposed to move the variable into a register and allow it a compiler to generate a more efficient program. Lecture 8 18

Register Storage Is not guaranteed in the C language. Useful for variables that are used more than others, such as the loop control for a large array Lecture 8 19

Register Storage The following function is called once. 100% void RegisterFunction() { register int LoopVar; for(loopvar=0;loopvar<100000;loopvar++) { printf("%d%%\r",loopvar/1000); } printf("\n"); } Lecture 8 20

Scope The scope of a variable is defined on where the variable is created. The properties of the variable is altered by where it is declared in the structure of the program. Lecture 8 21

Scope The scope of a variable is where inside the structure of a program a variable can be read, used, and changed. Lecture 8 22

Scope Variables are generally referred to having two types of scope. Local Global Lecture 8 23

Local Variables Local variables are declared inside a code block. Local variables are visible with reference to their code blocks. Lecture 8 24

Global Variables Global Variables are declared outside of all functions and Global variables are declared before they are used inside of functions Lecture 8 25

Global Variables A global variable is visible to all blocks of code A variable declared before all the blocks in a source file can be seen by all the functions of that source file. Lecture 8 26

Global Variables Global variables exist during the entire execution of a program. Global variables initialize to 0 Lecture 8 27

Global Variables Global variables are quick to use. Global variables are easy to declare and get working quickly. Global variables can be seen through out the program. THAT IS THE PROBLEM Global variables can easily cause a side effect. Lecture 8 28

Side Effects The properties that make a global variable easy to use also make it easy to cause an error. Lecture 8 29

Side Effects A line of code in one function forgotten by a programmer can cause a fault that goes throughout the entire program due to the scope of a global variable. Lecture 8 30

Global Properties of Local Variables A local variable can behave as a global variable with respect to a nested code block Lecture 8 31

Global Properties of Local Variables int w=0; void LocalScopes() { { int x=5; int y=10; { int x = 0; int z=15; printf("%d,%d,%d,%d\n",w,x,y,z); x=3;y=6;z=9;w=81; printf("%d,%d,%d,%d\n",w,x,y,z); } printf("%d,%d,%d\n",w,x,y); } printf("%d,%d\n",w,x); } Lecture 8 32

Modules and External Variables A module is a file containing source code in a program that all work together to perform a related task. Lecture 8 33

Modules and External Variables Each source module is compiled first, and then can be linked afterwards. Breaking large programs into modules allow for better organization. Lecture 8 34

Modules and External Variables In order to make functions work with each other in different modules, global variables may need to be used. Also, some functions may also have to be given extra behavior to cross between module files. Lecture 8 35

The special keyword for having a variable go across a module boundary is extern. A global variable extern int globalvar; Would be visible across multiple files Lecture 8 36

Modules and External Variables Typically, the original variable is declared without an extern, and is in a user defined header file, or in the C file with the main. int globalvar; Lecture 8 37

Modules and External Variables To block this ability to jump between modules, the variable can be declared static. static int globalvar; Lecture 8 38

Typedef A typedef is a special storage type, like static or register. Typedef is used to associate an identifier with a known data type. Typedef does not allocate storage, it just renames a data type. Lecture 8 39

Typedef Portability: Moving code from one machine to another may require a specific type of data field. The short type on one machine may be equal to the int type on another Lecture 8 40

Typedef Readability: There are a limitless number of combinations of ints and floats and other values. Giving a different name is easier to recognize or remember. typedef long big_int; typedef struct { float radius; foat center; } CIRCLE; Lecture 8 41

Function Review Function Definition: A Function definition is the main header of the function and its code block type identifier (int param 1, int param 2) { } /*Code here*/ Lecture 8 42

Function Review Function Types-The return type of a function is the function type. In communicating between pieces of a program, the return type can be used for communication between functions. Lecture 8 43

Function Review Function Declaration The function declaration is the prototype of a function. return_type identifier(type1,type2); Lecture 8 44

Function Review Function Call To call a function is to use the function name with its parameter list, and then to get its return type if necessary. Lecture 8 45

Function Review data=function_1(); data=function_2(paramter); function_3(); function_4(paramater); Lecture 8 46

Function Pointer Functions are identifiers in C Like all such routines, it is possible to have a pointer to a function just as a pointer to a variable. the notation for such a pointer is return type (* function_pointer_identifier) (parameter list) Lecture 8 47

Function Pointers Function pointers can be useful Combining different routines to achieve efficiency Doing a set of commands in different orders Lecture 8 48

Function Pointer int PtrTest1(int B) { int result=5; int LoopControl=0; int LoopControl2=0; printf("pointer Test 1\n"); for(loopcontrol=0;loopcontrol<b;loopcontrol++) { for(loopcontrol2=0;loopcontrol2<loopcontrol;loopcontrol2++) { printf("*"); } printf("\n"); } printf("\n"); return result; } Lecture 8 49

Function Pointer int PtrTest2(int C) { int result=0; printf("pointer Test 2\n"); printf("the parameter is %d\n",c); printf("\n"); return result; } Lecture 8 50

Function Pointer void FunctionPointerTest() { int (*fnptr)(int)=null; } fnptr=ptrtest1; fnptr(7); fnptr=ptrtest2; fnptr(7); Lecture 8 51

Function Pointer Pointer Test 1 * ** *** **** ***** ****** Pointer Test 2 The parameter is 7 Lecture 8 52