Fundamentals of Programming Session 12

Similar documents
Fundamentals of Programming Session 13

Fundamentals of Programming Session 4

Fundamentals of Programming Session 15

Fundamentals of Programming Session 19

Fundamentals of Programming Session 8

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

Fundamentals of Programming Session 20

Fundamentals of Programming Session 25

Fundamentals of Programming Session 7

Fundamentals of Programming Session 20

Fundamentals of Programming Session 24

Function Call Stack and Activation Records

Fundamentals of Programming Session 23

A Fast Review of C Essentials Part II

Fundamentals of Programming Session 19

Fundamentals of Programming Session 9

Fundamentals of Programming Session 14

University of Kelaniya Sri Lanka

Introduction to Programming session 24

The Three Attributes of an Identifier

Unit 7. Functions. Need of User Defined Functions

Introduction to Programming

Fundamentals of Programming Session 24

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

C: How to Program. Week /Mar/05

Introduction to Programming

Fundamentals of Programming Session 2

Functions and Recursion

C Functions. Object created and destroyed within its block auto: default for local variables

CSE 230 Intermediate Programming in C and C++ Functions

15 FUNCTIONS IN C 15.1 INTRODUCTION

Functions. Systems Programming Concepts

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

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

Introduction to Programming

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Lecture 3. Variables. Variables

C Programming Language

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

CS240: Programming in C

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

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

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

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

Chapter 2 - Introduction to C Programming

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

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

Lecture 04 FUNCTIONS AND ARRAYS

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

C: How to Program. Week /Apr/16

DECLARAING AND INITIALIZING POINTERS

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

Today's Topics. CISC 458 Winter J.R. Cordy

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

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

CE221 Programming in C++ Part 1 Introduction

CS240: Programming in C

Iterative Languages. Scoping

Chapter 7 Functions. Now consider a more advanced example:

Full file at C How to Program, 6/e Multiple Choice Test Bank

Class object initialization block destructor Class object

BLM2031 Structured Programming. Zeyneb KURT

Chapter 8: Intraprogram Communication. Lecture 8 1

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

Variables and Operators 2/20/01 Lecture #

Fundamentals of Programming Session 1

Programming for Engineers Introduction to C

CSCI 2132 Software Development. Lecture 20: Program Organization

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Functions. Functions. Identify Repeated Code. Identify Repeated Code. Identify Similar Code. Use Parameters to Customize 2/25/14

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

Chapter 2, Part I Introduction to C Programming

Programming Languages

Memory Allocation in C

Welcome to... CS113: Introduction to C

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

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Today s class. Finish review of C Process description and control. Informationsteknologi. Tuesday, September 18, 2007

PROGRAMMAZIONE I A.A. 2017/2018

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Functions and Recursion

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Work relative to other classes

Fundamentals of Programming Session 1

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

M.EC201 Programming language

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

CSE123. Program Design and Modular Programming Functions 1-1

User Defined Functions

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

PROGRAMMAZIONE I A.A. 2017/2018

Lecture 04 FUNCTIONS AND ARRAYS

Lab 3. Pointers Programming Lab (Using C) XU Silei

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Functions. Arash Rafiey. September 26, 2017

Chapter 5 C Functions

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

Transcription:

Fundamentals of Programming Session 12 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology

Outlines Storage Classes 2

Storage Classes 3 The attributes of variables include name, type, size and value. Actually, each identifier in a program has other attributes, including storage class, storage duration, scope and linkage. C provides four storage classes, indicated by the storage class specifiers: auto, register, extern and static. An identifier s storage class determines its storage duration, scope and linkage. An identifier s storage duration is the period during which the identifier exists in memory. An identifier s scope is where the identifier can be referenced in a program. Some can be referenced throughout a program, others from only portions of a program.

The four storage-class specifiers can be split into two storage durations: automatic storage duration and static storage duration. Keywords auto and register are used to declare variables of automatic storage duration. Variables with automatic storage duration are created when the block in which they re defined is entered; they exist while the block is active, and they re destroyed when the block is exited. A function s local variables (those declared in the parameter list or function body) normally have automatic storage duration. 4 Keyword auto explicitly declares variables of automatic storage duration.

5 For example, the following declaration indicates that double variables x and y are automatic local variables and they exist only in the body of the function in which the declaration appears: auto double x, y; For the remainder of the text, we ll refer to variables with automatic storage duration simply as automatic variables. Data in the machine-language version of a program is normally loaded into registers for calculations and other processing.

The compiler may ignore register declarations. For example, there may not be a sufficient number of registers available for the compiler to use. The following declaration suggests that the integer variable counter be placed in one of the computer s registers and initialized to 1: register int counter = 1; Keyword register can be used only with variables of automatic storage duration. 6

Keywords extern and static are used in the declarations of identifiers for variables and functions of static storage duration. Identifiers of static storage duration exist from the time at which the program begins execution. For static variables, storage is allocated and initialized once, when the program begins execution. For functions, the name of the function exists when the program begins execution. 7

8 However, even though the variables and the function names exist from the start of program execution, this does not mean that these identifiers can be accessed throughout the program. Storage duration and scope (where a name can be used) are separate issues. There are two types of identifiers with static storage duration: external identifiers (such as global variables and function names) and local variables declared with the storage-class specifier static. Global variables and function names are of storage class extern by default.

Global variables are created by placing variable declarations outside any function definition, and they retain their values throughout the execution of the program. Global variables and functions can be referenced by any function that follows their declarations or definitions in the file. Local variables declared with the keyword static are still known only in the function in which they re defined, but unlike automatic variables, static local variables retain their value when the function is exited. 9

The next time the function is called, the static local variable contains the value it had when the function last exited. The following statement declares local variable count to be static and to be initialized to 1. static int count = 1; All numeric variables of static storage duration are initialized to zero if you do not explicitly initialize them. 10

Question 1 What is the output of the following C code? #include <stdio.h> int f1 (int a, int b ){ int counter =1; while (counter < b) { a*=a; counter ++; } return a;} int main (void ){ int x=4, y =3; printf ("%d\n", f1 (x, y)); printf ("%d", x );} 11 Answer 256 4

Question 2 What is the output of the following C code? int f ( int x ) { printf ( " %d", x ); x++; return x; } int main ( ) { int x = 0 ; f( x ) ; x++; f( x ) ; printf ( " %d", f(x) ); return 0; } 12 Answer 0112

13 Question 3 What is the output of the following C code? void test(int b) { int main() { b = b < 10? b : 2 * b; } int b = 10; test(b); printf ("%d\n", b); if ( (b == 20) && (b = 15)) printf ("b\n"); printf ("%d\n", b); return 0; } Answer 10 10