You are on page 1of 12

Birla Institute of Technology & Science, Pilani

Second Semester 2015-2016, Computer Programming [CS F111]


Week #2
Tutorial Sheet #2 Date:___/_____/________
Name: ______________ _____________ID: ______________________ Lec Sec/Lab
Sec:______/_____

Section 1 Writing your first C program:


/* Authors: Aruna, Tathagat Ray, Anand Magadi Narasimhamurthy
This program demonstrates how to print a simple message */
#include<stdio.h>
main()
{
printf(This is my first program);
}
The first two lines of the above program are comments (written between /* */)
which basically is used for documentation to convey who are authors, what the
program does you may add details as to when it was written etc.
The third line in the program is called the preprocessor statement and is used to
because the program uses a predefined C function namely printf.
The fourth line main() is used to begin the program, where main() is the name of
this function we are about to write and the uniqueness of this function is that every
C program must have a main function.
The flower brackets { } indicate begin and end of the main function.
The sixth line printf is a predefined function in C library namely stdio.h and the aim
of this function is print the values depending upon what is written inside the
brackets.
Exercise 1: Write a program to print the message Welcome to C programming.
/* Authors: Aruna, Tathagat Ray, Anand Magadi Narasimhamurthy
This program demonstrates how to print a simple message */
#include<stdio.h>
main()
{
printf(Welcome to C programming);
}

Section 2: Data Types:


Consider the following expression to calculate the area of a circle Area=3.142 X
Radius X Radius. In order to calculate the Area one needs to know the value of the
Radius and we replace the Radius with the actual value and then compute the Area.
In C programming the actual value given is stored in the memory as sequence of
bytes which we have seen in the previous tutorial. This memory is given a unique
name which is termed as variable. The memory allocation varies depending upon
the type of value stored in the memory. For example you may want to store integers
or real numbers or characters. To cater to this need C language supports the a few
datatypes which are shown in the table along with how much memory is allocated
to store them.
Type

Size (bytes)

char, unsigned char

short int, unsigned short int

int, unsigned int, long int, unsigned long int

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.

int a; int b; int c;


this is same as
int a, b, c;
float d; float e; float f;
this can be written as
float d, e, f;
short int a, b, c ;
this can be written as
short a, b, c;
long int r, s, t ;
this can be written as
long r, s, r;

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;

Section 3: Operators, and expressions:


The symbols which are used to perform logical and mathematical operations in a C
program are called C operators.
Types of operators
C language offers many types of operators. They are,

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

Example Evaluate the following expressions


int i=8, j=5;
float x = 0.005, y = - 0.01;
char c = c, d = d;
Example: (3 * i 2 * j ) % ( 2 * d c)
The expression is treated as (3*8-2*5)%(2*100-99)
Note: the ASCII of character d id 100 and c is 99. Due to brackets the
subexpressions have to be evaluated first.
Step1: 3*8=24
Step2: 2*5=10
Step3: 24-10=14 is evaluated due to brackets
Step4: 2*100=200
Step5: 200-99=101
Step6: 14%101 = 14
Exercise5: using the same values of i,j,x,y,c and d given in the example
above evaluate the following
(i) 2 * ( ( i / 5 ) + ( 4 * ( j 3 ) ) % ( i + j 2 ) )
6

(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 )

Exercise7: Construct a logical expression to represent each of the following


conditions
a. marks is greater than or equal to 60 but less than 70
(marks>=60
&&
marks<70)
b. answer is either Y or y
== y)

( answer == Y || answer

c. n is odd and n is not 7


&& n != 7 )

( ( n % 2 ) == 1

d. c is a lowercase or uppercase letter


<=122 )||(c>=65 && c<=90))

(( c >= 97 && c

e. goldsponsor is in the range 5000-7000


&& goldsponsor <= 7000

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;

Other assignment operators in C language are given below.


Operators
Simple assignment
operator
Compound
assignment
operators

=
+
=
-=
*=
/+
%
=
&
=
^
=

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

5. Increment operators and Decrement Operators


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 C
programs.
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: -var_name; (or) var_name -;

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

b) If in the second statement a=0 is replaced by a==0 what will be output?


a=2

c) If in the second statement a=0 is replaced by a == -1 what will be output?


a=3
Exercise11: if int i=10, m=10 ; what value will be assigned to I after executing the
following statement?
i = i > m ? i*i , 20;
i=20

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.

Birla Institute of Technology & Science, Pilani


Second Semester 2015-2016, Computer Programming [CS F111]
Week #2
Tutorial Sheet #2 Date:___/_____/________
Name: ______________ _____________ID: ______________________ Lec Sec/Lab
Sec:______/_____

Section 4 Precedence and Associativity:


Precedence: Operator precedence determines the order in which operators are
evaluated. Operators with higher precedence are evaluated first.
For example: 3+4*5=23
Since * have higher precedence over + 4*5 is evaluated and then 3+20 is 23.

Associativity: The associativity of an operator is a property that determines how


operators
of
the
same precedence are
grouped
in
the
absence
of
parentheses. Associativity is only needed when the operators in an expression have
the same precedence. When an expression has two operators with the same
precedence, the expression is evaluated according to its associativity.

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

()
[]
.
->
++ --

Parentheses (function call)


Brackets (array subscript)
Member selection via object name
Member selection via pointer
Postfix increment/decrement

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

<< >>

Bitwise shift left, Bitwise shift right

left-to-right

<
<=
> >=

Relational less than/less than or equal to


Relational greater than/greater than or
equal to

left-to-right

== !=

Relational is equal to/is not equal to

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

Comma (separate expressions)

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

x= a-b / (3+c) * (2-1);

//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;

// stmt 6: here x is not initialized ... so garbage

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

Section 5 Type Casting:


Typecasting is a way to make a variable of one type, such as an int, act like another
type, such as a char, for one single operation. To typecast something, simply put the
type of variable you want the actual variable to act as inside parentheses in front of
the actual variable. (char)a will make 'a' function as a char.

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);

printf ("%d\n", ch);


ch = b+c; printf ("%c\n", ch);

printf ("%d\n", ch);

100

in = a + c; printf ("%d\n", in);

107

printf ("%c\n", in);

ch = (char) a+c; printf ("%d\n", a);

97

printf ("%c\n", a);


printf ("%f\n", (float) (a+c));
printf ("%f\n", a+c);
return 0;
}

90

a
107.000000
107.000000

You might also like