Type casting, C language Assignment Help

Assignment Help: >> Introduction to C language >> Type casting, C language

Type casting

Sometimes we need to force the compiler to explicitly convert the value from one type to another where the compiler will not do it automatically.

float result;

result = (float) 21/5;

Here the variable result is a float type and in the expression the division is in forcing to return a float value while both operands are of type integer. In this example the cast operation explicitly converts 21 to a float  and causes the division to be carried out as a floating-point division.

                char ch = 'A';

                int x, y = 5, z = 9;

                float f;

                x = (int)ch;

                f = (float) y * z;

 

Comparison Between Implicit Typecasting and Explicit Typecasting

Implicit typecasting

Explicit typecasting

(1)    It is done by compiler automatically.

(a)     It is done by programmer.

(2)    Needs less coding,

(b)   Need extra coding.

(3)    In general, implicit tasking is employed automatically.

(c)    In general, implicit tasking is employed By programmer for specific purpose.

(4)    The programming environments attributes, initial values are assigned by the system.

(d)   The programming environments attributes, initial values are assigned and set by the programmer.

(5)    No skills are required, in this case for the programmer

(e)   High skills and complete understanding of the program and operating system are the needs of the programmer

 

Rules for Evaluation of Expression

1.       First, parenthesized sub expression from left to right is evaluated.

2.       If parenthesis is nested, the evaluation begins with the innermost sub-expression.

3.       The precedence rule is applied in determining the order of application of operators in evaluating sub expression.

4.       The associativity rule is applied when two or more operators of the same precedence level appear in a sub expression.

5.       Arithmetic expressions are evaluated from left to right using the rules of precedence.

6.       When parenthesis is used, the expressions within parenthesis assume highest priority.

 

Operator Precedence and Associativity

Each operator in C has a precedence associated with it. This precedence is used to determine how an expression involving more than one operator is evaluated. There are distinct levels of precedence and an operator may belong to one of these levels. The operators at the higher level of precedence are evaluated first. The operators of the same precedence are evaluated either from left to right and from right to left depending on the level. This is known as the associativity property of an operator.

Operators

Description

Associativity

Priority

()

[]

Function Call

Array element reference

Left to right

 

1

+

-

++

--

!

~

*

&

sizeof

(type)

Unary plus

Unary minus

Increment

Decrement

Logical negation

Ones Complement

Pointer reference (indirection)

Address

Size of an object

Type cast (Conversion)

 

Right to Left

 

2

*

/

%

Multiplication

Division

Modulus

Left to right

3

+

-

Addition

Subtraction

Left to right

4

<< 

>> 

Left Shift

Right Shift

Left to right

5

<=

>=

Less than

Less than or equal to

Greater than

Greater than or equal to

Left to right

6

==

!=

Equality

Inequality

Left to right

7

&

Bitwise AND

Left to right

8

^

Bitwise XOR

Left to right

9

|

Bitwise OR

Left to right

10

&&

Logical AND

Left to right

11

||

Logical OR

Left to right

12

?:

Conditional expression

Right to left

13

=

*=, /=, %=

+=, -=, &=

^=, |=

<<=, >>=

Assignment operators

Short hand operators

Right to left

14

,

Comma operator

Left to Right

15

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd