Introduction to Programming SS 2012 Adrian Kacso, Univ. Siegen adriana.dkacsoa@duni-siegena.de Tel.: 0271/740-3966, Office: H-B 8406 Stand: April 25, 2012 Betriebssysteme / verteilte Systeme Introduction to Programming (1) i Introduction to Programming SS 2012 2 Data: Variables, Types, Constants Betriebssysteme / verteilte Systeme Introduction to Programming (1) 36
2 Data: Variables, Types, Constants... Contents Variables Data types Constants Betriebssysteme / verteilte Systeme Introduction to Programming (1) 37 2.1 Variables Reminder: the computer from inside (VERY simplistic) registers registers memory bus CPU ALU disk Betriebssysteme / verteilte Systeme Introduction to Programming (1) 38
2.1 Variables... Variables A variable is (or can be viewed as) a named memory region that can be used to store data of a specific type int myvar Memory Address 100 101 102 103 Each memory cell has the size of one byte The size of the variable depends on its type Betriebssysteme / verteilte Systeme Introduction to Programming (1) 39 2.2 Types Simple data types and their sizes type size int 4 bytes integer number (size depends on the machine!) short int 2 bytes integer number long int 4 bytes integer number float 4 bytes floating point number double 8 bytes floating point number char 1 byte single ASCII character bool 1 byte Boolean value (truth value) Betriebssysteme / verteilte Systeme Introduction to Programming (1) 40
2.2 Types... signed and unsigned integers int is a signed integer:... 3, 2, 1, 0, 1, 2, 3,... unsigned short int is an unsigned integer: 0, 1, 2,..., 65534, 65535 Note: The most significant bit indicates the sign of the integer Two s complement (C2) representation for signed integers: Allows the use of binary arithmetic operations on signed integers, yielding the correct 2 s complement results. Calculation of C2: invert the binary equivalent, and then add one. Betriebssysteme / verteilte Systeme Introduction to Programming (1) 41 2.2 Types... Value ranges type size range unsigned short int 2 bytes 0 to 65,535 short int 2 bytes -32,768 to 32,767 unsigned long int 4 bytes 0 to 4,294,967,295 long int 4 bytes -2,147,483,648 to 2,147,483,647 float 4 bytes 1.2e-38 to 3.4e38 double 8 bytes 2.2e-308 to 1.8e308 char 1 byte 256 (ASCII) character values bool 1 byte true or false Betriebssysteme / verteilte Systeme Introduction to Programming (1) 42
2.2 Types... Caution: Integer arithmetic (e.g., with int or char values) does not check for overflow! Precision of floating point arithmetic is limited! with float or double values, the lack of precision can ruin the results of your program! Don t worry: All you need is keep this in mind and be careful! Betriebssysteme / verteilte Systeme Introduction to Programming (1) 43 2.3 Declarations Declaring (defining) a variable All variables must be declared before they can be used Declaration: Type, followed by the name, followed by ; Example: unsigned long int myage; The name can be any sequence of the following characters: a... z, A... Z, 0... 9, but names must not start with a digit! Remember: C++ is case-sensitive: myage is not the same name as myage Some special words (keywords) are not allowed as variable names, e.g.: int, if, while Betriebssysteme / verteilte Systeme Introduction to Programming (1) 44
2.3 Declarations... Declaring more than one variable at a time unsigned int myage, myweight; // two unsigned ints long area, width, length; // three longs Basic Rule for Variable Names: Use names that make sense! Use names that have sufficient intellectual distance!!! Bad example: unsigned int x1, x2, x3; Good example: unsigned int myage, myweight; Betriebssysteme / verteilte Systeme Introduction to Programming (1) 45 2.3 Declarations... Assignment and initialization Assignment: int Width;... Width = 5; Initialization (= creation with initial assignment): int Width = 5; Before the first assignment, the content of a variable is undefined! always make sure that variables are initialized before you use them! Betriebssysteme / verteilte Systeme Introduction to Programming (1) 46
2.4 Constants Literal constants 42 012 0xFF -23.5e16 true a "this is a string\n" type: int type: int (octal notation) type: int (hexadecimal notation) type: double type: bool type: char type: (we will see later; 6.1) Use literal (numerical) constants only very carefully! Documentation, code maintainance,... Betriebssysteme / verteilte Systeme Introduction to Programming (1) 47 2.4 Constants... Symbolic constants Literal: students = classes * 15; Symbolic: const unsigned short int studentsperclass = 15; students = classes * studentsperclass; the keyword const says that this variable cannot be modified Advantages: prepare for change compiler can type-check operations on constant Betriebssysteme / verteilte Systeme Introduction to Programming (1) 48
2.4 Constants... Enumerated constants (enumerated types) Definition of an enumerated data type: enum Color { RED, GREEN, BLUE, BLACK, WHITE, CYAN, MAGENTA }; Color c = GREEN; Note that Color now is a new type (The enumerated constants get assigned int values. However, this is hardly ever useful) Betriebssysteme / verteilte Systeme Introduction to Programming (1) 49 2.5 Repetition Variables A variable is (or can be viewed as) a named memory region that can be used to store data of a specific type int myvar Memory Address 100 101 102 103 Simple data types: int, short int, long int (signed and unsigned) float, double, char, bool Betriebssysteme / verteilte Systeme Introduction to Programming (1) 50
2.5 Repetition... Declaring variables All variables must be declared before they can be used Declaration: Type, followed by the name, followed by ; names can contain digits and, but must not start with a digit names are case-sensitive; certain names are reserved Example: unsigned long int myage; Declaring more than one variable at a time: unsigned int myage, myweight; // two unsigned ints long area, width, length; // three longs Initialization (= creation with initial assignment): int Width = 5; Betriebssysteme / verteilte Systeme Introduction to Programming (1) 51 2.5 Repetition... Constants Literal constants: 42, 0xFF, -23.5e16, true, a, "this is a string\n",... Symbolic constants: const unsigned short int studentsperclass = 15; Enumerated constants (enumerated types): enum Color { RED, GREEN, BLUE, BLACK, WHITE, CYAN, MAGENTA }; Color c = GREEN; Betriebssysteme / verteilte Systeme Introduction to Programming (1) 52