Type Conversions

When an operator has operands of different types, operands are converted to a common type following certain rules.

Automatic Conversion

Narrower operand is converted into wider. For example, inter is converted to float in an expression containing an integer and a floating point value.

char is a small integer hence it can be used in an arithmetical expression. s[i]-‘0‘ returns the integer value corresponding to s[i].

Implicit Conversion

  • If one operand is long double, the other is converted to long double.
  • Else if one operand is double, the other is converted to double.
  • Else if one operand is float, the other is converted to float.
  • Else if one operand is unsigned long int, the other is converted to unsigned long int.
  • Else if one operand is unsigned int and other is long int, then if long int can represent values of unsigned int, the unsigned int is converted to long int. Else both are converted to unsigned long int.
  • Else if one operand is long int, the other is converted to long int.
  • Else if one operand is unsigned int, the other is converted to unsigned int.
  • Else both operands are taken as int.

In case of an expression, the type of right side is converted to the on the left.

Explicit Conversion

Conversion can be forced on an expression, with a cast operator.

eg: sqrt((double) n)

The value of the argument become double for the function call but the type of n remains the same i.e. in this case int.

Advertisements

About Odol Shinu

I've completed my B Tech in Information Technology in 2010 from Government Engineering College Sreekrishnapuram Palakkad under Calicut University.

Posted on August 14, 2010, in C Language. Bookmark the permalink. 1 Comment.

  1. You might change this theme … it is not very readable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: