KARPAGAM ACADEMY OF HIGHER EDUCATION
CLASS: I B.Sc. IT COURSE CODE: 22ITU101
BATCH-2022-2025
COURSE NAME: Problem Solving Techniques
Unit IV - Operators and Statements
UNIT-IV Syllabus
Unit IV – Operators and Statements
Importance of Casting, Different Types of Operators and their Precedence, Expressions, Con-
ditional Statements (One-Way, Two-Way and Multi-Way Conditional), Looping Statements
(For, While, do- while), Usage of Exit, continue, Break and Goto Statement.
.
Importance of Casting
What is Type Casting in C?
Type casting is the process in which the compiler automatically converts one data type in a
program to another one. Type conversion is another name for type casting. For instance, if a
programmer wants to store a long variable value into some simple integer in a program, then
they can type cast this long into the int. Thus, the method of type casting lets users convert
the values present in any data type into another data type with the help of the cast operator,
like:
(type_name) expression
Let us take a look at the following example to understand how we can utilise the cast operator
for dividing an integer variable with another one by performing it as an operation of floating-
point type:
#include <stdio.h>
main() {
int total = 17, values = 5;
double average;
average = (double) total / values;
printf(“The average of all the values available with us is : %f\n”, average );
}
The compilation and execution of the code mentioned here would generate the following
output of the program:
The average of all the values available with us is : 3.400000
You must note here that the precedence of the cast operator is much higher than that of the
division operator. Thus, the program would first convert the value of total into
the double type. After this, it will finally divide this value with the help of count yielding of
the double value.
As a matter of fact, the process of type conversion or type casting can be very implicit. It
means that the compiler is capable of performing it automatically. Conversely, we can specify
the data type explicitly using the cast operator. Both the methods are okay, but using the cast
operator whenever in need is considered a better programming practice (explicit).
Syntax for Type Casting in C
int val_1;
float val_2;
Prepared by :,S.Sumathi, Department of Computer Science, KAHE 1
, KARPAGAM ACADEMY OF HIGHER EDUCATION
CLASS: I B.Sc. IT COURSE CODE: 22ITU101
BATCH-2022-2025
COURSE NAME: Problem Solving Techniques
Unit IV - Operators and Statements
// Body of program
val_2 = (float) val_1; // type casting of the values
Types of Type Casting in C
The process of type casting can be performed in two major types in a C program. These are:
Implicit
Explicit
C Language Implicit Type Casting
Implementing the implicit type casting is very easy in a program. We use it to convert
the data type of any variable without losing the actual meaning that it holds. The implicit type
casting occurs automatically.
In simpler words, the implicit type casting performs the conversion without altering any of
the values stored in the program’s variables.
Follow these points to understand what rules the implicit type casting follows in a C program:
If we are performing a conversion on two of the different data types in a program,
then the conversion of the lower data type to the higher data type will occur
automatically.
If we suppose that we are performing the type casting operation among two different
data types like float and int, then the resultant value would be the floating data type
(float).
Examples of Implicit Type Casting in C
int x = 4;
float a = 12.4, b;
b = a / x;
In the example given above, the variable x will get converted to the float data type (float)
automatically, and it is comparatively a bigger data type in C. Here, the variable x and the
variable a would be equal in terms of their data types. Thus, the value of b would be equal to
12.4/4.0=3.1.
The Conversion of Char to Int
We can use the process of type casting to convert the character (char) data type to the int
(integer) data type in C. When we are performing a conversion between these two, the
resultant value would be an integer (int) data type. It is because the int data type is
comparatively bigger than the char data type in C.
When this conversion occurs, firstly, the value of the char data type gets converted to
its corresponding ASCII value. After this, it gets processed further accordingly.
Example
#include <stdio.h>
int main() {
int val = 10;
char x = ‘T’;
int total;
Prepared by :,S.Sumathi, Department of Computer Science, KAHE 2
, KARPAGAM ACADEMY OF HIGHER EDUCATION
CLASS: I B.Sc. IT COURSE CODE: 22ITU101
BATCH-2022-2025
COURSE NAME: Problem Solving Techniques
Unit IV - Operators and Statements
total = val + x;
printf(“In the code, we convert the char data type to int data type\n”);
printf(“The total of all these values is : %d\n”, total);
return 0;
}
The output obtained from the code mentioned above would be:
In the code, we convert the char data type to int data type
The total of all these values is : 94
In the example mentioned above, the compiler would convert the value allotted to the
char x variable to its corresponding ASCII value. Here, the ASCII value of the char x = T is
equivalent to 84. Thus, the value of total would be equal to the sum of the numbers 10 and 84.
At last, 94 would be printed as an output of the code.
C Language Explicit Type Casting
This process is not at all like the implicit type casting in C, where the conversion of
data type occurs automatically. Conversely, in the case of explicit type casting, the
programmer needs to force the conversion. In simpler words, one has to perform type casting
on the data types on their own.
Syntax:
(name_of_data_type) expression
Here, name_of_data_type refers to the name of that data type to which we want to
convert the available data type in the code. This expression can be a constant, a variable, or
even an actual expression in a program.
Examples of Explicit Type Casting in C
#include<stdio.h>
int main()
{
float num = 56.3;
int p = (int)num + 50; // data type casting explicitly
printf(“Let us understand Explicit Type Casting in C\n”);
printf(“The value of the digit used is: %f\n”, num);
printf(“The value of the variable p is: %d\n”,p);
return 0;
}
The output obtained by the code mentioned above would be as follows:
Let us understand Explicit Type Casting in C
The value of the digit used is: 56.299999
The value of the variable p is: 106
In the example mentioned above, we perform the declaration of a variable of a float type,
named num. After this, the float variable gets converted into an int (integer) data type with the
help of explicit data type casting. After this, it gets added with the number 50, and we get an
output of an integer.
Prepared by :,S.Sumathi, Department of Computer Science, KAHE 3
CLASS: I B.Sc. IT COURSE CODE: 22ITU101
BATCH-2022-2025
COURSE NAME: Problem Solving Techniques
Unit IV - Operators and Statements
UNIT-IV Syllabus
Unit IV – Operators and Statements
Importance of Casting, Different Types of Operators and their Precedence, Expressions, Con-
ditional Statements (One-Way, Two-Way and Multi-Way Conditional), Looping Statements
(For, While, do- while), Usage of Exit, continue, Break and Goto Statement.
.
Importance of Casting
What is Type Casting in C?
Type casting is the process in which the compiler automatically converts one data type in a
program to another one. Type conversion is another name for type casting. For instance, if a
programmer wants to store a long variable value into some simple integer in a program, then
they can type cast this long into the int. Thus, the method of type casting lets users convert
the values present in any data type into another data type with the help of the cast operator,
like:
(type_name) expression
Let us take a look at the following example to understand how we can utilise the cast operator
for dividing an integer variable with another one by performing it as an operation of floating-
point type:
#include <stdio.h>
main() {
int total = 17, values = 5;
double average;
average = (double) total / values;
printf(“The average of all the values available with us is : %f\n”, average );
}
The compilation and execution of the code mentioned here would generate the following
output of the program:
The average of all the values available with us is : 3.400000
You must note here that the precedence of the cast operator is much higher than that of the
division operator. Thus, the program would first convert the value of total into
the double type. After this, it will finally divide this value with the help of count yielding of
the double value.
As a matter of fact, the process of type conversion or type casting can be very implicit. It
means that the compiler is capable of performing it automatically. Conversely, we can specify
the data type explicitly using the cast operator. Both the methods are okay, but using the cast
operator whenever in need is considered a better programming practice (explicit).
Syntax for Type Casting in C
int val_1;
float val_2;
Prepared by :,S.Sumathi, Department of Computer Science, KAHE 1
, KARPAGAM ACADEMY OF HIGHER EDUCATION
CLASS: I B.Sc. IT COURSE CODE: 22ITU101
BATCH-2022-2025
COURSE NAME: Problem Solving Techniques
Unit IV - Operators and Statements
// Body of program
val_2 = (float) val_1; // type casting of the values
Types of Type Casting in C
The process of type casting can be performed in two major types in a C program. These are:
Implicit
Explicit
C Language Implicit Type Casting
Implementing the implicit type casting is very easy in a program. We use it to convert
the data type of any variable without losing the actual meaning that it holds. The implicit type
casting occurs automatically.
In simpler words, the implicit type casting performs the conversion without altering any of
the values stored in the program’s variables.
Follow these points to understand what rules the implicit type casting follows in a C program:
If we are performing a conversion on two of the different data types in a program,
then the conversion of the lower data type to the higher data type will occur
automatically.
If we suppose that we are performing the type casting operation among two different
data types like float and int, then the resultant value would be the floating data type
(float).
Examples of Implicit Type Casting in C
int x = 4;
float a = 12.4, b;
b = a / x;
In the example given above, the variable x will get converted to the float data type (float)
automatically, and it is comparatively a bigger data type in C. Here, the variable x and the
variable a would be equal in terms of their data types. Thus, the value of b would be equal to
12.4/4.0=3.1.
The Conversion of Char to Int
We can use the process of type casting to convert the character (char) data type to the int
(integer) data type in C. When we are performing a conversion between these two, the
resultant value would be an integer (int) data type. It is because the int data type is
comparatively bigger than the char data type in C.
When this conversion occurs, firstly, the value of the char data type gets converted to
its corresponding ASCII value. After this, it gets processed further accordingly.
Example
#include <stdio.h>
int main() {
int val = 10;
char x = ‘T’;
int total;
Prepared by :,S.Sumathi, Department of Computer Science, KAHE 2
, KARPAGAM ACADEMY OF HIGHER EDUCATION
CLASS: I B.Sc. IT COURSE CODE: 22ITU101
BATCH-2022-2025
COURSE NAME: Problem Solving Techniques
Unit IV - Operators and Statements
total = val + x;
printf(“In the code, we convert the char data type to int data type\n”);
printf(“The total of all these values is : %d\n”, total);
return 0;
}
The output obtained from the code mentioned above would be:
In the code, we convert the char data type to int data type
The total of all these values is : 94
In the example mentioned above, the compiler would convert the value allotted to the
char x variable to its corresponding ASCII value. Here, the ASCII value of the char x = T is
equivalent to 84. Thus, the value of total would be equal to the sum of the numbers 10 and 84.
At last, 94 would be printed as an output of the code.
C Language Explicit Type Casting
This process is not at all like the implicit type casting in C, where the conversion of
data type occurs automatically. Conversely, in the case of explicit type casting, the
programmer needs to force the conversion. In simpler words, one has to perform type casting
on the data types on their own.
Syntax:
(name_of_data_type) expression
Here, name_of_data_type refers to the name of that data type to which we want to
convert the available data type in the code. This expression can be a constant, a variable, or
even an actual expression in a program.
Examples of Explicit Type Casting in C
#include<stdio.h>
int main()
{
float num = 56.3;
int p = (int)num + 50; // data type casting explicitly
printf(“Let us understand Explicit Type Casting in C\n”);
printf(“The value of the digit used is: %f\n”, num);
printf(“The value of the variable p is: %d\n”,p);
return 0;
}
The output obtained by the code mentioned above would be as follows:
Let us understand Explicit Type Casting in C
The value of the digit used is: 56.299999
The value of the variable p is: 106
In the example mentioned above, we perform the declaration of a variable of a float type,
named num. After this, the float variable gets converted into an int (integer) data type with the
help of explicit data type casting. After this, it gets added with the number 50, and we get an
output of an integer.
Prepared by :,S.Sumathi, Department of Computer Science, KAHE 3