answersLogoWhite

0


Best Answer
IMPLICIT TYPE CONVERSIONImplicit type conversion, also known as coercion, is an automatic type conversion by thecompiler. Some languages allow, or even require, compilers to provide coercion.

In a mixed-type expression, data of one or more subtypes can be converted to asupertype as needed at runtime so that the program will run correctly. For example, the following is legal C language code:

double d; long l; int i; if (d > i) d = i; if (i > l) l = i; if (d == l) d *= 2;

Although d, l and ibelong to different data types, they will be automatically converted to equal data types each time a comparison or assignment is executed. This behavior should be used with caution, as unintended consequences can arise. Data can be lost when floating-point representations are converted to integral representations as the fractional components of the floating-point values will be truncated (rounded towards zero). Conversely, converting from an integral representation to a floating-point one can also lose precision, since the floating-point type may be unable to represent the integer exactly (for example, floatmight be an IEEE 754 single precision type, which cannot represent the integer 16777217 exactly, while a 32-bit integer type can). This can lead to unintuitive behavior, as demonstrated by the following code:

#include int main() { int i_value = 16777217; float f_value = 16777217.0; printf("The integer is: %i\n", i_value); printf("The float is: %f\n", f_value); printf("Their equality: %i\n", i_value == f_value); }

On compilers that implement floats as IEEE single precision, and ints as at least 32 bits, this code will give the peculiar result of printing out "The integer is: 16777217", followed by "The float is: 16777217.000000", then "Their equality: 0" (where 1 represents equal). This odd behavior is caused by an implicit cast of i_value to float when it is compared with f_value; a cast which loses precision, making the values being compared different.

Following important points:

  1. Float to int causes truncation, ie removal of the fractional part.
  2. double to float causes rounding of digit
  3. long int to int causes dropping of excess higher order bits.
EXPLICIT TYPE CONVERSIONExplicit type conversion is a type conversion which is explicitly defined within a program (instead of being done by a compiler for implicit type conversion).

double da = 3.3; double db = 3.3; double dc = 3.4; int result = (int)da + (int)db + (int)dc; //result == 9 //if implicit conversion would be used (as if result = da + db + dc), result would be equal to 10

There are several kinds of explicit conversion.

checked Before the conversion is performed, a runtime check is done to see if the destination type can hold the source value. If not, an error condition is raised.unchecked No check is performed. If the destination type cannot hold the source value, the result is undefined.bit pattern The raw bit representation of the source is copied verbatim, and it is re-interpreted according to the destination type. This can also be achieved via aliasing.

In object-oriented programming languages, objects can also be downcast : a reference of a base class is cast to one of its derived classes.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

int i= 3;

double d;

An example could help:

d= i; /* implicit */

d= (double)d; /* explicit */

Explicit conversion is necessary in some cases, example:

d= ((double)i)/2; /* d= 1.5 */

d= i/2; /* d= 1 !!! */

printf ("%g", (double)i); /* prints 3.0 */

printf ("%g", i); /* prints... something invalid */

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is implicit and explicit data type conversion in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is implicit and explicit data type conversion in C programming?

Implicit data type conversion happens automatically by the compiler when a value is assigned to a different data type. Explicit data type conversion, on the other hand, is done by the programmer using type casting to convert a value from one data type to another. It gives the programmer control over how the conversion is done.


Implict and empilet of type converision?

An implicit type of conversion does not need a special syntax in the source code, an explicit conversion makes use of the conversion keyword.


What is the Difference between implicit and explicit variable declaration?

Implicit and explicit determine what can be passed to a method. If a method is not declared as explicit the compiler will attempt to look for any implicit conversions from the type being passed to the type the method expects. for example, if the method expects a long and you pass in an unsigned char, the compiler will not complain because an unsigned char can be implicitly converted to a long without any loss of data. If you declare the method as explicit the data type that is used in the method declaration is the data type that needs to be passed to the method. If you want to pass a char *ptr to a method that expects a long you will have to cast the char *ptr to a long when calling the method. For example, foo((long)ptr);


What does implicit deny means?

The phrase implicit deny means that something is completely denied unless it has explicit permission. For instance, the traffic in a network.


Explain about type casting in java?

Type casting means explicitly converting one data type to another. For example, the following won't be allowed:int a;long b;b = 5;a = b;In the last line, the compiler will complain, due to a possible data loss - long has a larger range. But if you believe that the conversion won't cause a problem in your program, you can override the error message with an explicit conversion (or typecast):...a = (int) b;Type casting means explicitly converting one data type to another. For example, the following won't be allowed:int a;long b;b = 5;a = b;In the last line, the compiler will complain, due to a possible data loss - long has a larger range. But if you believe that the conversion won't cause a problem in your program, you can override the error message with an explicit conversion (or typecast):...a = (int) b;Type casting means explicitly converting one data type to another. For example, the following won't be allowed:int a;long b;b = 5;a = b;In the last line, the compiler will complain, due to a possible data loss - long has a larger range. But if you believe that the conversion won't cause a problem in your program, you can override the error message with an explicit conversion (or typecast):...a = (int) b;Type casting means explicitly converting one data type to another. For example, the following won't be allowed:int a;long b;b = 5;a = b;In the last line, the compiler will complain, due to a possible data loss - long has a larger range. But if you believe that the conversion won't cause a problem in your program, you can override the error message with an explicit conversion (or typecast):...a = (int) b;


What do you mean by type conversion in c plus plus?

A no converting type cast is a type cast where the underlying data type is intrinsically the same as the type being cast, thus no conversion is required. This will often occur when explicitly converting from one type definition to another when both are in fact the same definition. For instance, type casting from an unsigned int to a size_t is a no converting type cast because the size_t data type is merely a type definition (typedef) for an unsigned integral. Thus type casting between any of the following would result in a no converting type cast: unsigned unsigned int unsigned long unsigned long int size_type size_t Note that unsigned and long are both modifiers, not types. If you omit the type, int is assumed, thus the first four are fundamentally unsigned int. Note also that a conversion and a type cast are not really the same thing, but they do similar jobs. A conversion is an implicit conversion between types while a type cast is an explicit conversion between types. In other words, they're both conversions, but not all conversions are type casts. This is best demonstrated with some examples: int i = 3; In the above example, the literal constant 3 is implicitly int, thus this is an example of a no conversion type cast (same fundamental type). long j = long (i); The above example is also a no converting type cast because int and long are fundamentally the same type. Remember, long is a modifier, not a type in its own right, thus long int is implied, and a long int is fundamentally the same as an int. unsigned int k = i; The above example is an implicit conversion because the value of i (3) is assigned to a fundamentally different type to that of i. unsigned int l = (unsigned int) i; unsigned int m = static_cast<unsigned int> (i); The above examples are explicit conversions (type casts). Both explicitly cast the integral 3 to an unsigned int. Explicit conversion (type casting) between primitive types is usually unnecessary because the compiler knows how to convert between these types and it's generally not something the programmer need overly concern themselves with. However, it can sometimes be important to highlight the fact that a conversion is taking place. In these cases it is best to be explicit and the static_cast operator is the best way of making the cast stand out. Conversion to or from more complex objects should be handled by the object's themselves through class conversion operators that implement static_cast where appropriate, and dynamic_cast or reinterpret_cast when the conversion is more complex. Again, it can be important to highlight the fact a conversion is taking place so it's best to keep conversion operators to the absolute minimum and force consumers to be explicit with their conversions. You can also use const_cast to highlight the fact that the constness of a type is being converted.


What is an example of a data type conversion function?

toReal


What is the difference between automatic type conversion and explicit type conversion?

In programing languages automatic type conversion occurs when the language automatically changes a variable between different types for you. For example changing a number type to a string type. It usually does this because the type you are using is incompatible with the operation you are trying to preform, so it converts it to a new type which is compatible for you. Explicit conversion requires that you, the programmer, explicitly code the details on how the conversion is to take place. While automatic type conversion is convenient the conversion may not perform exactly how you expect. For example converting between a floating point number , e.g. 3.5 to an integer - will the system round the floating point number to 4 , or will it truncate the number to 3?


What happens when you perform arithmetic operations mixing different types of variables in java programming?

Java performs an implicit conversion to a unifying type.


What is coercion in php?

Coercion, type coercion or type conversion is the process of altering the data type of a variable into another data type. PHP uses implicit type conversion (aka. Weak/Loose Typing) so it is not necessary to declare the type of a variable. For instance, the following will not produce any errors: <?php $foo = "A string"; $foo = 1; $foo = array(); ?> You can however explicitly cast variables to a certain type: <?php $int = (int)4.5/2; ?> In contrast, strongly typed languages such as C/C++/Java force you to declare the type of value a variable will hold and explicitly state the new type to perform a conversion; not doing so will result in a compiler error.


Why is data conversion necessary?

Data conversion is a process where information or code is converted from one type of data to another. This is useful in many circumstances. If one computer uses an ASCII type encoding, and another uses another type of encoding, the two computers will be able to communicate with each other effectively.


What is a conversion function?

As the name suggests, a conversion function is a function that converts a value from one type to another. Many such conversions are either implicit or built-in operations, such as when converting from an int to a double. However, when converting between user-defined types, or between a user-defined type and a built-in type, we must write a function to explicitly perform the conversion for us. In object-oriented languages, we rely on conversion constructors and conversion operators to perform these conversions implicitly, but in C we must explicitly call the appropriate conversion functions.