Presentation is loading. Please wait.

Presentation is loading. Please wait.

ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 1: Types, Operators and Expressions.

Similar presentations


Presentation on theme: "ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 1: Types, Operators and Expressions."— Presentation transcript:

1 ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 1: Types, Operators and Expressions

2 ספטמבר 04Copyright Meir Kalech2 The Binary World  Describes the world using only 0 and 1:  open – 1, close – 0  success – 1, failure – 0  true – 1, false – 0 This dual notation representation is called a “ bit ” (short for “ BInary digiT ” ). We can ’ t describe the door ’ s color or the course number with 0/1.  Do you have any idea how to do that?

3 ספטמבר 04Copyright Meir Kalech3 Great!!! Join two bits together. With two bits we can represent 4 states:  000  011  102  113 Now we can count 4 colors or 4 numbers. But the raised question still remains. The Binary World

4 ספטמבר 04Copyright Meir Kalech4 JOINING AGAIN?! Join eight bits together – call them a “ byte ”. In eight bits we can represent 256 (2 8 ) states:  000000000  000000011  … …  11111110254  11111111255 But for describing the number of (real ) bugs at my home, this byte is not large enough. The Binary World

5 ספטמבר 04Copyright Meir Kalech5 YES, JOIN THEM AGAIN! But now join two bytes (not bits) together. Our world contains now 2 16 states, and if it is still not sufficient, join in two more bytes, and then four, etc … This scheme is OK for positive numbers. But how can we represent negative numbers or floating point numbers? The Binary World

6 ספטמבר 04Copyright Meir Kalech6 Negative numbers: Assign the Most Significant Bit (MSB) as the sign of the number, and use the remaining bits to represent the number itself. Each number could be formed in one of 2 notations: 1.Normal binary notation. 2.2 ’ s complement notation. In 2 ’ s complement notation:  If the MSB is 0 then the number is positive.  If the MSB is 1 then the number is negative. Negative Numbers

7 ספטמבר 04Copyright Meir Kalech7 The following byte represents a certain number. Which number is it? There are 2 options here: 1.If the number is kept in the normal binary notation, it is 133. 2.If the number is kept in the 2's complement notation, it is -123.  How do I know this? Negative Numbers 00011001

8 ספטמבר 04Copyright Meir Kalech8 1.Let sum the bits from the MSB to LSB in the following form: 1*2 7 +0*2 6 +0*2 5 +0*2 4 +0*2 3 +1*2 2 +0*2 1 +1*2 0 = 133 2.If the number is kept in the 2's complement notation, we complement and add 1 to get its complement value: Negative Numbers not 10000101 01111010 +1 01111011(123)

9 ספטמבר 04Copyright Meir Kalech9 What is the scope of each of the above notations? Normal binary notation: 00000000 to 11111111 = ((2 0 -1) – (2 8 -1)) 2's complement notation: There is no difference between positive 0 or negative 0. So the (asymmetric) scope is: 00000000 to 01111111 = ((2 0 -1) – (2 7 -1)) and 10000000 to 11111111 = (-(2 7 ) – -(2 0 )) Negative Numbers

10 ספטמבר 04Copyright Meir Kalech10 Floating-point representation involves composing the number from 4 parts: 1.The sign of the number. 2.The mantissa ( 0 < mantissa < 1 ). 3.The sign of the exponent. 4.The exponent. Examples: 23.456 -0.00078 The number’s sign is + The number’s sign is - The mantissa is 0.23456 The mantissa is 0.78 The exponent’s sign is + The exponent’s sign is - The exponent is 2 The exponent is 3 Floating Point Numbers

11 ספטמבר 04Copyright Meir Kalech11 The figure below describes the general principle of floating-point representation (details might vary between different operating systems). One way to represent floating point is: sign mantissa sign exponent In this (single float) case, 32 bits (4 bytes) are used. Floating Point Numbers 1 bit 23 bits 1 bit 7 bits

12 ספטמבר 04Copyright Meir Kalech12 It ’ s the programmer ’ s responsibility to know: how many bytes are needed for the correct representation of the number. which data type is needed for this representation. Examples: If the program gets a student ’ s grade as input from the user, the programmer needs to represent a positive number between 0 to 100. One byte is enough for this. If the program gets a customer account status from the user, say of values between -1025308 to 250, the programmer can use a floating point number. 4 bytes should be enough. Programmer ’ s Responsibility

13 ספטמבר 04Copyright Meir Kalech13 How could we define to the program the requested number of bytes and data type? The program uses keywords. Each keyword has a role in the program environment. The program has keywords for the pre-defined types. How to request this?

14 ספטמבר 04Copyright Meir Kalech14 unsigned char -1 bytefor characters char -1 bytefor characters unsigned short int - 2 bytes for short positive integers short -2 bytesfor short integers unsigned int -2/4 bytesfor positive integers int -2/4 bytesfor integers unsigned long -4 bytesfor large positive integers long -4 bytesfor large integers float -4 bytesfor floating point double -8 bytes for long floating point long double - 10 bytes for longer floating point Common (C) Types

15 ספטמבר 04Copyright Meir Kalech15 char and unsigned char are set to represent characters. Each character has two representations: 1.Binary 2.ASCII The binary representation is a 8-bit number. The ASCII representation is the “ figure ” of the character. An ASCII table can map between these two representations. Characters

16 ספטמבר 04Copyright Meir Kalech16 For instance: For the lower-case character ‘ a ’ : Binary: 01100001(97) ASCII: ‘ a ’ What is the difference between the number 3 and the character ‘ 3 ’ ? The number 3 is: Binary: 00000011 (3) ASCII: end of text (ETX) (also the ^C character) The character ‘ 3 ’ is: Binary: 00110011(51) ASCII: ‘ 3 ’ Characters

17 ספטמבר 04Copyright Meir Kalech17 A variable defines an area for storing data. Each has:  name  type  address  value  Examples: 100150 char colorint catalog_number Variable definition: type variable_name;  For instance:int grade; Name, type and address are not allowed to change. If value hasn ’ t been defined/assigned, the variable value is garbage. Variables ‘a’‘a’ 9 8 5 8 7

18 ספטמבר 04Copyright Meir Kalech18 Memory is an area in the computer that is used to store variables. The compiler is responsible for preparing an execution file and to inform how many bytes in memory are necessary for the execution of the file. The number of bytes needed is also derived from the variables that are defined in the program. Conclusion: variable definition = needed memory area Memory

19 ספטמבר 04Copyright Meir Kalech19 The C language has portability and reusability characteristics. Portability: the language is not tied down to a certain platform/environment. Reusability: the language enables reuse of code. The pre-processor directive “ #include ” enables including (standard) utility files and user defined files into the program during compilation. This way, the programmer can use utilities that were defined already by other programmers. #include - File Inclusion

20 ספטמבר 04Copyright Meir Kalech20 C supplies functions that are responsible for input/output. scanf is usually used for input and printf for output. Syntax: scanf( “ % format string ”, &variable name); printf( “ % format string ”, variable name); #include library to use the I/O functions. Example: #include int x; float y; char z; scanf(“%d %f %c”, &x, &y, &z); printf(“x=%d y=%f z=%c\n”, x, y, z); Input/Output (I/O)

21 ספטמבר 04Copyright Meir Kalech21 In addition to types, C also defines operators. There are various classes of operators: Assignment operators Arithmetic operators Increment/Decrement operators Relational operators Logical operators Operators

22 ספטמבר 04Copyright Meir Kalech22 Do you like mathematics? If not, CONGRATULATIONS!!! The meaning of the assignment operator in C is definitely different from its meaning in mathematics!!! The assignment operator ‘ = ‘ assigns a given value to a variable. For instance: int x = 5; // assigns 5 into the variable x. x = x+2; /* assigns the current value of x (5) plus 2 into x. Its new value is 7. */ Be careful of:  overflow (rvalue is above the range of lvalue).  underflow (rvalue is under the range of lvalue). Assignment Operators

23 ספטמבר 04Copyright Meir Kalech23 Arithmetic Operators  +plus (unary and binary)  -minus (unary and binary)  *multiplication  /division  %remainder of division (modulus) The precedence of the *, / and % operators, is higher than that of the binary + and - operators. The statement: x = x + 2; can be written in the form x += 2; The syntax of this assignment operation is: operand1 operator= operand2 Which is identical to: operand1 = operand1 operator operand2

24 ספטמבר 04Copyright Meir Kalech24 An expression is a combination of operators and operands. An operand may be a variable or constant. The simplest expression is an operand by itself. For instance: 6 2 + 3 7 > 1 a a = a * (4 > 5) Expressions

25 ספטמבר 04Copyright Meir Kalech25 Each expression has: Type Value Address Type and value of the expression are derived from the operands ’ type and value. For instance:  5 type is int, because the default of constant integers is int; value is 5.  3.2 type is double, because the default of constant floating point numbers is double; value is 3.2.  3+2 type is int because 3 is int and 2 is int; value is 5. Expressions

26 ספטמבר 04Copyright Meir Kalech26 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 in an expression should be of 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 have to take the course again. But if not … Expressions

27 ספטמבר 04Copyright Meir Kalech27 Two types of conversions: 1.Implicit Conversion: Invoked automatically by the compiler when an expression mixes different types. 2.Explicit Conversion (Type Casting): Invoked by the programmer code. Conversion can result in:  Promotion: The type is converted to a “ higher ” type.  Demotion: The type is converted to a “ lower ” type, which may lead to trouble (information loss). A temporal variable is created during the conversion process, that is identical to the original variable to be converted, except its type. Conversions

28 ספטמבר 04Copyright Meir Kalech28 Comments on the conversion process : The original variable does not change. The temporal variable disappears at the statement end. Example of implicit conversion: double d = 12.5; int a = 3, b = 4; a = b + d; Promotion to make this addition possible:  b int  double Addition of the 2 double types:  b + d Demotion upon this assignment:  b + d double  int (CAUTION!) Conversions

29 ספטמבר 04Copyright Meir Kalech29 Example of explicit conversion: int a = 7, b = 4; int result; result = (float) a / b; Promotion to make division possible (explicit)  a int  float Promotion to make division possible (implicit)  b int  float Demotion upon assignment  a / b float  int (CAUTION!) Conversions

30 ספטמבר 04Copyright Meir Kalech30 C provides two (unusual) unary operators for incrementing and decrementing variables by one (1). Prefix Increment/Decrement operators: ++, -- unary-expression: ++ unary-expression -- unary-expression Postfix Increment/Decrement operators: ++, -- postfix-expression: postfix-expression ++ postfix-expression -- Increment/Decrement

31 ספטמבר 04Copyright Meir Kalech31 Prefix operator: the operand is incremented or decremented by 1 and its new value is the value of the expression. Postfix operator: expression value is the value of the postfix-expression before the increment or decrement operator is applied. Examples: int x = 5, y;  y = ++x;// y and x are 6  y = x++;// y is 5, x is 6 Increment/Decrement

32 ספטמבר 04Copyright Meir Kalech32 == Equal != Different (not equal) > Greater than < Less than >= Greater or equal than <= Less or equal than == and != have a lower precedence than the others, and all of them have a lower precedence than the arithmetic operators. If the relation is:  true: the relation returns 1.  false: the relation returns 0. Relational Operators

33 ספטמבר 04Copyright Meir Kalech33 Two examples: int x=2; (x>5)+1; The value of the expression x>5 is false (0). The value of entire expression is 1 (0+1). int a=2, b; ((b = 2) == a); Expression value is true. Relational Operators

34 ספטמבר 04Copyright Meir Kalech34 Operators that can form more complex expressions. Let a and b be expressions: If the first (left) operand of a logical AND operation is equal to 0, the second (right) operand is not evaluated. If the first (left) operand of a logical OR operation is equal to 1, the second (right) operand is not evaluated. Logical Operators a || ba or b a && ba and b !anot a aba || ba && b!a TTTTF TFTFF FTTFT FFFFT

35 ספטמבר 04Copyright Meir Kalech35 Example: int a=5, b=3, c=0; Which of the following expressions are TRUE (T)? Which are FALSE (F)? Logical Operators ExpressionEvaluated asIntermediateValue (T or F) a>b || c>b5>3 || 0>3T || FT 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)


Download ppt "ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 1: Types, Operators and Expressions."

Similar presentations


Ads by Google