Lecture 3. Variables. Variables

Similar documents
Functions. (transfer of parameters, returned values, recursion, function pointers).

Fundamentals of Programming Session 12

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

CMPE-013/L. Introduction to C Programming

Computers Programming Course 5. Iulian Năstac

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

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

Memory Usage in Programs

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

A Fast Review of C Essentials Part II

DECLARAING AND INITIALIZING POINTERS

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

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

C: How to Program. Week /Mar/05

University of Kelaniya Sri Lanka

Chapter-8 DATA TYPES. Introduction. Variable:

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

Dynamic Memory Allocation

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

Lecture 3: C Programm

C++ (classes) Hwansoo Han

Types, Variables, and Constants

This exam is to be taken by yourself with closed books, closed notes, no calculators.

Fundamental of Programming (C)

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

A Fast Review of C Essentials Part I

POINTER AND ARRAY SUNU WIBIRAMA

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

Chapter 8: Intraprogram Communication. Lecture 8 1

CS240: Programming in C

BLM2031 Structured Programming. Zeyneb KURT

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

12 CREATING NEW TYPES

Computer Programming

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Lecture 02 C FUNDAMENTALS

Programming. Data Structure

C Introduction. Comparison w/ Java, Memory Model, and Pointers

a data type is Types

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

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

Computer Programming Unit 3

Pointers, Dynamic Data, and Reference Types

As stated earlier, the declaration

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

POINTER & REFERENCE VARIABLES

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Data Representation and Storage. Some definitions (in C)

Chapter 2 - Introduction to C Programming

Lecture 2: C Programm

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

ET156 Introduction to C Programming

Use a calculator and c = 2 π r to calculate the circumference of a circle with a radius of 1.0.

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

CSE 5A Final Fall 2006

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

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

M.EC201 Programming language

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

CHAPTER 4 FUNCTIONS. 4.1 Introduction

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

Structured Programming. Functions and Structured Programming. Functions. Variables

Fundamentals of Programming

Unit 7. Functions. Need of User Defined Functions

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Understand Execution of a Program

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Run-time Environments - 2

Principles of C and Memory Management

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

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Introduction to C++ Systems Programming

Dynamic Memory Allocation and Command-line Arguments

CS240: Programming in C

C introduction: part 1

Presented By : Gaurav Juneja

Exam 3 Chapters 7 & 9

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

ANSI C Reserved Words

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Sound Programming with PC Speaker

Worksheet 4 Basic Input functions and Mathematical Operators

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

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C Programming Multiple. Choice

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

Government Polytechnic Muzaffarpur.

Pointers (2) Applications

Programming Languages

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

Variables (2D) Young Won Lim 3/28/18

6.096 Introduction to C++ January (IAP) 2009

Transcription:

Lecture 3 Variables Variables Data processed by programs are input from keyboard by user, are read from the storage medium or are obtained by evaluating expressions. For this purpose it is necessary to use the memory areas in which is recorded data and from which is read these data. This is achieved through so-called variables or objects. Declaration of a variable (object) specifies its type and the name (identifier) with which it will be referred: variable_type variable_name; 1

Exemples: int my_variable; double radius; float * r; unsigned int n; int var=12000; double real=2.5; float var1, var2=1.1, var3; // declaration with initial value // declaration with initial value // multiple declaration Note : The memory address where a variable is allocated can not be specified by the user. The compiler allocate the variable depending on available memory. The user can find the address to which was assigned a variable with operator & applied to the variable name. For example: &my_variable A variable is a named data storage location in your computer's memory A variable has: - name - is an identifier - type - which is established when the variable is defined (e.g. int, float, char, etc.). - value - which can be changed by assigning a new value to the variable Note : Once defined, the type and the location of a variable cannot be changed! 2

Representation of a variable in memory int myvariable = 12000; Attributs of variabiles: type of data - may be fundamental or user-defined type and determine the structure, the range of values, the memory size; storage class - set the memory in which to put the identifier associated information (data segment, register, stack, heap) and define the lifetime of its allocation; domain - the portion of the program where the variable can be referred; it is determined by the position of statement; life time - this is how long the variable is allocated in memory; link - specifies how to associate an identifier with an object or function in the process of editing the links. 3

Position statement determines the existence of two fundamental areas: Domain file (global) Domain block (local) IDs with the domain block are called local and are the result of statements within a block (they range from the declaration and end block) or formal parameters of a function definition (have the function block domain). IDs with the domain file are called global and are declared outside any function (the field is between the declaration and end of file). Memory class auto - specifies a variable automatic and can be used for local variable area (with space allocated on the stack). static - force the static life of variable without changing its domain. These variables are allocated in the data segment, so the allocation is throughout the execution of the program, but can be referred only in the block in which it was declared. register - has the effect of saving variable in a register of the processor and not in memory, resulting in increased speed of execution of the program. These variables are local variables, non-static, type int or char and don t have memory address. extern - indicate external links and provides static allocation for local and global variables or functions. 4

auto - specifies a variable automatic and can be used for local variable area (with space allocated on the stack). or: int a=5; printf("a = %d\n", a); The two examples are the same auto int a=5; printf("a = %d\n", a); static - force the static life of variable without changing its domain. These variables are allocated in the data segment, so the allocation is throughout the execution of the program, but can be referred only in the block in which it was declared. #include <conio.h> void func(void) int a=10; /* local variable - allocated on stack - time life execution of function - domain: function static int b=10; /* local variable - allocated on stack - time life execution of program - domain: function a=a+1; b=b+1; printf("\na=%d, b=%d", a, b); getch(); 5

register - has the effect to save the variable in a register of the processor and not in memory, resulting in increased speed of execution of the program. These variables are local variables, non-static, type int or char and don t have memory address. register int a=5; printf("a = %d\n", a); extern - indicate external links and provides static allocation for local and global variables or functions. // File example_a.cpp /* Example using extern variables and functions #include <conio.h> extern int a; extern void printf("\na=%d", a); getch(); // File example_b.cpp /* Example using extern variables and functions int a=150; void func() printf ("\nis executed func"); printf ("\na=%d", a); 6

Access modifiers Access modifiers that can be used are const and volatile and they can control how change in one variable. const const variables (constants with name) can not be modified during the execution of a program. Instructions trying change const variables generates compile errors. Examples: const int a = 99; a = a+1; a = 5; // declaration of const variable // error, can not change the declared const // error, can not change the declared const volatile Volatile variables can be changed from outside the program (eg some interruptions). Constants can not be modified during the execution of a program. #define MAX 100 const int MIN = -100; int m ; m= (MIN +MAX) / 2; printf("m = %d", m); MAX = 1000; // eroare! MIN = -1000; // eroare! 7