C programming Language Chapter 1 Variables and Statements

C programming Language Chapter 1: Variables and Statements 2/4/2022 Copyright Meir Kalech 1

Binary World § Describing the world only with 0 and 1: § open – 1 close – 0 § successful – 1 failure – 0 § true – 1 false – 0 n n This representation is called a “bit”. We can’t describe the door’s color or the course number with 0/1. Do you have any new idea? 2/4/2022 Copyright Meir Kalech 2

Binary World Great!!! n n Join two bits together. In two bits we can represent four states: n n n 00 01 10 11 0 1 2 3 Now we can count 4 colors, or 4 numbers. But the above question still remains. 2/4/2022 Copyright Meir Kalech 3

Binary World JOINING AGAIN? ! n n Join eight bits together, and call them “byte”. In eight bits we can represent 256 states (28): n n n 00000001 … 11111110 1111 0 1 254 255 But for describing the number of ants at my home, this byte is not long enough. 2/4/2022 Copyright Meir Kalech 4

Binary World YES, JOINING AGAIN! n n But now join two bytes (not bits) together. Our world contains now 216 states, and if it is still not sufficient, add two more bytes, and then four etc… But how can we represent negative numbers or floating point numbers? 2/4/2022 Copyright Meir Kalech 5

Negative numbers n Negative numbers: n n Each number could be formed in one of the following modes: n n n save the most significant bit (msb) for the sign of the number, and the rest represents the number itself. Normal binary mode. 2’s complement mode. In 2’s complement mode: if the msb is 1 then the number is negative if the msb is 0 then the number is positive n Does an example is necessary? ? ? Next slide… 2/4/2022 Copyright Meir Kalech 6

Negative numbers n The following byte contains a certain number. Which number is it ? . . . 1 0 0 1 0 1 n Two options: n n n If the number is kept in the normal binary mode, it is 133. If the number is kept in the 2's complement mode, it is 123. How do I know? 2/4/2022 Copyright Meir Kalech 7

Negative numbers n Let sum the bits from msb to lsb in the following form: n n 1*27+0*26+0*25+0*24+0*23+1*22+0*21+1*20 = 133 If the number is kept in the 2's complement mode: 1 0 0 1 0 1 not + 2/4/2022 0 1 1 0 1 1 Copyright Meir Kalech (123) 8

Negative numbers n n n What is the scope of each of the above modes? Normal binary mode: 0000 to 1111 ((20 -1) – (28 -1)) 2's complement mode: There is not a difference between positive 0 or negative 0. So the scope is: 0000 to 01111111 ((20 -1) – (27 -1)) and 10000000 to 1111(-(20) – -(27)) 2/4/2022 Copyright Meir Kalech 9

Floating Point Number n Floating point representation involves splitting the number into 4 parts: n n n The The sign of the number. mantissa ( 0 < mantissa < 1 ). sign of the exponent. Examples: n 23. 456: The number’s sign is + The mantissa is 0. 23456 The exponent’s sign is + The exponent is 2 2/4/2022 -0. 00078: The number’s sign is The mantissa is 0. 78 The exponent’s sign is The exponent is 3 Copyright Meir Kalech 10

Floating Point Number n n The figure below describes the general principle of floating point representation. Details may be changed between operating systems. One way to represent is: 1 bit sign exponent 23 bits 1 bit mantissa 7 bits sign In this case 32 bits (4 bytes) are used. 2/4/2022 Copyright Meir Kalech 11

Programmer’s Responsibility n n n It is the programmer’s responsibility to know how many bytes (s)he needs. It is the programmer’s responsibility to know which data type (s)he needs. Examples: n n If the program should gets a student’s grade as input from the user, the programmer needs positive number between 0 to 100. One byte should be well enough. If the program should gets a customer account status from the user, the programmer needs positive or negative floating point number between -1025308 to 250. 4 bytes should be enough. 2/4/2022 Copyright Meir Kalech 12

How to request bytes? n n How could we define the requested number of bytes and type? The compiler uses key words. Each key word has a role in the compiler environment. The compiler has key words for pre-defined types. 2/4/2022 Copyright Meir Kalech 13

types unsigned char unsigned short int short unsigned int unsigned long float double long double 2/4/2022 1 byte used for characters 2 bytes used for short positive integers 2 bytes used for short integers 4 (2) bytes used for positive integers 4 (2) bytes used for integers 4 bytes used for positive integers 4 bytes used for floating point 8 bytes used for long floating point 10 bytes used for long floating point Copyright Meir Kalech 14

Characters n n char and unsigned char are assigned to represent characters. Each character has two representations: n n n Binary. ASCII. The binary representation is a binary number in 8 bits. The ASCII representation is the “figure” of the character. There is an ASCII table that maps between the representations. 2/4/2022 Copyright Meir Kalech 15

Characters n For instance: n The character ‘a’: • Binary: 01100001 • ASCII: ‘a’ n (97) What is the difference between the number 3 and the character ‘ 3’? n The number 3 is: • Binary: 00000011 • ASCII: end of text (ETX) n The character ‘ 3’ is: • Binary: 0011 (51) • ASCII: ‘ 3’ 2/4/2022 Copyright Meir Kalech 16

Variable n n Variable is an area for storing data. Each variable has: n n name type address value Name type and address are not allowed to change. 100 150 ‘a’ char color Variable definition: For instance: 98587 int catalog_number type variable_name; int grade; If the value hasn’t been defined, its value is garbage. 2/4/2022 Copyright Meir Kalech 17

Memory n n Memory is an area in the computer, which is used for variables storing. The compiler is responsible to prepare the execution file and to inform it how many bytes are necessary for the execution. The number of bytes is derived from the variables, which are defined in the program. Conclusion: variable definition = memory area. 2/4/2022 Copyright Meir Kalech 18

#include n n n C++ is a portability and reusability language. Portability - The language is not tied with its environment. Reusability - The language enables reuse code. The precompiling command “#include” enables including utilities files (build in) and user defined files into the program. In this way the programmer can use utilities, which were prepared by other programmers. 2/4/2022 Copyright Meir Kalech 19

Input/Output n n n C supplies functions that responsible to the input and output. scanf for input and printf for output. stdio. h library should be included in order to use i/o functions. Syntax: n scanf(“% identifier of a variable”, & variable name); n printf(“% identifier of a variable”, variable name); Example: #include <stdio. h> int x; char z; float y; scanf(“%d %f %c”, &x, &y, &z); printf(“x=%d y=%f z=%c“, x. y. z); 2/4/2022 Copyright Meir Kalech 20

Operators n n In addition to types, the compiler defines operators. There are certain classes of operators: n n n Assignment operator Arithmetic operators Increment and decrement operators Relational operators Logical operators 2/4/2022 Copyright Meir Kalech 21

Assignment Operator n n n This operator assigns a value to a variable. Do you like mathematics? If not, CONGRATULATION!!! The meaning of assignment operator in c++ is definitely different from its meaning in mathematics!!! For instance: n n n int x = 5; // assigning 5 into the variable x x = x+2; // assigning the old value of x (5) + 2 into x. Its new value is 7 Be careful of overflow (rvalue is over the range of lvalue) and of underflow (rvalue is under the range of lvalue). 2/4/2022 Copyright Meir Kalech 22

Arithmetic Operator § § § + * / % n The precedence of the *, / and % operators, is higher than that of + and -. The statement x = x + 2; may be written in the form x += 2; n n plus (unary & binary) minus (unary & binary) multiplication division remainder of division The syntax of this assignment operation is: • operand 2 operator= operand 2 n Which is identical to • operand = operand 1 operator operand 2 2/4/2022 Copyright Meir Kalech 23

Expression n n Expression is a combination of operators and operands. Operand may be a variable or constant. The simplest expression is operand alone. For instance: n n n 2+3 6 7>1 A = 8*(4>5) 5 2/4/2022 Copyright Meir Kalech 24

Expression n Each expression has: n n n Type value Address Type and value are derived from the operands’ type and value. For instance: n n n 5: type is int, because all constant integer are int, and value is 5. 3. 2: type is double, because all constant floating point are double, and value is 3. 2 3+2: type is int because 3 is int and 2 is int, the value is 5. 2/4/2022 Copyright Meir Kalech 25

Expression n n And what about 3+2. 4? 3 is int and 2. 4 is double. What is the type of an expression of int+double? RULE: the operands should be the same types. WHAT? ? ? My grade in “introduction to computing” is 60 and my exercise grade is 59. 5. Should I attend the course again? First, we should understand what is conversion. If you understand, you will not take the course again. But if not… 2/4/2022 Copyright Meir Kalech 26

Conversion n n Two types of conversions: Implicit Conversion: Invoked automatically by the compiler when expression mix different types. Explicit Conversion (Casting): Invoked by the programmer. Conversion can result in: n n n Promotion: The type is converted to a “higher” type. Demotion: The type is converted to a “lower” type, which may lead to trouble. New temporal variable is created during the conversion process, which is identical to the original variable except of its type. 2/4/2022 Copyright Meir Kalech 27

Conversion n Comment: n n n The original variable hasn’t been changed. The temporal variable is dead in the end of the statement. Example for implicit conversion: double d = 12. 5; int a = 3, b = 4; a = b + d; n n n Promotion to make addition possible n b int double Addition of double types n b + d Demotion upon assignment n b + d double int (CAUTION !) 2/4/2022 Copyright Meir Kalech 28

Conversion n Example for explicit conversion: int a=7, b = 4; int result; result = (float)a / b; n n n Promotion to make division possible (explicit) n a int float Promotion to make division possible (implicit) n b int float Demotion upon assignment n a / b float int (CAUTION !) 2/4/2022 Copyright Meir Kalech 29

Increment and Decrement n n C provides two unusual unary operators for incrementing and decrementing variables by one (1). Prefix Increment and Decrement Operators: ++, -unary-expression : ++ unary-expression -- unary-expression n Postfix Increment and Decrement Operators: ++, -postfix-expression : postfix-expression ++ postfix-expression -- 2/4/2022 Copyright Meir Kalech 30

Increment and Decrement n n n prefix operator: the operand is incremented or decremented and its new value is the result of the expression. postfix operator: increment or decrement operation is the value of the postfix-expression before the increment or decrement operator is applied. Examples: int x=5, y; 1. y=x++; 2. y=++x; 2/4/2022 //y=5, x=6 //y=x=6 Copyright Meir Kalech 31

Relational Operators n n n n == Equal != Different > Greater than < Less than >= Greater or equal than <= Less or equal than == and != have a lower precedence over others, and all of them have lower precedence than arithmetic operators. If the relation is: n n true - expression returns 1. false - the relation returns 0. 2/4/2022 Copyright Meir Kalech 32

Relational Operators n Examples: int x=2; (x>5)+1; • The value of the expression x>5 is false (0) • The value of whole expression is 1 (0+1) int a=2, b; ((b=2) == a) return true. 2/4/2022 Copyright Meir Kalech 33

Logical Operators n n Expressions which forms more complex expressions. Let a, b expressions a b a||b a&& !a b a||b a or b T T F a&&b a and b T F F !a n n not a F T T F F F F T If the first (left) operand of a equal to 0, the second (right) If the first (left) operand of a equal to 1, the second (right) 2/4/2022 logical-AND operation is operand is not evaluated. logical-OR operation is operand is not evaluated. Copyright Meir Kalech 34

Logical Operators n Example: int a=5, b=3, c=0; n Which of the following expressions are TRUE? Which are FALSE? n Expression Evaluated as Intermediate Value (T or F) a>b || c>b 5>3 ||0>3 T||F T a>b && c>b a!=2 || !c b<a || b<c && a<c (b<a || b<c) && a<c a || b && c b<=a && (c=1) 2/4/2022 Copyright Meir Kalech 35
- Slides: 35