C Programming Language

Similar documents
BSM540 Basics of C Language

C Programming Language

BSM550 C Programming Language

Function Call Stack and Activation Records

Fundamentals of Programming Session 12

BSM540 Basics of C Language

Lecture 04 FUNCTIONS AND ARRAYS

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

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

BSM540 Basics of C Language

CS240: Programming in C

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

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

Self-referential Structures and Linked List. Programming and Data Structure 1

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

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

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

Iterative Languages. Scoping

Computer Programming Unit 3

Fundamental of Programming (C)

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

Lecture 04 FUNCTIONS AND ARRAYS

C: How to Program. Week /Apr/16

Memory Allocation in C

The Three Attributes of an Identifier

Chapter 7 Functions. Now consider a more advanced example:

A Fast Review of C Essentials Part II

Functions. Systems Programming Concepts

C Programming Language

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

C Programming Review CSC 4320/6320

C: How to Program. Week /Apr/23

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

Unit 7. Functions. Need of User Defined Functions

AMCAT Automata Coding Sample Questions And Answers

IV Unit Second Part STRUCTURES

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

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

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

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

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

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

University of Kelaniya Sri Lanka

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

Variation of Pointers


COP 3223 Introduction to Programming with C - Study Union - Spring 2018

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Subject: Fundamental of Computer Programming 2068

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

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

Functions and Recursion

Programming Languages

Fundamentals of Programming & Procedural Programming

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

CSE 230 Intermediate Programming in C and C++ Functions

Content. In this chapter, you will learn:

Dynamic memory allocation

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

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

Computer Systems Lecture 9

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

PROGRAMMAZIONE I A.A. 2018/2019

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

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

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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

M.EC201 Programming language

int marks[10]; // fixed size and fixed address No change in Memory address.

Lecture 9 - C Functions

15 FUNCTIONS IN C 15.1 INTRODUCTION

Dynamic Memory Allocation

Contents of Lecture 3

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

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

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

Functions BCA-105. Few Facts About Functions:

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

CS240: Programming in C

Introduction to C Final Review Chapters 1-6 & 13

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

C Programming Language

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

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

First of all, it is a variable, just like other variables you studied

Lecture 3. More About C

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

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

Programming Language B

Chapter 5 C Functions

Prepared by: Shraddha Modi

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

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

M.CS201 Programming language

Transcription:

Department of Electrical, Electronics, and Communication Engineering C Programming Language Storage Classes, Linkage, and Memory Management Manar Mohaisen Office: F208 Email: manar.subhi@kut.ac.kr Department of EECE

Review of the Precedent Lecture Explained how to handle files in C Department of Electrical, Electronics, and Communication Engineering

Department of Electrical, Electronics, and Communication Engineering Class Objectives Explain Variable Scopes, Storage Classes, and Linkage Explain Memory Allocation Using malloc() Explain Memory Allocation Using calloc() Explain Memory Release Using free() Discussions

Department of Electrical, Electronics, and Communication Engineering Scopes Scope Region(s) of a program that can access the variable. Scopes Block scope Region between opening brace and matching closing brace Function prototype scope Applies to variable names used in function prototype, Example: int dummy(int size); File scope Can be accessed by all functions in the file (or several files).

Department of Electrical, Electronics, and Communication Engineering Scopes contd. Block Scope sum and i: their scope is all the function q: it has the scope indicated below. double dummy(int a) double sum; int i; for(i = 0; i < 10; i++) double q= i * a; // starts of scope for q // end of scope of q

Department of Electrical, Electronics, and Communication Engineering Scopes contd. File Scope # include <stdio.h> int length = 10; // a variable with file scope void main() double dummy(int a) double sum; int i; for(i = 0; i < length; i++) double q= i * a; // starts of scope for q // end of scope of q

Department of Electrical, Electronics, and Communication Engineering Linkage Linkage Variable with block or prototype scope don t have linkage. Variable with file scope have either internal or external linkage Internal Linkage The variable can be used anywhere inside a single file. External Linkage The variable can be used in a multifile program. int gains = 7; // external linkage static int size = 5; // internal linkage

Department of Electrical, Electronics, and Communication Engineering Storage Classes Storage Classes [Duration] auto specifier The memory location reserved for the variable is temporary. The auto keyword is rarely used. auto int ii = 0; /* equivalent to (int ii = 0) */ static specifier The variable has permanent duration; it is not deleted when the program exits the scope where the static variable is defined. #include <stdio.h> int add2(int a, int b) static int counter = 1; /* static (permanent) variable */ printf( This is call number %d\n, counter++); return(a + b); void main() int ii = 0, jj = 1; for(ii = 5; ii >=0; ii--, jj++) printf( %d\n, add2(ii, jj)); /* prints 6, six times */

Storage Classes contd. Hiding Variables /* hiding.c -- variables in blocks */ #include <stdio.h> int main() int x = 30; /* original x */ printf("x in outer block: %d\n", x); int x = 77; /* new x, hides first x */ printf("x in inner block: %d\n", x); printf("x in outer block: %d\n", x); while (x++ < 33) /* original x */ int x = 100; /* new x, hides first x */ x++; printf("x in while loop: %d\n", x); printf("x in outer block: %d\n", x); Department of Electrical, Electronics, and Communication Engineering return 0;

Storage Classes contd. register Specifier Department of Electrical, Electronics, and Communication Engineering Variable is saved at the CPU registers. Fast access to the variable. However, register keyword is a suggestion to the processor and a register variable might not be saved in a CPU register. #include <stdio.h> void main() register int ii = 0, jj = 1; for(ii = 5; ii >=0; ii--, jj++) printf( %d\n, (ii + jj)); /* prints 6, six times */ extern Specifier A variable with extern specifier is a reference to a variable with the same name defined at the external level of any of the source files in the program.

Storage Classes contd. extern Specifier contd. Example #include <stdio.h> int ii = 5; Department of Electrical, Electronics, and Communication Engineering int add2(int a, int b) /* ii is redefined, global ii is no more visible */ int ii = (a + b); return(ii); void main() extern int ii; /* reference to the external level ii */ for(ii = 10; ii >=0; ii--) printf( %d\n, add2(ii, ii));

malloc() Function Allocates a block of memory. Department of Electrical, Electronics, and Communication Engineering Allocated Memory: malloc() and free() Input is the number bytes to reserve in memory. No name is assigned to the reserved memory block. Therefore, use a pointer to handle the reserved block. Returns a pointer to the first byte of the memory block. double * ptd; ptd = (double *) malloc(30 * sizeof(double)); ptd pointer holds the address of the first reserved byte. Therefore, ptd can be used to access the elements of the block. ptd[1], ptd[5], and ptd[n] are second, sixth, and n-th elements of the block.

Department of Electrical, Electronics, and Communication Engineering Allocated Memory: malloc() and free() contd. Example Ask the maximum size and then allocates enough space. # include <stdio.h> void main() double * ptd; int ii = 0, size = 0; puts( Enter the maximum number of elements ); scanf( %d, &size); ptd = (double *) malloc(size * sizeof(double)); for(ii = 0; ii < size;ii++) ptd[ii] = rand() % 10;

free() Function Department of Electrical, Electronics, and Communication Engineering Allocated Memory: malloc() and free() contd. Allocated memory (using malloc()) is not deleted automatically. So, the memory used by the program increases with every malloc() use. Use free() to release the reserved memory blocks allocated using malloc(). double * ptd; ptd = (double *) malloc(30 * sizeof(double)); // your code free(ptd);

Allocated Memory: calloc() calloc() Function It differs from malloc() in that Department of Electrical, Electronics, and Communication Engineering It takes two arguments Number of elements Element size (in Byte) It initializes all the elements of the memory block to 0. double * ptd; ptd = (double *) calloc(30, sizeof(double)); // your code free(ptd);

Summary & Discussion Department of Electrical, Electronics, and Communication Engineering Explained Variable Scopes, Storage Classes, and Linkage Explained Memory Allocation Using malloc() Explained Memory Allocation Using calloc() Explained Memory Release Using free()