14)
TYPE CONVERSION
Converting an expression of a given type into another type is known as type Conversion.
They are of two types:
1. Implicit type Conversion
2. Explicit type Conversion
Implicit type Conversion
Implicit conversions do not require any operator. They are automatically performed when a
value is copied to another compatible type.
Example: short a=2000;
int b;
b=a;
Here, the value of a has been converted from short to int and we have not specified any type
Conversion operator. This is known as a standard conversion. Standard conversions affect
fundamental data types, and allow conversions such as the conversions between numerical
types such as short to int, int to float, double to int and so on and some pointer conversions.
Some of these conversions may imply a loss of precision.
Explicit Type Conversion
Explicit conversion happens when the user forces the conversion from one type to another.
C is a strong typed language. Many conversions, especially those that imply a different
interpretation of the value, require us to do explicit conversion.
In C the term applies to data conversions specified by the programmer, as opposed to the
automatic data conversions we just described. Sometimes a programmer needs to convert a
value from one type to another in a situation where the compiler will not do automatically. To
cast data from one type to another you specify the new type in parenthesis before the value
you want to be converted.
For example, to convert an integer count to float, the cast expression will be;
(float) count.
Suppose you want to cast the product of two integers to a float, you would code
(float) (num1 * num2)
Suppose an intermediate result won't fit the unsigned type either. In such a case we might be
able to solve the problem by using cast.
Example : Program to demonstrate the type conversion
#include<stdio.h>
main()
{
Output
printf(“ 5/2 = %d \n”, 5/2); 5/2 = 2
printf(“ 5.0/2 = %f \n”, 5.0/2); 5.0/2 = 2.5
printf(“ 5/2 = %f \n”, (float)5/2);
} 5/2 =2.5
15) Explain the difference between call by value and call by reference with a example for each
In a programming language, one can invoke functions in two ways: known as Call by Value and
Call by Reference. The general basis of differentiation in these two ways is the type of values
that passes through them in the form of parameters.
The parameters that pass to the functions are known as actual parameters, and the parameters
that the functions receive are known as formal parameters.
What is Call by Value?
In this particular parameter passing method, the values of the actual parameters copy into the
function’s formal parameters. It stores both types of parameters in different memory locations.
Thus, if one makes any changes inside the function- it does not show on the caller’s actual
parameters.
This method passes a copy of an actual argument to the formal argument of any called
function. In the case of a Call by Value method, any changes or alteration made to the formal
arguments in a called function does not affect the overall values of an actual argument. Thus, all
the actual arguments stay safe, and no accidental modification occurs to them.
Example
#include <stdio.h>
void swapx(int x, int y);
int main()
{
int a = 10, b = 20;
swapx(a, b); // Actual Parameters
printf("In the Caller:\na = %d b = %d\n", a, b);
return 0;
}
void swapx(int x, int y) // Formal Parameters
{
int t;
t = x;
x = y;
y = t;
printf("Inside Function:\nx = %d y = %d\n", x, y);
}
What is Call by Reference?
In this case, both the formal and actual parameters refer to a similar location. It means that if
one makes any changes inside the function, it gets reflected in the caller’s actual parameters.
This method passes the address or location of the actual arguments to the formal arguments of
any called function. It means that by accessing the actual argument’s addresses, one can easily
alter them from within the called function. Thus, in Call by Reference, it is possible to make
alterations to the actual arguments. Thus, the code needs to handle the arguments very
carefully. Or else, there might be unexpected results and accidental errors.
Example
#include <stdio.h>
void swapx(int*, int*);
int main()
{
int a = 10, b = 20;
swapx(&a, &b); // Actual Parameters
printf("Inside the Caller:\na = %d b = %d\n", a, b);
return 0;
}
void swapx(int* x, int* y) // Formal Parameters
{
int t;
t = *x;
*x = *y;
*y = t;
printf("Inside the Function:\nx = %d y = %d\n", *x, *y);
}
Call By Value Call By Reference
While calling a function, instead of passing
While calling a function, we pass the
the values of variables, we pass the address
values of variables to it. Such functions
of variables(location of variables) to the
are known as “Call By Values”.
function known as “Call By References.
In this method, the value of each
In this method, the address of actual
variable in the calling function is copied
variables in the calling function is copied into
into corresponding dummy variables of
the dummy variables of the called function.
the called function.
With this method, the changes made to
With this method, using addresses we would
the dummy variables in the called
have access to the actual variables and hence
function have no effect on the values of
we would be able to manipulate them.
actual variables in the calling function.
In call-by-values, we cannot alter the
In call by reference, we can alter the values of
values of actual variables through
variables through function calls.
function calls.
Values of variables are passed by the Pointer variables are necessary to define to
Simple technique. store the address values of variables.
Call by value is considered safer as Call by reference is risky as it allows direct
original data is preserved modification in original data
16) C program to perform multiplication of two matrices
17)Algorithm To implement binary search. Illustrate with example
18)C program to implement Quick sort and explain with example.