Professional Documents
Culture Documents
Size (bytes)
Float
Double
long double
12
Note: The size of the basic data types shown in the above table in C may vary from
one machine to the other.
The syntax for declaring the variable is: datatype variable;
For example the following are few declarations:
int a;
float c;
char name;
int sum;
double count;
If two are more variables of the same data type has to be declared they can be
combined as shown in example given below.
i.
ii.
iii.
iv.
Example: Suppose you are asked to compute the area of a circle where the radius
accepts whole numbers only. What datatypes will be used?
In this example we know that the radius is accepting a whole number and the area
is a real number and hence the following declarations must be used.
int radius;
float area;
Exercise2: Assume that you are assigning an alphabet to every lecture section
what datatype you will use and how do you declare it?
char section;
1.
2.
3.
4.
5.
6.
7.
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment/decrement operators
Conditional operators (ternary operators)
Special operators
1. Arithmetic Operators in C:
C Arithmetic operators are used to perform mathematical calculations like addition,
subtraction, multiplication, division and modulus in C programs.
S.no
1
2
3
4
5
Example:
Arithmetic Operators
+
*
/
%
The
following
algebraic
Operation
Addition
Subtraction
multiplication
Division
Modulus
2
2 r +2 rh
expression
Example
A+B
A-B
A*B
A/B
A%B
is
written
as
2*3.142*r*r+2*3.142*r*h
Exercise 3: Convert the following expression
a x + bx+ c
into a C expression.
a*x*x+b*x+c
Example: if a, b are two integer type values. If a=40 and b=20 then a%b = 0
Exercise4: If a, b, and c are three integer type variables. If b=2 and c = 7, what is
the value of a after the execution of the statement a = b + c * c % b;
a=3
(i) ( i 3 * j ) % ( c + 2 * d ) / ( x y )
-466.667
2. Relational Operators
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 Relational Operators
Example
Operation
1
>
x>y
x is greater than y
2
<
x<y
x is less than y
3
>=
x >= y
x is greater than or equal to y
4
<=
x <= y
x is less than or equal to y
5
==
x == y
x is equal to y
3.Logical Operators
There are 3 logical operators in C language. They are, logical AND (&&),
logical OR (||) and logical NOT (!).
S.n Operat
Name
Example
Description
o
ors
1
&&
logical
(x>5)&&(y<5) It returns true when both conditions
AND
are true
2
||
logical
(x>=10)||
It returns true when at-least one of
OR
(y>=10)
the condition is true
3
!
logical
!
It reverses the state of the operand
NOT
((x>5)&&(y<5 ((x>5) && (y<5))
))
If ((x>5) && (y<5)) is true, logical
NOT operator makes it false
Example: If a = 6 and b = 9, what is the output of the expression a||b
The expression is 6||9 since || is a logical operator any non zero number|| non zero
number is 1.
Exercise 6: If the values of a and b are same as in the example, what is the output
of the following expressions:
1.a && b
1
2.! (a+b)
0
3. a=b=5
a=5,b=5
Example: Construct a logical expression to represent each of the following
conditions
a.n is divisible by 3 and n is not 30
n is divisible by 3 can be written as (n%3==0) since any number divisible by 3 will
have a remainder 0.
Hence the expression can be written as ( ( n % 3 ) == 0 && n != 30 )
b.c is a lowercase letter
since the ASCII value of a is 97 and z is
122 any lowercase letters must lie
within this range
Hence the expression can be written as ( c >= 97 && c <=122 )
( answer == Y || answer
( ( n % 2 ) == 1
(( c >= 97 && c
goldsponsor >=5000
4. Assignment operators 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;
=
+
=
-=
*=
/+
%
=
&
=
^
=
Example
sum = 10
sum +=
10
sum
-=
10
sum
*=
10
sum
/=
10
sum %=
10
sum&=10
sum
10
^=
Explanation
10 is assigned
sum
This is same as
+ 10
This is same as
10
This is same as
* 10
This is same
sum / 10
This is same as
% 10
This is same as
& 10
This is same as
^ 10
to variable
sum = sum
sum = sum
sum = sum
as sum =
sum = sum
sum = sum
sum = sum
Example: Trace the following code fragment, showing the value of each variable
each time it changes
int x=2,y=4;
x=y%=x+2;
1st operation x+2=4
2nd operation y%=4 can be expanded as y=y%4 hence y=0
3rd operation x=y hence x=0
Hence finally the values of x=0 and y=0
Exercise8:Trace the following code fragment, showing the value of each variable
each time it changes
int x, y, z;
x = y = z = 6;
x *= y += z -= 4;
1st operation z=z-4
2nd operation y=y+z
3rd operation x=x*y
x = 48, y = 8, z = 2
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : - i ; i - ;
6. Conditional or Ternary operators
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.
Example. If a = 6 and b = 9, what is the output of the expression a=(++b <3) ? a:b
++b causes b value to be 10 and since 10<3 value is false a is assigned the
value of b which is 10.Thus the effect of this statement will cause a=10 and
b=10.
Exercise9: If a = 6 and b = 9, what is the output of the expression a=(b <3) ? a:b+
+
a=9,b=10
Exercise10:
Consider the following statements.
a=0;
a= (a=0)? 2:3;
printf(%d,a);
a) What will be output?
a=3
7. Special Operators
Below are some of special operators that C language offers.
S.no
Operators
Description
1
&
This is used to get the address of the variable.
Example : &a will give address of a.
2
*
This is used as pointer to a variable.
Example : * a where, * is pointer to the variable a.
3
Sizeof ()
This gives the size of the variable.
Example : size of (char) will give us 1.
For example, the expression a+b-c is considered in C language + and have same
precedence and hence we need to decide whether + to be evaluated first or . In C
language most (not all refer to Table1) operators are left associative hence first a+b
is evaluated and then c is subtracted from that result of a+b.
Preceden
ce
Operat
or
Description
()
[]
.
->
++ --
Associativi
ty
Left-to-right
++
-- Prefix increment/decrement
+
- Unary plus/minus
!
~ Logical negation/bitwise complement
(type)
Cast (convert value to temporary value
*
of type)
&
Dereference
sizeof
Address (of operand)
Determine size in bytes on this
implementation
Right-to-left
* / %
Multiplication/division/modulus
left-to-right
+ -
Addition/subtraction
left-to-right
<< >>
left-to-right
<
<=
> >=
left-to-right
== !=
left-to-right
&
Bitwise AND
left-to-right
Bitwise exclusive OR
left-to-right
10
Bitwise inclusive OR
left-to-right
11
&&
Logical AND
left-to-right
12
||
Logical OR
left-to-right
13
?:
Ternary conditional
right-to-left
14
=
+=
*=
%=
^=
<<=
>>=
Assignment
Addition/subtraction assignment
Multiplication/division assignment
Modulus/bitwise AND assignment
Bitwise exclusive/inclusive OR assignment
Bitwise shift left/right assignment
right-to-left
-=
/=
&=
|=
15
left-to-right
Table1: Precedence and Associativity table where 1 indicated higher precedence and
it decreases as it goes down the table from 2-15.
Example: Which of the following is the correct order of evaluation for the below
expression?
z=x+y*z/4%21
In the example using Table1 *,/ and % and higher than + and and
Step 1: y*z
Step 2: result of Step1/4
Step 3: result of Step 3%2
Step 4: x + result of Step3
Step 5: Result of Step4 - 1
Hence the order of evaluation is * / % + - =
Exercise12:
In the following program, what values of a, b, c, and x will be printed after the
execution of each statement: stmt 1 to stmt 9.
int main(void)
{
int a,b,c,x;
a=9;
b=12;
c=3;
x= a b /3 +c *2 -1;
//stmt 1
a=9,b=12,c=3,x=10
//stmt 2
a=9,b=12,c=3,x=7
x= a ( b /(3+c) * 2) -1;
//stmt 3
a=9,b=12,c=3,x=4
x*=a/=7;
//stmt 4
a=1,b=12,c=3,x=4
x = b *= c;
// stmt 5
a=1,b=36,c=3,x=36
x = x + 9;
value.
a=1,b=36,c=3,x=45
x = a++ - b + --b * c;
//stmt 7
x = (a++) - b + --b * c;
//stmt 8
a=3,b=34,c=3,x=69
x = a && b && c || 1;
//stmt 9
a=3,b=34,c=3,x=1
return 0;
}
a=2,b=35,c=3,x=70
Example:
char c;
int j;
float f;
double d,r;
r=(c*j)+(f/j)(f+d);
Figure 1
In the example above
Step 1: Evaluate (c*j) where c is a character and j is int, hence char is implicitly
converted into int and result is int.
Step 2: Evaluate (f/j) where f is float and j is int, hence j is implicitly converted into
float and the result is float.
Step 3: Evaluate (f+d) where f is float and d is double, hence f is converted into
double and result is double.
Step 4: Add results from step1 and step2 and where the result of step1 is int and
step2 is float, hence convert the result of step1 is implicitly converted into float
and the result is float.
Step 5: Subtract result of step3 from result of step4 where the result of step3 is
double and the result of step 4 is float and hence convert the result of step4 is
implicitly converted into to double and the result is double.
Exercise 13: In the following program what value will be printed corresponding to
each printf() statement.
#include <stdio.h>
int main(void){
char a = 'a', ch;
int b = 90, c=10, in;
ch = b; printf ("%c\n", ch);
100
107
97
90
a
107.000000
107.000000