ete105 fall2014 lec4 (east west university)

6

Click here to load reader

Upload: turjo987

Post on 25-May-2015

20 views

Category:

Engineering


2 download

DESCRIPTION

East West University ETE lecture for all.

TRANSCRIPT

Page 1: Ete105 fall2014 lec4 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

Semester: Fall 2014

ETE 105: Computer Fundamentals and Programming Language

Lecture 4: C Data Types, Operators & Expressions

STRING CONSTANTS

A string constant consists of any number of consecutive character (including none), enclosed in

(double) quotation marks.

Example 1: Several string constants are shown below:

“green” “Washington, D. C. 20005” “270-32-

3456”

“$19.95” “THE CORRECT ANSWER IS:” “2*(I+3)/J”

“ ” “Line 1\nLine 2\nLine 3” “ ”

Example 2: The following string constant includes three special characters that are represented

by their corresponding excape sequences.

“\tTo continue, press the \”RETURN\” key\n”

DATA TYPES

C supports several different types of data, each of which may be represented differently within

the computer’s memory. The basic data types are listed below.

Data

Type

Description Typical Memory

Requirements int integer quantity 2 bytes or one word (varies

from one compiler to another) char single character 1 byte float floating-point number (i.e., a number containing

a decimal point and /or an exponent)

1 word (4 bytes)

double double-precision floating-point number (i.e.,

more significant figures, and an exponent which

may be larger in magnitude)

2 words (8 bytes)

The basic data types can be augmented by the use of the data type short, long, signed and

unsigned. All available basic arithmetic types are listed below:

Data Type Bytes Range short int 2 -32,768 +32,767 unsigned short int 2 0 +65,535 usnigned int 4 0 +4,294,967,295 int 4 -2,147,483,648 +2,147,483,647 long int 4 -2,147,483,648 +2,147,483,647 signed char 1 -128 +127 unsigned char 1 0 +255

Page 2: Ete105 fall2014 lec4 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering float 4 double 8 long double 12

DECLARATIONS

A declaration associates a group of variables with a specific data type. All variables must be

declared before they can appear in executable statements.

A declaration consists of a data type, followed by one or more variable names, ending with a

semicolon.

Example 3: A C program contains the following type declarations.

int a, b, c;

float root1, root2;

char flag, text[80];

Example 4: A C program could also have the following type declarations.

short int a, b, c;

long int r, s, t;

int p, q;

Initial values can be assigned within a type declaration. To do so, the declaration must consist of

a data type, followed by a variable name, an equal sign (=) and a constant of the appropriate

type. A semicolon must appear at the end, as usual.

Example 5: A C program contains the following type declarations.

int c = 12;

char star = ’*’;

float sum = 0;

double factor = 0.21023e-6;

EXPRESSIONS

An expression represents a single data item, such as a number or a character. The expression may

consist of a single entity, such as a constant, variable, an array element or a reference to a

function. It may also consist of some combination of such entities, interconnected by one or

more operators. Expressions can also represent logical conditions that are either true or false.

For example, several simple expressions are:

a+b

x=y

x<=y

STATEMENTS

A statement causes the computer to carry out some actions. There are three different classes of

statement in C:

1. An Expression statement consists of an expression followed by a semicolon. The

execution of expression statement causes the expression to be evaluated.

Page 3: Ete105 fall2014 lec4 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering 2. A Compound statement consists of several individual statements enclosed within a pair

of braces { }.The individual statements themselves be expression statements, compound

statements or control statements. Unlike an expression, a compound statement does not

end with a semicolon.

3. Control statements are used to create special program features, such as logical tests,

loops and branches.

Example 6: Several expression statements are shown below.

a=3;

c=a+b;

++i;

printf(“Area=%f”, area);

;

Example 7: A typical compound statement is shown below.

{

pi = 3.1416;

circumference = 2*pi*radius;

area = pi*radius*radius;

}

Example 8: The following control statement creates a conditional loop in which several actions

are executed repeatedly.

while(count<=n){

sum+=x;

++count;

}

C OPERATORS

C provides the following types of operators:

Arithmetic Operators

Relational Operators

Logical Operators

Bitwise Operators

Assignment Operators

Misc Operators

ARITHMETIC OPERATORS

For the relational operators shown below, assume variable A holds 10 and variable B holds 20.

Operator Description Example

+ Adds two operands A + B will give 30

- Subtracts second operand from the first A - B will give -10

* Multiplies both operands A * B will give 200

Page 4: Ete105 fall2014 lec4 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

/ Divides numerator by de-numerator B / A will give 2

% Modulus Operator and remainder of after an integer division B % A will give 0

++ Increments operator increases integer value by one A++ will give 11

- - Decrements operator decreases integer value by one A- - will give 9

RELATIONAL OPERATORS

For relational operators shown below, assume variable A holds 10 and variable B holds 20.

Operator Description Example

Checks if the values of two operands are equal or not, if yes

then condition becomes true.

(A= =B) is not

true.

Checks if the values of two operands are equal or not, if values

are not equal then condition becomes true.

(A != B) is true.

Checks if the value of left operand is greater than the value of

right operand, if yes then condition becomes true.

(A > B) is not true.

Checks if the value of left operand is less than the value of

right operand, if yes then condition becomes true.

(A < B) is true.

Checks if the value of left operand is greater than or equal to

the value of right operand, if yes then condition becomes true.

(A >= B) is not

true.

Checks if the value of left operand is less than or equal to the

value of right operand, if yes then condition becomes true.

(A <= B) is true.

LOGICAL OPERATORS

For logical operators shown below, assume variable A holds 1 and variable B holds 0.

Operator Description Example

&& Called Logical AND operator. If both the operands are non-

zero, then condition becomes true.

(A && B) is false.

|| Called Logical OR Operator. If any of the two operands is

non-zero, then condition becomes true.

(A || B) is true.

! Called Logical NOT Operator. Use to reverses the logical

state of its operand. If a condition is true then Logical NOT

operator will make false.

!(A && B) is true.

Page 5: Ete105 fall2014 lec4 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

ASSIGNMENT OPERATORS

Operator Description Example

Simple assignment operator, Assigns values from right side

operands to left side operand

C = A + B will assign

value of A + B into C

Add AND assignment operator, It adds right operand to the

left operand and assign the result to left operand

C += A is equivalent

to C = C + A

Subtract AND assignment operator, It subtracts right

operand from the left operand and assign the result to left

operand

C -= A is equivalent

to C = C - A

Multiply AND assignment operator, It multiplies right

operand with the left operand and assign the result to left

operand

C *= A is equivalent

to C = C * A

Divide AND assignment operator, It divides left operand

with the right operand and assign the result to left operand

C /= A is equivalent

to C = C / A

Modulus AND assignment operator, It takes modulus using

two operands and assign the result to left operand

C %= A is equivalent

to C = C % A

Left shift AND assignment operator C <<= 2 is same as C

= C << 2

Right shift AND assignment operator C >>= 2 is same as C

= C >> 2

Bitwise AND assignment operator C &= 2 is same as C

= C & 2

bitwise exclusive OR and assignment operator C ^= 2 is same as C =

C ^ 2

MISC OPERATORS IN C

Operator Description Example

sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.

& Returns the address of a variable. &a; will give actual address of the

variable.

* Pointer to a variable. *a; will pointer to a variable.

? : Conditional Expression If Condition is true ? Then value X :

Otherwise value Y

Page 6: Ete105 fall2014 lec4 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

HIERARCHY OF OPERATORS

Operator Category Operators Associatively

unary operators RL

arithmetic multiply, divide and remainder LR

arithmetic add and subtract LR

relational operators LR

equality operators LR

logical AND LR

logical OR LR

Conditional operator RL

Assignment operator RL