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