Programming In C++ Spring Semester 2013 Lecture 12 Programming In C++, Lecture 12 By Umer Rana.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Introduction to C Programming
Storage class in C Topics Automatic variables External variables
Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By Umer Rana.
Spring Semester 2013 Lecture 5
Bit Field.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
True or false A variable of type char can hold the value 301. ( F )
Structure of a C program
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Storage Classes.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
C++ for Engineers and Scientists Third Edition
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
C Tokens Identifiers Keywords Constants Operators Special symbols.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
Data Types. Data types Data type tells the type of data, that you are going to store in memory. It gives the information to compiler that how much memory.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Learners Support Publications Classes and Objects.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
A First Book of ANSI C Fourth Edition
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
LESSON 06.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Functions and an Introduction to Recursion
C Functions -Continue…-.
Tokens in C Keywords Identifiers Constants
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Storage class in C Topics Automatic variables External variables
Introduction to C Programming Language
Type Conversion, Constants, and the String Object
CMSC 104, Section 4 Richard Chang
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Lectures on Numerical Methods
Classes and Objects.
C Storage classes.
Storage class.
Scope Rules Of Variables
Submitted By : Veenu Saini Lecturer (IT)
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
STORAGE CLASS.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
C Programming Lecture-17 Storage Classes
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Programming In C++ Spring Semester 2013 Lecture 12 Programming In C++, Lecture 12 By Umer Rana

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Storage Classes Every C variable holds a characteristic called its Storage Class. The Storage class define two characteristics of variable: Lifetime Visibility Storage Class help us to write programs that use memory more efficiently, run faster and less prone to programming errors. Correct use of storage class is especially important in large programs.

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Lifetime The lifetime of a variable is the length of time it retains a particular value. In terms of their lifetime variables can be divided into two categories. 1.Automatic 2.Static & External

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Lifetime Automatic Automatic variables are written inside the braces that serve as delimiters for a function. They are created when the function containing them is called as destroyed when the function is terminates. Fun() { int delta; auto int beta; register int gamms; }

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Lifetime Static Variables: A static variable is known only to the function in which it is defined but unlike automatic variables, it does not disappear when the function terminates. Instead it keeps its place in memory and therefore its value. Fun() { static int delta; }

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Lifetime External Variables: Like static variables external variables exist for the life of the program. Variable of type Static & External exist for the life of a program. int zeta; main () { } Fun() { }

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Visibility: Visibility of a variable refers to which parts of a program will be able to recognize it. Could be in block, a function, a file, a group of files or an entire program. Automatic & Static Variables Visibility: An automatic variable is only recognized within the function in which it is define therefore it is some time called local variables. Static variables are also visible only in the function where they are defined.

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Visibility: External Variables Visibility: To Create a variable that is visible to more then one function must be declare externally out side of any function. int zeta; main () { } Fun() { }

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Blocks: A Block is a section of code set off by braces. Visibility of automatic and static variables can be restricted inside a block. main() { int Var1; { int Var2; }

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Register Variables Register variables are stored in the microprocessor’s registers instead of storing it in memory. Register can be accessed much faster than memory locations. Register class variables that are frequently accessed can result in significantly increasing a program’s speed. Drawbacks use of register variables, only few variables can be register variables in given program. Because it all depends on how many registers the program needs for other purposes.

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Enumerated Data Type The enumerated (enum) type give us the opportunity to invent our own data type and specify what values it can take on. Enum birds { sparrow, robin, eagle, egret }; enum birds var1,var2;

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Enumerated Data Type The compiler treats enumerated variables as integers. Each value on the list of permissible values corresponds to an integer. Starting with 0 to onward. thus sparrow is stored as 0, robin as 1, eagle as 2 and egret as 3.

Enumerated Data Type Programming In C++, Lecture 12 By Umer Rana int main () { enum departments{ management, research, clerical, sales }; struct{ char name[30]; float salary; enum departments dept; }employees; strcpy(employees.name,"David Ben"); employees.salary= ; employees.dept=sales; printf("Name: %s \n",employees.name); printf("Salary: %.2f \n",employees.salary); printf("Department: %d \n",employees.dept); }

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Type Conversion: When data type are mixed in an expression the compiler converts the variables to compatible types before carrying out the intended operation. In these conversions variables of lower rank are converted to the rank of the higher-ranking operand. The ranking corresponds roughly to how many bytes each type occupies in memory.

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Type Casting: Typecasting provides a way to force a variable to be of a particular type. This overrides the normal type conversions we just described. The mechanism for type casting is to precede the variable by the desired type. e.g int ans; float Var1=100.50; ans= (int) Var1;

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Labels & goto Statement. A goto statement can cause program control to end up almost anywhere in the program. Label are used only with goto. The goto must specify a label and the label must exist. The Label must be followed by a colon, can be on separate line or on the same line as another statement.

Advanced Variables Programming In C++, Lecture 12 By Umer Rana Labels & goto Statement. int main () { int num; LabelP2:; printf("plese enter the lucky number:"); scanf("%d",&num); if (num==1) goto P1; else goto P2; Label P1:printf(" \n\n\n************ Good Luck ************"); getch(); }