Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

C PROGRAMMING ASSIGNMENT 1

NAME: - MADDI ABHILASH


CLASS: - 1MCA
REG NO: - 1947234
C PROGRAMMING
CIA 1
C PROGRAMMING ASSIGNMENT 2

Associativity of Operators
Associativity is concept by which we resolve the concept between priorities of operators.

Associativity is used when two operators of same precedence appear in an expression. Associativity
can be either Left to Right or Right to Left.

For example: - Multiplication and Division have same priority and both have left to right
associativity then how can we resolve this problem.

Given Expression: - 4*2/3=?

Rules:-

1) Left to right associative means that left operand must be unambiguous (non-confusion) i.e.
it must not involve any further sub expression evaluation.
2) Right to left associative means that right operand must be unambiguous (non-confusion)
i.e. it must not involve any further sub expression evaluation.

To solve this kind of problem use this method:

Create a table to resolve and understand this concept

Operator left right remark

* 4 2*3 Left is unambiguous

/ 4*2 3 Right is unambiguous

As both enjoys left to right associativity, first * operations would be evaluated as its left part of
unambiguous.

So, 4*2/3 (4*2)/3 = 2.6.

Program to show associativity operators


#include<stdio.h>

main()

int x;

x=10/(5*2);

printf("value of x is %d",x);
C PROGRAMMING ASSIGNMENT 3

Output:-

Priority of Operators
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others.

For example, the multiplication operator has a higher precedence than the addition operator.

Precedence of operators comes into picture when in an expression we need to decide which
operator will be evaluated first. Operator with higher precedence will be evaluated first.

Example:

2+3*5

2+ (3*5) =17. (2+3)*5 = 25

Note: - precedence of multiplication is greater than addition


C PROGRAMMING ASSIGNMENT 4

Program to demonstrate the precedence or the priorities of the operators

#include<stdio.h>

int main()

int a,b,c,d,e;

a=10,b=5,c=5,d=10;

e=a/b+c+d;

printf("Result of the expression is:%d",e);

Output:-
C PROGRAMMING ASSIGNMENT 5

C - Type Casting
Implicit Type Conversion
Implicit type casting means conversion of data types without losing its original meaning. This type
of typecasting is essential when you want to change data types without changing the significance of
the values stored inside the variable.

Implicit type conversion happens automatically when a value is copied to its compatible data type.
During conversion, strict rules for type conversion are applied. If the operands are of two different
data types, then an operand having lower data type is automatically converted into a higher data
type. This type of type conversion can be seen in the following example.

Program to demonstrate Implicit Type Conversion


#include<stdio.h>
int main()
{
int b=2;
float x = 4.5;
double a,y = 15.5;
long int z = 40;
a = z/b+b*x-y;
printf("Answer=%f\n", a);
return 0;

}
C PROGRAMMING ASSIGNMENT 6

Output:-

Explicit Type Conversation

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.

Program to demonstrate Explicit Type Conversion

#include<stdio.h>

int main()

int a,b;

float x;
C PROGRAMMING ASSIGNMENT 7

a=10;

b=4;

x=a/(float)b;

printf("Answer=%f\n", x);

return 0;

Output:-

You might also like