Introduction to Programming - (C Language) - Unit : 1 – PRECEDENCE OF OPERATORS IN C & TYPE CONVERSION AND CASTING

 

PRECEDENCE OF OPERATORS IN C

The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operators direction to be evaluated, it may be left to right or right to left.

Let's understand the precedence by the example given below:

1.     int value=10+20*10;  The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator).

The precedence and associatively of C operators is given below:

Category

Operator

Associativity

Postfix

() [] -> . ++ - -

Left to right

Unary

+ - ! ~ ++ - - (type)* & sizeof

Right to left

Multiplicative

* / %

Left to right

Additive

+ -

Left to right

Shift

<< >>

Left to right

Relational

< <= > >=

Left to right

Equality

== !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

|

Left to right

Logical AND

&&

Left to right

Logical OR

||

Left to right

Conditional

?:

Right to left

Assignment

= += -= *= /= %=>>= <<= &= ^= |=

Right to left

Comma

,

Left to right

 

TYPE CONVERSION AND CASTING

 In the C programming language, type casting and type conversion are two concepts used to change the data type of a value or variable. They allow you to convert a value from one data type to another.

Type Casting:

Type casting is a way to explicitly change the data type of a variable. It is done by using a type cast operator, which is denoted by parentheses followed by the desired data type. Here's the syntax:

(new_data_type) value;

For example, let's say we have an integer variable num and we want to treat it as a floating-point number:

int num = 10;

float result = (float) num;

In this example, we are casting the num variable to the float data type using (float). The resulting value will be stored in the result variable.

Type Conversion:

Type conversion, on the other hand, is an implicit conversion that occurs automatically when an expression involving different data types is evaluated. The compiler automatically converts one type to another based on a set of predefined rules.

Here are a few examples of type conversion:

int num1 = 10;

float num2 = 3.5;

int sum = num1 + num2;  // The float value will be implicitly converted to an int

In this example, the sum variable will hold the value 13 because the float value 3.5 is implicitly converted to an integer before performing the addition.

int num1 = 10;

char ch = 'A';

int result = num1 + ch;  // The char value will be implicitly converted to an int

In this case, the character 'A' will be converted to its corresponding ASCII value (65) before being added to num1.

It's important to note that not all conversions are possible or safe. For example, converting a large floating-point value to an integer may result in data loss. In such cases, it's recommended to use explicit type casting to ensure the desired behavior.

float num = 3.7;

int truncated = (int) num;  // Explicitly cast the float to an int

Here, the value of num will be truncated to 3, and it will be stored in the truncated variable using explicit type casting.

Type of conversions

In C, there are several types of conversions that can occur implicitly or explicitly. Here are some commonly encountered types of conversions:

Integer Promotion: Smaller integer types, such as char and short, are automatically promoted to larger integer types, such as int or long, when used in expressions. This promotion ensures that smaller types are compatible with the larger types.

Example:

char c = 'A';

int sum = c + 10; // char 'A' is promoted to int before addition

Arithmetic Conversion: When performing arithmetic operations on operands of different types, the operands are automatically converted to a common type. The rules for arithmetic conversions depend on the types involved, but generally, the conversion results in a type that can represent the largest range and precision of the operands.

Example:

int num = 10;

float result = num + 3.5; // int is converted to float before addition

Implicit Type Conversion: Implicit conversions are performed by the compiler without the need for any explicit cast operators. These conversions occur when assigning values of one type to variables of another type, or when passing arguments to functions.

Example:

int num = 10;

double result = num; // int is implicitly converted to double

Explicit Type Casting: Explicit type casting allows you to convert a value from one type to another by using a cast operator. This type of conversion is done explicitly by the programmer and may result in data loss or unexpected behavior if not used carefully.

Example:

float num = 3.7;

int truncated = (int) num; // Explicitly cast float to int

Enum Conversions: Enumerated types in C can be converted to integers implicitly. Enumerators are assigned unique integer values, and these values can be used interchangeably with integers.

Example:

enum Weekday { MON, TUE, WED, THU, FRI, SAT, SUN };

int day = MON; // Enum value is implicitly converted to int

Pointer Conversions: Pointers of one type can be explicitly converted to pointers of another compatible type using explicit type casting. This is typically done when working with void pointers or when converting between different pointer types.

Example:

int num = 10;

void* ptr = (void*)&num; // Explicitly cast int pointer to void pointer

These are some of the common types of conversions in C. It's important to understand the rules and implications of each type to ensure proper handling of data types in your programs.

Example

1.     (type)value;      

Without Type Casting:

int f= 9/4;  

printf("f : %d\n", f );//Output: 2  

With Type Casting:

float f=(float) 9/4;  

printf("f : %f\n", f );//Output: 2.250000  

Type Casting example

Let's see a simple example to cast int value into float.

#include <stdio.h>      

#include <conio.h>    

void main()

{      

clrscr();      

float f= (float)9/4;  

printf("f : %f\n", f );  

 getch();      

}  

 

Output:

f : 2.250000
Example 2:

#include <stdio.h>

int main()

{

    // Type casting

    double pi = 3.14159265359;

    int intPi = (int)pi; // Casts double to int (truncates decimal part)

 

    printf("pi as double: %lf\n", pi);

    printf("pi as int (cast): %d\n", intPi);

 

    // Type conversion

    int num1 = 5;

    int num2 = 2;

    float result = (float)num1 / num2; // Casts one operand to float for float division

 

    printf("Result of int division as float: %f\n", result);

 

    return 0;

}

 

Output:

 

pi as double: 3.141593

pi as int (cast): 3

Result of int division as float: 2.500000

 

In this example, type casting is used to change the data type of a variable temporarily, and type conversion is used to ensure that a specific operation is performed with the desired data types.

 

Comments

Popular posts from this blog

How to Get a Job in Top IT MNCs (TCS, Infosys, Wipro, Google, etc.) – Step-by-Step Guide for B.Tech Final Year Students

Common HR Interview Questions

How to Get an Internship in a MNC