35
04 ררררררCopyright Meir Kalech 1 C programming Language Chapter 1 : Types, Operators and Expressions

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

  • View
    234

  • Download
    1

Embed Size (px)

Citation preview

ספטמבר 04 Copyright Meir Kalech 1

C programming Language

Chapter 1 :Types, Operators and Expressions

ספטמבר 04 Copyright Meir Kalech 2

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?

ספטמבר 04 Copyright Meir Kalech 3

Great!!! Join two bits together. With two bits we can represent 4 states:

00 0 01 1 10 2 11 3

Now we can count 4 colors or 4 numbers. But the raised question still remains.

The Binary World

ספטמבר 04 Copyright Meir Kalech 4

JOINING AGAIN?! Join eight bits together – call them a “byte”. In eight bits we can represent 256 (28) states:

00000000 0 00000001 1 … … 11111110 254 1 1111111 255

But for describing the number of (real ) bugs at my home, this byte is not large enough.

The Binary World

ספטמבר 04 Copyright Meir Kalech 5

YES, JOIN THEM AGAIN! But now join two bytes (not bits) together.

Our world contains now 216 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

ספטמבר 04 Copyright Meir Kalech 6

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

ספטמבר 04 Copyright Meir Kalech 7

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

0 0 0 1 1001

ספטמבר 04 Copyright Meir Kalech 8

1. Let sum the bits from the MSB to LSB in the following form: 1*27+0*26+0*25+0*24+0*23+1*22+0*21+1*20 = 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)

ספטמבר 04 Copyright Meir Kalech 9

What is the scope of each of the above notations?

Normal binary notation:00000000 to 11111111 = ((20-1) – (28-1))

2's complement notation: There is no difference between positive 0 or negative

0. So the (asymmetric) scope is: 00000000 to 01111111 = ((20-1) – (27-1))

and 10000000 to 11111111 = (-(27) – -(20))

Negative Numbers

ספטמבר 04 Copyright Meir Kalech 10

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.78The exponent’s sign is + The exponent’s sign is -The exponent is 2 The exponent is 3

Floating Point Numbers

ספטמבר 04 Copyright Meir Kalech 11

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

ספטמבר 04 Copyright Meir Kalech 12

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

ספטמבר 04 Copyright Meir Kalech 13

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?

ספטמבר 04 Copyright Meir Kalech 14

unsigned char - 1 byte for characterschar - 1 byte for charactersunsigned short int - 2 bytes for short positive integersshort - 2 bytes for short integersunsigned int - 2/4 bytes for positive integersint - 2/4 bytes for integersunsigned long - 4 bytes for large positive integerslong - 4 bytes for large integersfloat - 4 bytes for floating pointdouble - 8 bytes for long floating pointlong double - 10 bytes for longer floating point

Common (C) Types

ספטמבר 04 Copyright Meir Kalech 15

char and unsigned char are set to represent characters.

Each character has two representations:1. Binary2. 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

ספטמבר 04 Copyright Meir Kalech 16

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

ספטמבר 04 Copyright Meir Kalech 17

A variable defines an area for storing data. Each has: name type address value

Examples:100 150

char color int 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’ 9 8 5 8 7

ספטמבר 04 Copyright Meir Kalech 18

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

ספטמבר 04 Copyright Meir Kalech 19

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

ספטמבר 04 Copyright Meir Kalech 20

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 <stdio.h> library to use the I/O functions. Example:

#include <stdio.h>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)

ספטמבר 04 Copyright Meir Kalech 21

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

ספטמבר 04 Copyright Meir Kalech 22

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

ספטמבר 04 Copyright Meir Kalech 23

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

ספטמבר 04 Copyright Meir Kalech 24

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

ספטמבר 04 Copyright Meir Kalech 25

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

ספטמבר 04 Copyright Meir Kalech 26

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

ספטמבר 04 Copyright Meir Kalech 27

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

ספטמבר 04 Copyright Meir Kalech 28

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

ספטמבר 04 Copyright Meir Kalech 29

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

ספטמבר 04 Copyright Meir Kalech 30

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

ספטמבר 04 Copyright Meir Kalech 31

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

ספטמבר 04 Copyright Meir Kalech 32

== 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

ספטמבר 04 Copyright Meir Kalech 33

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

ספטמבר 04 Copyright Meir Kalech 34

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 || b a or b

a && b a and b

!a not a

aba || ba && b!a

TTTTF

TFTFF

FTTFT

FFFFT

ספטמבר 04 Copyright Meir Kalech 35

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)