Lecture 7 8 (Pic) .PDF 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

PROGRAMMING IN ‘C’

 In C programs, values for the variables are assigned


using assignment operators.
 For example, if the value ―10‖ is to be assigned for the
variable ―sum‖, it can be assigned as
―sum= 10;‖
 Other assignment operators in C language are given
below.
Operators Example Explanation

Simple
assignment = sum = 10
10 is assigned to
operator variable sum
sum += 10 This is same as
Compound += sum = sum + 10
assignment This is same as
operators -= sum -= 10 sum = sum – 10
This is same as
*= sum *= 10 sum = sum * 10
This is same as
/+ sum /= 10 sum = sum / 10
This is same as
%= sum = sum % 10
sum %= 10
This is same as
&= sum&=10 sum = sum & 10
This is same as
^= sum ^= 10 sum = sum ^ 10
 Relational operators are used to find the relation between two
variables. i.e. to compare the values of two variables in a C
program.
S.no Operators Example Description
x is greater than y
1 > x>y

2 < x<y x is less than y


x is greater than or
3 >= x >= y equal to y
x is less than or
4 <= x <= y equal to y
5 == x == y x is equal to y

x is not equal to y
6 != x != y
 These operators are used to perform logical operations
on the given expressions.
 There are 3 logical operators in C language. They are,
logical AND (&&), logical OR (||) and logical NOT (!).
S.no Operators Name Example Description

It returns true when both


1 && logical AND (x>5)&&(y<5) conditions aretrue

It returns true when at-


2 || logical OR (x>=10)||(y>=10 least one of the
) condition is true

It reverses the state of the


3 ! logical NOT !((x>5)&&(y<5) operand
) ―((x>5)&&
(y<5))‖
If―((x>5)
&& (y<5))‖ is true,
logical NOT operator
makes itfalse
 These operators are used to perform bit operations. Decimal values
are converted into binary values which are the sequence of bits and
bit wise operators work on these bits.
 Bit wise operators in C language are & (bitwise AND), | (bitwise
OR), ~ (bitwise OR), ^ (XOR),
 << (left shift) and >> (right shift).

Operator_symbol Operator_name
& Bitwise_AND
| Bitwise OR
~ Bitwise_NOT
^ XOR
<< Left Shift
>> Right Shift
x y x|y x&y x^ y

0 0 0 0 0

0 1 1 0 1

1 0 1 0 1

1 1 1 1 0
 Conditional operators return one value if condition is true and
returns another value is condition is false.
 This operator is also called as ternary operator.
Syntax : (Condition? true_value:false_value);
Example: (A > 100 ? 0 :1);
 In above example, if A is greater than 100, 0 is returned else 1 is
returned. This is equal to if else conditional statements.
 Increment operators are used to increase the value of the variable
by one and decrement operators are used to decrease the value of
the variable by one in Cprograms.
 Syntax:
Increment operator: ++var_name ;( or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
 Example:
Increment operator : ++i; i ++ ;
Decrement operator : – – i ; i – – ;
 Below table will explain the difference between pre/post increment
and decrement operators in C.

S.no Operator type Operato Description


r

1 Pre increment
++i Value of i is incremented before assigning it to
variable i.

Post–increment Value of i is incremented after assigning it to


i++ variable i.
2

Value of i is decremented before assigning it


— –i to variable i.
3 Pre decrement

Value of i is decremented after assigning it to


i– — variable i.
4 Post_decrement
 Below are some of special operators that C language offers.
S.no Operators Description

This is used to get the address


1 & of the variable.

Example : &a will give


address of a.
This is used as pointer to a
2 * variable.

Example : * awhere, * is
pointer to the variablea.
This gives the size of the
3 Sizeof () variable.

Example : size of (char) will


give us 1.
 Arithmetic expression in C is a combination of variables, constants
and operators written in a proper syntax. C can easily handle any
complex mathematical expressions but these mathematical
expressions have to be written in a proper syntax.
 Note: C does not have any operator for exponentiation.
 C operators in order of precedence (highest to lowest). Their
associativity indicates in what order operators of equal precedence
in an expression are applied.
 Parentheses are also used to group sub-expressions to force a
different precedence; such parenthetical expressions can be nested
and are evaluated from inner to outer.
 Postfix increment/decrement have high precedence, but the actual
increment or decrement of the operand is delayed (to be
accomplished sometime before the statement completes execution).
So in the statement y = x * z++; the current value of z is used to
evaluate the expression (i.e., z++ evaluates to z) and z only
incremented after all else is done.
Operator Description Associativity
() Parentheses (function call) (see Note 1) left-to-right
[] Brackets (array subscript)
. Member selection via object name Member
-> selection via pointer
++ -- Postfix increment/decrement (see Note 2)
++ -- Prefix increment/decrement right-to-left
+- Unary plus/minus
!~ Logical negation/bitwise complement
(type) Cast (convert value to temporary value of type)
* Dereference
& Address (of operand)
sizeof Determine size in bytes on this implementation
*/% Multiplication/division/modulus left-to-right
+- Addition/subtraction left-to-right
<<>> Bitwise shift left, Bitwise shift right left-to-right
<<= Relational less than/less than or equal to left-to-right
>>= Relational greater than/greater than or equal to
==!= Relational is equal to/is not equal to left-to-right
& Bitwise AND left-to-right
^ Bitwise exclusive OR left-to-right
| Bitwise inclusive OR left-to-right
&& Logical AND left-to-right
|| Logical OR left-to-right
?: Ternary conditional right-to-left
= Assignment right-to-left
+=-= Addition/subtraction assignment
*=/= Multiplication/division assignment
%=&= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<=>>= Bitwise shift left/right assignment
, Comma (separate expressions) left-to-right
 At first, the expressions within parenthesis are evaluated. If no
parenthesis is present, then the arithmetic expression is evaluated
from left to right. There are two priority levels of operators in C.
High priority: * /%
Low priority: + -
 The evaluation procedure of an arithmetic expression includes two
left to right passes through the entire expression.
 In the first pass, the high priority operators are applied as they are
encountered and
 In the second pass, low priority operations are applied as they are
encountered.
Suppose, we have an arithmetic expression as:
x = 9 – 12 / 3 + 3 *2 -1
 This expression is evaluated in two left to right passes as:
First Pass
Step 1: x = 9-4 + 3 * 2 – 1
Step 2: x = 9 – 4 + 6 - 1
Second Pass
Step 1: x = 5 + 6 – 1
Step 2: x = 11 – 1
Step 3: x = 10
But when parenthesis is used in the same expression, the order of
evaluation gets changed.
For example,
x = 9 – 12 / (3 + 3) * (2 –1)
 When parentheses are present then the expression inside the
parenthesis are evaluated first from left to right. The expression is
now evaluated in three passes as:
First Pass
Step 1: x = 9 – 12 / 6 * (2 – 1)
Step 2: x= 9 – 12 / 6 * 1
Second Pass
Step 1: x= 9 – 2 * 1
Step 2: x = 9 – 2
Third Pass
Step 3: x=7
 When we have an expression as:
x = 9 – ((12 / 3) + 3 * 2) –1
 The expression is now evaluated as:
First Pass:
Step 1: x = 9 – (4 + 3 * 2) – 1
Step 2: x= 9 – (4 + 6) – 1
Step 3: x= 9 – 10 -1
Second Pass
Step 1: x= - 1 – 1
Step 2: x = -2
 When variables and constants of different types are combined in an
expression then they are converted to same data type. The process of
converting one predefined type into another is called type conversion.
 Type conversion in c can be classified into the following two types:
1. Implicit Type Conversion
 When the type conversion is performed automatically by the compiler
without programmer‘s intervention, such type of conversion is known
as implicit type conversion or type promotion.
 The compiler converts all operands into the data type of the largest
operand.
 The sequence of rules that are applied while evaluating expressions are given
below:
 All short and char are automatically converted to int, then,
 If either of the operand is of type long double, then others will be converted to long
double and result will be long double.
 Else, if either of the operand is double, then others are converted to double.
 Else, if either of the operand is float, then others are converted to float.
 Else, if either of the operand is unsigned long int, then others will be converted to
unsigned long int.
 Else, if one of the operand is long int, and the other is unsigned int, then
 if a long int can represent all values of an unsigned int, the unsigned int is
converted to long int. otherwise, both operands are converted to unsigned long int.
 Else, if either operand is long int then other will be converted to long int.
 Else, if either operand is unsigned int then others will be converted to unsigned int.
 It should be noted that the final result of expression is converted to type of variable
on left side of assignment operator before assigning value to it.
 Also, conversion of float to int causes truncation of fractional part, conversion of
double to float causes rounding of digits and the conversion of long int to int causes
dropping of excess higher order bits.
 Explicit Type Conversion
The type conversion performed by the programmer by posing the
data type of the expression of specific type is known as explicit type
conversion.
The explicit type conversion is also known as type casting.
Type casting in c is done in the following form:
(data_type)expression;
where, data_type is any valid c data type, and expression may be
constant, variable or expression.
For example, x=(int)a+b*d;
The following rules have to be followed while converting the
expression from one type to another to avoid the loss of
information:
All integer types to be converted to float.
All float types to be converted to double.
All character types to be converted to integer.
 C provides standard functions scanf() and printf(), for performing
formatted input and output. These functions accept, as parameters, a
format specification string and a list of variables.
 The format specification string is a character string that specifies the
data type of each variable to be input or output and the size or width
of the input and output.
 The function printf() is used for formatted output to standard output
based on a format specification. The format specification string,
along with the data to be output, are the parameters to the printf()
function.
 Syntax:
printf (format,data1,data2,… );
 In this syntax format is the format specification string. This string
contains, for each variable to be output, a specification beginning
with the symbol % followed by a character called the conversion
character.
 Example:
printf (―%c‖, data1);
 The character specified after % is called a conversion character
because it allows one data type to be converted to another type and
printed.
Conversion Character Meaning

d The data is converted to decimal (integer)

c The data is taken as a character.

s The data is a string and character from the string ,


are printed until a NULL,character is reached.

f The data is output as float or double with a default


Precision 6.

Symbols Meaning

\n For new line (linefeed return)

\t For tab space (equivalent of 8 spaces)


 The function scanf() is used for formatted input from standard input
and provides many of the conversion facilities of the function
printf().
 Syntax
scanf (format, num1, num2,……);
 The function scnaf() reads and converts characters from the
standards input depending on the format specification string and
stores the input in memory locations represented by the other
arguments (num1, num2,….).
 For Example:
scanf(―%c %d‖,&Name,&Roll No);
 Note: the data names are listed as &Name and &Roll No instead of
Name and Roll No respectively. This is how data names are
specified in a scnaf() function. In case of string type data names, the
data name is not preceded by the character &.

You might also like