i ) d = i; if (i > l) l = i; if (d == l) d *= 2;. Although d, l and i belong to different data types, they will be automatically converted to equal data types each time a comparison or assignment is executed. Some of these conversions may imply a loss of precision, which the compiler can signal with a warning. This warning can be Assignment operator: allow implicit conversion from a particular type on assignments. Type-cast 1 2 3 4, double x = ; int y; y = int (x); // functional notation y = (int) x; // c-like cast notation.">
1 Mazurr

C Implicit Cast Assignment

Because implicit type conversions are usually unsafe, and C++ takes a more safe stance to typing than C does.

C will usually allow implicit conversions, even if most chances are that the conversion is an error. That's because C assumes that the programmer knows exactly what they are doing, and if not, it is the programmer's problem, not the compiler's problem.

C++ will usually disallow things that could potentially be errors, and require you to explicitly state your intention with a type cast. That's because C++ is trying to be programmer-friendly.

You might ask how come it is friendly when it is actually requiring you to type more.

Well, you see, any given line of code, in any program, in any programming language, will generally be read many more times than it will be written (*). So, ease of reading is much more important than ease of writing. And when reading, having any potentially unsafe conversions stand out by means of explicit type casts helps to understand what is going on and to have a certain level of certainty that what is happening is in fact what was intended to happen.

Besides, the inconvenience of having to type the explicit cast is trivial compared to the inconvenience of hours upon hours of troubleshooting to find a bug which was caused by a mistaken assignment that you could have been warned about, but never were.

(*) Ideally it will be written only once, but it will be read every time someone needs to review it to determine its suitability for reuse, and every time there is troubleshooting going on, and every time someone needs to add code near it, and then every time there is troubleshooting of nearby code, and so on. This is true in all cases except for "write-once, run, then throw away" scripts, and so it is no wonder that most scripting languages have a syntax which facilitates ease of writing with complete disregard to ease of reading. Ever thought that perl is completely incomprehensible? You are not alone. Think of such languages as "write-only" languages.

Implicit Type Conversion in C

As already discussed we can mix different kinds of data types and constants in an expression. In such expressions operand of one data type is converted to another data type. This process is known as Type Conversion.

There are two types of type conversion:

  1. Implicit type conversion.
  2. Explicit type conversion.

Implicit type conversion #

This type of conversion is done by the compiler according to the following rules:

  1. All operands of type or will be converted to int before any operation and the result will be in too.
  2. If one operand is of type , then the other operand will be converted to and then the result will be in too.
  3. Otherwise, If one operand is of type then the other operand will be converted to and the result will be in .
  4. Otherwise, If one operand is of type then the other operand will be converted to and the result will be in .
  5. Otherwise, If one operand is of type then the other operand will be converted to and the result will be in .
  6. Otherwise, If one operand is of type then the other operand will be converted to and the result will be in .
  7. Otherwise, If one operand is of type then the other operand will be converted to and the result will be in .
  8. Otherwise, If one operand is of type then the other operand will be converted to and the result will be in .

Let's take some examples to make everything clear.

Example 1:

Here one operand is of type and other is of type . Therefore according to rule 3, the variable a will be converted to . Hence the result of the overall operation is i.e .

Example 2:

Here according to rule 1, will be converted to before any operation and the result of the overall expressions will be of type . Since the integral value of is (i.e ASCII value of the character ). Hence .

Example 3:

Here according to rule 7, variable of type will be first converted to i.e (ASCII value of ), before addition will be carried out. And the result of the whole operation will be an . Hence .

Here are some more examples:

All the above-mentioned rules can be simplified by assigning a rank to each type. Here is how it works.

Whenever two operands of different data types are involved in an expression, the operand of lower rank will be converted to a data type of higher rank. This process is called the promotion of type.

Type Conversion in assignment #

If types of the operand in an assignment expression is different, then the operand on the right-hand size will be converted to the type of left-hand operand according to the following rules.

  1. If types of the operand in an assignment expression is different, then the operand on the right-hand size will be converted to the type of left-hand operand according to the following rules.

    If the right-hand operand is of lower rank then it will be promoted to the rank of left woaknb.wz.sk's take an example:

    here right operand is of type and the right-hand operand is of type . According to rule lower rank operand (in this case ) will be promoted to a higher rank (in this case ). So before assignment i.e (ASCII value) will be promoted to and then assigned to .

  2. Otherwise, if right-hand operand is of higher rank then it will be demoted to the rank of left-hand operand. For example:

    Recall that by default floating point constants are of type . In this case, right-hand operand i.e is of type and the left-hand operand is of type . Therefore before assignment operation will be demoted to type and then the assignment will take place.

Some consequences of type conversion in assignment are:

  1. High order bits may be lost when is converted to or to or to . Let's take an example to understand this point clearly. Suppose we have the following statement.

    Here we are trying to assign ( type) to a variable. According to type conversion rules: data type of higher rank will be converted to data type lower rank. But there is a problem, recall that type can only take values from to . Cleary is out of range for variable . In such cases, an appropriate value from the other side of the range is picked up and stored in . So eventually what gets stored variable is a smiley character having ASCII value .

  2. Fractional part will be truncated during conversion from floating point type (like , ) to type.

  3. When the type is converted to type digits are rounded off.

  4. When is converted to or to there is not increase in accuracy.

  5. When type is changed to type, the sign may be dropped.

The following example demonstrates how type conversion takes place.

Expected Output:

Leave a Comment

(0 Comments)

Your email address will not be published. Required fields are marked *