Thursday 5 December 2019

C Tutorial - Type Conversion or Modifier

Type modifier

The basic data types may have modifier preceding them to indicate special properties of the object being declared.

These modifiers change the meaning of the basic data types to suit the specific needs.

These modifiers are unsigned, signed, long and short. It is also possible to give these modifiers in combination, e.g., unsigned long int.

eg:-
Modifier for char Data Type

main()
{
char ch=291;
printf("%d\t%c\n",ch,ch);
}

output:- 35

Here ch has been defined as a char ,and char cannot take a value bigger than +128.That is why assigned value of ch is 291 and is considered to be 35 (291-128-128).

Data type
Range
Bytes occupied
Format
signed char
-128 to +127
1
%c
unsigned char
0 to 255
1
%c
short signed int
-32768 to 32767
2
%d
short unsigned int
0 to 65535
2
%u
long signed int
-2147483648 to +2147483647
4
%ld
long unsigned int
0 to 4294967295
4
%lu
float
-3.4e38 to 3.4e38
4
%f
double
-1.7e4932 to +1.7e308
8
%lf
long double
-1.7e4932 to 1.7e4932
10
%lf


Evaluation of expression

An expression is a combination of variables, constants and operators arranged according to the syntax of the language.

C can handle any complex expression with ease.

It is little bit different from algebraic expression.

Algebraic Expressions
C Expressions
axb-cxd
a*b-c*d
(m+n)(a+b)
(m+n)*(a+b)

Evaluation of expression:

We can evaluate an expression by using assignment statement. As

Variable = Expression.

e.g. :

Temp = ((f * cos(x)/sin (y))+(g * sin(x)/cos(y)))

All relevant variables must be assigned values before the evaluation of the expression.

Type conversion in expression:

To effectively develop C programs, it would be necessary for you to understand the rules that are used for the implicit conversion of operands.

If an expression contains an operation between an int and a float, the int would be automatically promoted to a float before carrying out of operation.

Automatic type conversion

If the operands are of different types the lower type is automatically converted to the higher typebefore the operation proceeds
The result is of the higher type

Given below is the sequence of rules that are applied by evaluating expressions.

Operator 1
Operator 2
Result
Long Double
any
Long Double
Double
any
Double
Float
any
Float
Unsigned Long Int
any
Unsigned Long In
Long Int
any
Long Int
Unsigned Int
any
Unsigned Int

Final result of an expression to the type of the variable on the left of the assignment signed before assigning the value to it.

However, the following changes are introduced during the final assignment:

1. Float to Int causes truncation of the fractional part.

2. Double to float causes rounding of digits.

3. Long int to int causes dropping of the excess higher order bits

Type Casting:

Casting a value is a forcing a type conversion in a way that is different from the automatic conversion and this process is called type cast.


Type Definition using typedef

C allows us to create data types via the typedef statement.

The format of typedef statement is:

typedef data type new_type_name;

Example:

typedef int units;
units bat1,bat2; /*this statement is equivalent to int bat1,bat2*/


Previous Page                                       Next Page

More Topics : 

100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...