5. Example for floating point variable:
Exampl
e
Result
a*b 25.0
a/b 6.25
a+b 14.5
a-b 10.5
a%b NA
a=12.5
b=2.0
5
6. Example for Character:
Example Result
C1 80
c1+c2 164
c1+c2+5 169
c1+c2+
‘5’
217
Char C1,
C2
C1=‘T’
C2=‘P’
The ASCII code of ‘5’ is 53
6
7. 2-Increment & Decrement Operators:
Operator Symbol Example
Increment ++ a++ or ++a
Decrement -- a-- or --a
7
8. 2-Increment & Decrement Operators:
Operator Symbol Example
Increment ++ a++ or ++a
Decrement -- a-- or --a
8
9. 3-Relational Operators
(use in decision making)
Operator
Symbo
l
Example
less than < a<b
greater than > a>b
less than or equal <= a<=b
greater than or
equal
>= a>=b
equal == a==b
Not equal != a!=b9
15. Bitwise Operator
Operator Symbol Example
AND & a&b
OR | (Pipe) a|b
Exclusive OR ^ (Caret) a^b
Complement ~ (Tilde) ~a
Right Shift >> a>>b
Left Shift << a<<b
15
16. Address and Indirection Operator:
Operator Symbol Example
Address & addr=&var
Indirection * Value=*addr
16
18. Conditional Operator
Generic Expression Example
result=(expression) ? Value1: value 2; max=(a>b) ? a:b;
If the expression is true (other than 0) then value 1 will be assigned to variable
“result”
If the expression is false (equal to 0) then value 2 will be assigned to variable “result”
18
20. Algebraic Expression:
Algebraic Expression C Expression
a x b- c x d A *b-c*d
(m+n)(a+b) (m+n)*(a+b)
3x2+2x+5 3*x*x+2*x+5
a+b+c/d+e (a+b+c)/(d+e)
[2by/d+1-x/3(z+y)] 2*b*y/(d+1)-x/3*(z+y)
20
21. Assignment Operators
Operator Symbol Example
Equal(assign the value, different from
==)
= a=b
addition += a+=b (same as a=a+b)
subtraction -= a-=b (same as a=a-b)
multiplication *= a*=b (same as a=a*b)
division /= a/+b (same as a=a/b)
remainder %= a%=b (same as a=a%b)
Bitwise AND &= a&=b (same as a=a&b)
Bitwise inclusive OR |= a|=b (same as a=a|b)
Bitwise exclusive OR ^= a^=b (same as a=a^b)
Left shift <<= a<<=2 (same as a=<<2)
Right shift >>= a>>=3 (same as a=>>3)
21
22. Example: Assignment Operator
Expression Alternative expression
Variable 1+= variable 2 Variable 1=variable1 +variable 2
Variable 1-= variable 2 Variable 1=variable1 -variable 2
Meanings: The variable 1 on left side will add value
of variable 2 on right side and the resultant new value
will be assign to variable 1
22
24. Escape Sequence:
Escape sequence is used in the printf() function to do something to
the output.
Escape
Sequence
Effect
a Beep sound, alarm
b Backspace
f Form feed (for printing)
n New line
r Carriage return (move the curser to beginning of current line)
t Tab
v Vertical tab
Backslash (to print at output)
” Used to print (“) sign
’ Used to print (‘) sign
27
25. Format Specifier
Tells the printf() function the format of the output to be printed put
No Format Specifier Output Type Output Example
1 %d or %i Signed decimal integer (short) 76
2 %u Unsigned decimal integer (short) 76
3 %o Unsigned octal integer 134
4 %x Unsigned hexadecimal (small letter) 9c
5 %X Unsigned hexadecimal (capital letter) 9C
6 %f Integer including decimal point 76.0000
7 %e Signed floating point (using e notation) 7.6000e+01
8 %E Signed floating point (using E notation) 7.6000E+01
9 %g The shorter between %f and %e 76
10 %G The shorter between %f and %E 76
11 %c Single Character (both for signed & unsigned) ‘7’
12 %s String “76”
28
26. Filed–Width Specifiers:
With the help of this
Interpretation of a variable’s type
Width of the field
Number of decimal places printed
Justification
29
27. Example # 1: Field-Width Specifer:
A g e i s 3 3 .
A g e i s 3 3 .
A g e i s 3 3 .
printf(“Age is %2d.”, age);
printf(“Age is %4d.”, age);
printf(“Age is %6d.”, age);
30
28. Example # 2: Field-Width Specifer:
main()
{
Float temp=27.25;
printf(“The temperature is %f.”, temp);
}
Output:
The Temperature is 27.250000
main()
{
Float temp=27.25;
printf(“The temperature is %.2f.”, temp);
}
Output:
The Temperature is 27.25
31
29. Study the following example:
main()
{
printf(“%.1f%8.1f%8.1fn”,3.0,12.5,523.3);
printf(“%.1f%8.1f%8.1fn”,300.0,1200.5,52300.3);
}
What will be the output??
32
31. Study the following example:
main()
{
printf(“%-.1f%-8.1f%-8.1fn”,3.0,12.5,523.3);
printf(“%-.1f%-8.1f%-8.1fn”,300.0,1200.5,52300.3);
}
What will be the output??
34
33. Explanation:
%8.1f
%
8 . 1
f
Signals format
specification
field width
# of digits to the
right of decimal
Indicates
decimal-format
floating point
36
34. Scanf and Assignment:
scanf:
It is used to save the function input into its relavent
variable.
Assignment operator:
Its takes answer of a statement and saves into other
variable
37
36. Associativity of operators
When two expression contains two operators of
equal priority the tie between them is settled using
the associativity of the operators .
1. Left to right (left operand must be unambiguous)
2. Right to left (right operand must be unambiguous)
Means it must not be involved in evaluation of any
other sub-expression
Example:
a=3/2*5
39
37. Example: a=3/2*5
Operator left right remark
/ 3 2 or 2*5
Left operand
is
unambiguous
, Right is not
* 3/2 or 2 5
Right
operand is
unambiguous
, Left is not
Meanings: Since both have / and * have L to R associativity and
Only / has unambiguous, left operand (necessary condition for L to R associativity
is performed earlier.
40