Exercise: Write the output of this program without writing the code.

Similar documents
Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

The C Programming Language course syllabus associate level

Lecture 22: C Programming 4 Embedded Systems

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Chapter 9 Text Files User Defined Data Types User Defined Header Files

C++FA 5.1 PRACTICE MID-TERM EXAM

Sources: On the Web: Slides will be available on:

C Interview Questions

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Chapter 5 Names, Bindings, Type Checking, and Scopes

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Illustration 1: Diagram of program function and data flow

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

C++ Programming Language

PLAN YOUR PERFECT WINTER!

13 Classes & Objects with Constructors/Destructors

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Arrays in Java. Working with Arrays

Stack Allocation. Run-Time Data Structures. Static Structures

arrays C Programming Language - Arrays

Object Oriented Software Design II

Data Structures using OOP C++ Lecture 1

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

10CS35: Data Structures Using C

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

5 Arrays and Pointers

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Stacks. Linear data structures

El Dorado Union High School District Educational Services

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

1 Abstract Data Types Information Hiding

14 Sep Jul 2016 (Weeks 1 - V14)

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

Implementation Aspects of OO-Languages

Computer Science 2nd Year Solved Exercise. Programs 3/4/2013. Aumir Shabbir Pakistan School & College Muscat. Important. Chapter # 3.

The Function Pointer

Chapter 13 Storage classes

14 Sep Jul 2016 (Weeks 1 - V14)

Matrix Multiplication

/* File: blkcopy.c. size_t n

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

LAUREA MAGISTRALE - CURRICULUM IN INTERNATIONAL MANAGEMENT, LEGISLATION AND SOCIETY. 1st TERM (14 SEPT - 27 NOV)

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

MISRA-C:2012 Standards Model Summary for C / C++

Simple C Programs. Goals for this Lecture. Help you learn about:

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Numerical Algorithms Group

Lecture 11 Array of Linked Lists

Ada-95 dla programistów C/C++

SDHNS 3 Hour English Food Handlers Class

14 Sep Jul 2016 (Weeks 1 - V14)

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Java from a C perspective. Plan

Aes<6p. Finding & Accepting Available Jobs. Viewing the Job Details. Learning Center Finding & Accepting Available Jobs Page 1 of 5

Java Virtual Machine Locks

Memory Management Simulation Interactive Lab

Linux Crontab: 15 Awesome Cron Job Examples

A C Test: The 0x10 Best Questions for Would-be Embedded Programmers

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

System Software Prof. Dr. H. Mössenböck

Java Types and Enums. Nathaniel Osgood MIT April 25, 2012

Regression Verification: Status Report

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

Name = array(x,y,...,z) the indexing starts at zero, i.e. Name(0) = x. Programming Excel/VBA Part II (A.Fring)

C++ DATA STRUCTURES. Defining a Structure: Accessing Structure Members:

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

2.3 WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

Object Oriented Software Design II

CMSC 202H. ArrayList, Multidimensional Arrays

C++ Overloading, Constructors, Assignment operator

Training Assessments Assessments NAEP Assessments (selected sample)

Answers to Review Questions Chapter 7

Parameter passing in LISP

Binary storage of graphs and related data

public static void main(string[] args) { System.out.println("hello, world"); } }

Data Structure with C

Part I. Multiple Choice Questions (2 points each):

Chapter 3: Writing C# Expressions

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

There e really is No Place Like Rome to experience great Opera! Tel: to discuss your break to the Eternal City!

Part-time Diploma in InfoComm and Digital Media (Information Systems) Certificate in Information Systems Course Schedule & Timetable

Adding Alignment Support to the C++ Programming Language / Wording

Intel EP80579 Software for Security Applications on Intel QuickAssist Technology Cryptographic API Reference

Overview. Lecture 1: an introduction to CUDA. Hardware view. Hardware view. hardware view software view CUDA programming

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Pemrograman Dasar. Basic Elements Of Java

Java Interview Questions and Answers

This section describes how LabVIEW stores data in memory for controls, indicators, wires, and other objects.

CENTRAL OHIO TECHNICAL COLLEGE COLLEGE CALENDAR* Page 1

An Overview of Java. overview-1

Transcription:

OBJECTIVE: Today, scope rules, storage classes, and dynamic memory allocation will be discussed. Memory Allocation Dimension of an array can be determined in two ways; static or dynamic. Static allocation is done at the beginning of the program, which does not allow to change the dimension of the array. However, dynamic allocation gives a flexibility to adjust the dimension of the array. Since it is a dynamic allocation, the dimension of the array can be assigned everywhere in the program. calloc() and malloc() are used to create memory allocation dynamically in the program. A function call of the form calloc(n,dimsize) allocates contiguous memory for an array of n elements with each element having dimsize bytes. The space is initialized with all bits set to zero. If the call is successful, a pointer of type void that points the base of the array in memory is returned; otherwise, NULL is returned. The malloc() is used in a similar manner, if the call is successful, it returns a pointer of type void that points to the requested space in memory; otherwise, NULL is returned. Unlike calloc(), the function malloc() does not initialize the space in memory that it makes available. Space that has been dynamically allocated with either calloc() or malloc() is deallocated by using free(ptr) where the memory is pointed by ptr. If ptr is NULL, the function has no effect. If ptr is not NULL, it must be the base address of space previously allocated by a call to calloc(), malloc(). Let us consider the below simple program; #include<stdio.h> #include<stdlib.h> main() int *a,*b,n; printf("enter the dimension of array:"); scanf("%d",&n); a=calloc(n,sizeof(int)); b=malloc(n*sizeof(int)); Relation of Multidimensional arrays with Pointers The relation between pointers and multidimensional arrays are valid as well as the one dimensional arrays. Let the matrix A has a dimension 3 5 and B has a dimension 7 9 2. Then, A[i][j] is equivalent (&A[0][0] + 5 i + j), B[i][j][k] is equivalent (&B[0][0][0] + 9 2 i + 2 j + k). Scope Rules: The fundamental idea for scoping is that the identifiers are accessible only within the block in which they are declared. Moreover, they are unknown outside of the boundaries of that block. 1

main() int a=2; printf("a=%d\n",a); int a=5; printf("a=%d\n",a); printf("a+1=%d\n",++a); Exercise: Write the output of this program without writing the code. #include<stdio.h> main() int a=1,b=2,c=3; int b=4; float c=5.2; printf("a= %d, b=%d, c=%f\n",a,b,c); a=b; int c; c=b; printf("a= %d, b=%d, c=%f\n",a,b,c); Storage Classes: Every variable and function in C has two basic property: type and storage class. In C, there exists 4 different storage classless ; automatic, external, register, and static with corresponding keywords are auto, extern,register, and static. Automatic Storage Class The variables defined within the function bodies are automatic by default. The automatic storage class is the most common storage class. This stems from two reasons: the variables are defined in the function body, they exist only when the function is called, and these variables disappear when the function is exited. Therefore, these variables are local variables. Declarations of variables in the functions are implicitly automatic. The keyword auto is used explicitly identify the variable which is a member of automatic storage class. auto float a=1; float b; auto int c; 2

a, b, c are member of automatic storage class. External Storage Class To transmit the variables across the functions and blocks, external variables are used. These variables can be accessed by name by any function. When a variable is declared outside a function, storage is permanently assigned to it, and its storage class is extern. External variables never disappear. Because they exist throughout the execution life of the program. Exercise: What should be the output of this program?. extern int a=2, b=3, c=5; int func() int b,c; b=4; c=5; a=c-b; c++; return(b++); main() printf("func returns: %d\n",func()); Register Storage Class The storage class register tells the compiler that the associated variables should be stored in high-speed memory registers. The storage class register is used to improve the execution speed. When the execution speed is concern, a few variables can be defined in register storage class. register int i; /* also it can be written as register i*/ for(i=0;i<limit;i++)... Static Storage Class Static declarations have two important and distinct uses. One of them is to allow a local variable to retain its previous value when the block is reentered. The second use is in connection with external declaration. 3

Exercise: What should be the output of this program?. int functi(int a) static int c=0; c++; return c+a; main() int result,i=0; while(i<=4) result=functi(i); i++; printf("result is %d\n",result); Enumeration Types: The keyword enum is used to declare enumeration types. It provides a means of naming a finite set, and of declaring identifiers as elements of the set. Consider the below declaration; enum daysun, mon, tue, wed, thu, fri, sat; This creates the user-defined type enum day. The keyword enum is followed by the tag name day. The enumerators are the identifiers sun, mon, tue, wed, thu, fri, sat, which are constants of type int. By default, the first one is 0, and each succeeding one has the next integer value. To declare a variable with a type enum day; enum day d1,d2; main() enum daysun=1,mon,tue,wed,thu,fri,sat; enum day d1,d2; d1=fri; d2=3; printf("%d %d",d1,d2); main() enum daysun=12,mon,tue=25,wed,thu,fri,sat; enum day d1,d2; d1=fri; d2=3; printf("%d %d",d1,d2); 4

#include <stdlib.h> enum daysun,mon,tue,wed,thu,fri,sat; typedef enum day day; day find_next_day(day d) day next_day; switch(d) case sun: next_day=mon; case mon: next_day=tue; case tue: next_day=wed; case wed: next_day=thu; case thu: next_day=fri; case fri: next_day=sat; case sat: next_day=sun; return next_day; main() printf("%d",find_next_day(5)); 5

#include <stdlib.h> enum daysun,mon,tue,wed,thu,fri,sat; typedef enum day day; day find_next_day(day d) day next_day; return ((day)(((int)(d)+1) % 7 )); main() printf("%d",find_next_day(12)); 6