variables, data types, operator & expression in c in detail

48
VARIABLES, DATA TYPES, OPERATOR & EXPRESSION Chap 2 06/28/2022 1

Upload: gourav-kottawar

Post on 12-Apr-2017

884 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

1

VARIABLES, DATA TYPES, OPERATOR & EXPRESSION Chap 2

Page 2: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

2

Contents 2.1 Character Set 2.2 C Tokens Keywords & Identifiers Constants Integer, Floating Point,

Character, String, Enumeration 2.3 Backslash characters /

Escape sequences 2.4 Data Types in C  2.5 Variables  2.6 Declaration & Definition 2.7 User-Defined Type

declarations

2.8 Operators & Expressions Arithmetic, Relational, Logical, Increment Decrement , Bit wise, Assignment, Conditional2.9 Type conversions in Expressions 2.10 Implicit Type Conversion 2.11 Explicit Type Conversions 2.12 Precedence & Associability of Operators.

Page 3: Variables, Data Types, Operator & Expression in c in detail

3

2.1 character set in c

05/03/2023

Page 4: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

4

2.1 character set in c

Page 5: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

5

2.2 C tokens Smallest individual units is called as

‘Tokens ‘

Page 6: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

6

Keywords And Identifiers Every C word is classified either

“Keyword” or “Identifier “.Rules for Keywords Has fixed meaning Meaning can not be changed Served as basic building block for

program statement. Must be written in lower case

Page 7: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

7

Keywords and Identifiers

Page 8: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

8

Keywords and Identifiers IdentifiersRefer to names of variables , functions and

arraysRules for identifiers1. First character must be an alphabet (or

underscore)2. Must consist of only letters, digits or

underscore.3. Only first 31 characters are significant.4. Cannot use a keyword5. Must not contain white space

Page 9: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

9

Constants

Constants

Numeric Constants

Integer Constants

Real Constants

Character constants

Single Character Constants

String Constants

Constants in C refers to fixed values and do not change during the execution of a program

Page 10: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

10

constants Integer Constants Real Constants can be represented as exponential format.

mantissa e exponentMantissa is either a real number expressed in

decimal notation or integerThe exponent is an integer number with an

optional plus or minus signThe letter e can be written in lowercase or

uppercase.

Page 11: Variables, Data Types, Operator & Expression in c in detail

11

constants Single character constants String constants Backslash Character constants – useful in output

functions also known as escape sequences

05/03/2023

Page 12: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

12

Escape seqence

Page 13: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

13

Variables A data name used to store data values May take different values at different times of

program execution Rules for variable1. Should not be keyword2. Should not contain white space3. Uppercase and lower case are significant4. Must begin with letter ( or underscore)5. Normally should not be more than eight

characters.

Page 14: Variables, Data Types, Operator & Expression in c in detail

Data Transformation Programs transform data from one form to

another Input data Output data Stimulus Response

Programming languages store and process data in various ways depending on the type of the data; consequently, all data read, processed, or written by a program must have a type

Two distinguishing characteristics of a programming language are the data types it supports and the operations on those data types

Page 15: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

15

2.4 Data types C is rich in its data type. Variety of data type allows programmer

to select appropriate data type as per need

ANSI supports 3 classes of data type1. Primary or fundamental data type2. Derived data type3. User defined data type.

Page 16: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

16

Data types in C

Page 17: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

17

Data types in C

Page 18: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

18

Page 19: Variables, Data Types, Operator & Expression in c in detail

19

2.8 OPERATORS AND EXPRESSIONS

Page 20: Variables, Data Types, Operator & Expression in c in detail

Expressions Combination of Operators and Operands

Example 2 * y + 5

Operands

Operators

Page 21: Variables, Data Types, Operator & Expression in c in detail

Definition

“An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations and is usually used to manipulate data and variables”Ex: a+b

Page 22: Variables, Data Types, Operator & Expression in c in detail

Operators in C1. Arithmetic operators2. Relational operators3. Logical operators4. Assignment operators5. Increment and decrement operators6. Conditional operators7. Bitwise operators8. Special operators

Page 23: Variables, Data Types, Operator & Expression in c in detail

Arithmetic operators

Operator example Meaning+ a + b Addition –unary- a – b Subtraction- unary* a * b Multiplication/ a / b Division

% a % b Modulo division- remainder

Page 24: Variables, Data Types, Operator & Expression in c in detail

Relational Operators

Operator Meaning< Is less than

<= Is less than or equal to> Is greater than

>= Is greater than or equal to

== Equal to!= Not equal to

Page 25: Variables, Data Types, Operator & Expression in c in detail

Logical OperatorsOperator Meaning

&& Logical AND|| Logical OR! Logical NOT

Logical expression or a compound relational expression-An expression that combines two or more relational expressions Ex: if (a==b && b==c)

Page 26: Variables, Data Types, Operator & Expression in c in detail

Truth Table

a bValue of the expression

a && b a || b0 0 0 0

0 1 0 1

1 0 0 11 1 1 1

Page 27: Variables, Data Types, Operator & Expression in c in detail

Assignment operators

Syntax:v op = exp;

Where v = variable, op = shorthand assignment operator

exp = expressionEx: x=x+3

x+=3

Page 28: Variables, Data Types, Operator & Expression in c in detail

Shorthand Assignment operators

Simple assignment operator Shorthand operator

a = a+1 a + =1a = a-1 a - =1

a = a* (m+n) a * = m+n

a = a / (m+n) a / = m+n

a = a %b a %=b

Page 29: Variables, Data Types, Operator & Expression in c in detail

Increment & Decrement OperatorsC supports 2 useful operators namely1. Increment ++2. Decrement – operatorsThe ++ operator adds a value 1 to the

operandThe – operator subtracts 1 from the

operand++a or a++--a or a--

Page 30: Variables, Data Types, Operator & Expression in c in detail

Rules for ++ & -- operators

1. These require variables as their operands2. When postfix either ++ or – is used with

the variable in a given expression, the expression is evaluated first and then it is incremented or decremented by one

3. When prefix either ++ or – is used with the variable in a given expression, it is incremented or decremented by one first and then the expression is evaluated with the new value

Page 31: Variables, Data Types, Operator & Expression in c in detail

Examples for ++ & -- operators

Let the value of a =5 and b=++a thena = b =6Let the value of a = 5 and b=a++ thena =5 but b=6i.e.: 1. a prefix operator first adds 1 to the

operand and then the result is assigned to the variable on the left

2. a postfix operator first assigns the value to the variable on left and then increments the operand.

Page 32: Variables, Data Types, Operator & Expression in c in detail

Conditional operators

Syntax:exp1 ? exp2 : exp3Where exp1,exp2 and exp3 are expressionsWorking of the ? Operator:Exp1 is evaluated first, if it is nonzero(1/true) then the

expression2 is evaluated and this becomes the value of the expression,

If exp1 is false(0/zero) exp3 is evaluated and its value becomes the value of the expression

Ex: m=2; n=3 r=(m>n) ? m : n;

Page 33: Variables, Data Types, Operator & Expression in c in detail

Bitwise operators

These operators allow manipulation of data at the bit level

Operator Meaning & Bitwise AND| Bitwise OR^ Bitwise exclusive OR

<< Shift left>> Shift right

Page 34: Variables, Data Types, Operator & Expression in c in detail

Special operators

1. Comma operator ( ,)2. sizeof operator – sizeof( )3. Pointer operators – ( & and *)4. Member selection operators – ( . and -

>)

Page 35: Variables, Data Types, Operator & Expression in c in detail

Arithmetic Expressions

Algebraic expression C expression

axb-c a*b-c

(m+n)(x+y) (m+n)*(x+y)a*b/c

3x2+2x+1 3*x*x+2*x+1a/b

S=(a+b+c)/2

cab

ba

2cba S=

Page 36: Variables, Data Types, Operator & Expression in c in detail

Arithmetic Expressions

Algebraic expression C expression

area= area=sqrt(s*(s-a)*(s-b)*(s-c))

sin(b/sqrt(a*a+b*b))

tow1=sqrt((rowx-rowy)/2+tow*x*y*y)

tow1=sqrt(pow((rowx-rowy)/2,2)+tow*x*y*y)

y=(alpha+beta)/sin(theta*3.1416/180)+abs(x)

))()(( csbsass

Sin

22 ba

b

21 2

xyyx

22

1 2xyyx

xy

sin

Page 37: Variables, Data Types, Operator & Expression in c in detail

Precedence of operatorsBODMAS RULE-Brackets of Division Multiplication Addition SubtractionBrackets will have the highest precedence and have to be evaluated first, then comes of , then comes division, multiplication, addition and finally subtraction.C language uses some rules in evaluating the expressions and they r called as precedence rules or sometimes also referred to as hierarchy of operations, with some operators with highest precedence and some with least.The 2 distinct priority levels of arithmetic operators in c

are-Highest priority : * / %Lowest priority : + -

Page 38: Variables, Data Types, Operator & Expression in c in detail

Rules for evaluation of expression1. First parenthesized sub expression from left to right are

evaluated.2. If parentheses are nested, the evaluation begins with the

innermost sub expression3. The precedence rule is applied in determining the order

of application of operators in evaluating sub expressions4. The associatively rule is applied when 2 or more

operators of the same precedence level appear in a sub expression.

5. Arithmetic expressions are evaluated from left to right using the rules of precedence

6. When parentheses are used, the expressions within parentheses assume highest priority

Page 39: Variables, Data Types, Operator & Expression in c in detail

Hierarchy of operators

Operator Description Associativity( ), [ ] Function call, array

element referenceLeft to Right

+, -, ++, - -,!,~,*,&

Unary plus, minus, increment, decrement, logical negation, 1’s complement, pointer reference, address

Right to Left

*, / , % Multiplication, division, modulus

Left to Right

Page 40: Variables, Data Types, Operator & Expression in c in detail

Example 1Evaluate x1=(-b+ sqrt (b*b-4*a*c))/(2*a) @ a=1,

b=-5, c=6=(-(-5)+sqrt((-5)(-5)-4*1*6))/(2*1)=(5 + sqrt((-5)(-5)-4*1*6))/(2*1)=(5 + sqrt(25 -4*1*6))/(2*1)=(5 + sqrt(25 -4*6))/(2*1)=(5 + sqrt(25 -24))/(2*1)=(5 + sqrt(1))/(2*1)=(5 + 1.0)/(2*1)=(6.0)/(2*1)=6.0/2 = 3.0

Page 41: Variables, Data Types, Operator & Expression in c in detail

Example 2Evaluate the expression when a=4

b=a- ++a=a – 5=5-5=0

Page 42: Variables, Data Types, Operator & Expression in c in detail

05/03/2023

42

Order of evaluationHighest() [] ->

! ~ ++ -- (type *) & sizeof* / %+ -<< >>< <= > >=== !=&^|&&||? := += -= *= /=

Lowest ,

Page 43: Variables, Data Types, Operator & Expression in c in detail

43

2.9 Type conversions in expressions 1. Implicit Type Conversion C permits mixing of constants and

variables of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. This automatic conversion is known as implicit type conversion.

The rule of type conversion: the lower type is automatically converted to the higher type.

Page 44: Variables, Data Types, Operator & Expression in c in detail

44

2.14 Type conversions in expressions for example,

int i, x; float f; double d; long int li ;

The final result of an expression is converted to the type of the variable on the left of the assignment.

long

long

float

float

float

float

double

doubleint

x = li / i + i * f – d

Page 45: Variables, Data Types, Operator & Expression in c in detail

45

3.14 Type conversions in expressions

Conversion Hierarchy is given below

long int

Conversion hierarchy

charshort

floatdouble

long double

int

unsigned long int

Page 46: Variables, Data Types, Operator & Expression in c in detail

46

3.14 Type conversions in expressions

2. Explicit conversion We can force a type conversion in a way . The general form of explicit conversion is

( type-name ) expression for example

x = ( int ) 7.5 ; a = ( int ) 21.3 / (int) 4.5; a = ( float ) 3 / 2 ; a = float ( 3 / 2 ) ;

Page 47: Variables, Data Types, Operator & Expression in c in detail

47

2.15 Operator precedence and Associativity Rules of Precedence and Associativity

(1)Precedence rules decides the order in which different operators are applied.

(2)Associativity rule decide the order in which multiple occurrences of the same level operator are applied.

Table3.8 on page71 shows the summary of C Operators.

for example, a = i +1== j || k and 3 != x

Page 48: Variables, Data Types, Operator & Expression in c in detail