Javatpoint Logo
Javatpoint Logo

Implicit type conversion in C

In the C programming language, implicit type conversion refers to the compiler's conversion of one data type into another data type while the program is being executed. Another name for it is automated type conversion.

Implicit type conversion in the C programming language is commonly referred to as "type coercion" or "type promotion". It is a function that automatically changes one data type to another without the use of explicit casting or conversion procedures. It occurs whenever a function call, assignment, or operation requiring various data types is performed. C has a set of rules for implicit type conversion to prevent data loss and unexpected outcomes from occurring during these conversions.

It is quite simple to implement implicit typecasting in a program. Using it, we can change the data type of any variable without affecting the current meaning. Automatic implicit type casting takes place. Put another way; implicit type casting carries out the conversion without changing any of the values included in the variables used by the program.

To comprehend the guidelines that implicit type casting in a C program accepts, remember the following points:

  • The lower data type will automatically be converted to the higher one if we conduct a conversion on two separate data types in a program.
  • The resultant value would be the floating data type (float) if we were to assume that we were executing a type casting operation between two different data types, such as float and int.

Many ways of occurrence of implicit type conversion:

In C programming, implicit type conversion can happen in a variety of circumstances. Here are a few of the many ways that it might:

Arithmetic Operations: Implicit conversion may occur when carrying out arithmetic operations with operands of various data types. C promotes a common operands type based on "usual arithmetic conversion" norms.

Example:

Assignment: If the data types of the variables on the right-hand side (RHS) and left-hand side (LHS) differ, implicit conversion may occur during variable assignment.

Example:

Function calls: Implicit conversion may occur when calling a function with arguments of a different data type than the function parameters. The arguments in C are automatically converted to match the parameter types.

Example:

Comparison operators: If the operands of a comparison have distinct data types, C may implicitly transform them to a common type.

Example:

Mixed Data Type Expressions: Implicit conversion may occur when expressions contain mixed data types. The operands are promoted to a common type using "usual arithmetic conversion" in C.

Example:

Conditional Operator (Ternary Operator): The ternary operator necessitates that the types of both of its outcome expressions match. To ensure compatibility, C implicitly converts one type to another if they have different types.

Example:

Integral to Floating-Point Conversion: C implicitly transforms an integer to a floating-point number when used in an expression with a floating-point number.

Example:

Floating-Point to Integral Conversion: Similar to how Java implicitly converts floating-point numbers into integers; C does the same when a floating-point number is used in an expression with an integer.

Example:

Integral promotion: When expressions with bigger integer types (int, long) and smaller integer types (char, short), their smaller types are promoted to the bigger type before the operation begins.

Example:

Conditional Expression in if and while statements: When using conditional expressions with distinct data types in if and while statements, implicit conversion may occur in such statements' conditions.

Example:

Bitwise Shift Operators: Implicit conversion may occur when using the left shift << and right shift >> on operands of different integer types.

Example:

Boolean Expressions: Implicit conversion may occur when employing boolean expressions (logical AND && and logical OR ||) with operands of various data types.

Example:

Combining Signed and Unsigned Types: Implicit conversion may occur when signed and unsigned integer types are combined in expressions or assignments.

Example:

Function Return Types: When a function returns a value of a different data type than the caller anticipated, implicit conversion can occur. The implicit conversion of the return value to the anticipated data type occurs.

Example:

Program:

Let's take a program to understand the use of implicit type conversion in C.

Output:

intValue is greater than or equal to floatValue.
Congratulations! You passed the exam.
Result 1: 8.140000
Double age: 25.000000
Area: 314.159000
Result 2: 2.857143
Result 3: 5.000000Result 4: 40
Result 5: 5
Result 6: 5.000000
Size of num2: 4

Explanation:

  • We include the required header file h to our example to use the standard input/output routines.
  • We specify the main function, which is where our program begins.
  • Examples of several situations where implicit type conversion occurs are provided inside the main function.
  • After that, we set the value of the float variable pi to 14 and the value of the integer variable num to 5. The float variable result1 stores the outcome of the addition operation num + pi after implicitly converting the integer num to a float before the addition.
  • We set the value of the integer variable age to 25. After that, we provide the double variable doubleAge the age value. In this case, the assignment implicitly converts the integer age to a double.
  • We create the function calculateArea, which computes the area of a circle given a double input radius. We call calculateArea from the main function with the integer value radius = 10. Before calling the function, a double is implicitly created from the radius value.
  • We set the value of the floatValue variable to 5 and the value of the integer variable intValue to 15. Before converting the integer intValue to a float, we contrast intValue with floatValue in the if statement.
  • We define a float variable, y, with a value of 5, and an integer variable, x, with 10. The integer x is converted to a float before the division when we perform the division operation x / y, and the outcome is kept in the float variable result2.
  • We declare a float variable floatValue2 with a value of 5 and an integer variable value with a value of 5. It is known as a ternary operator. The integer value is implicitly changed to a float in the expression value > 3? value: floatValue2 of the ternary operator.
  • We define a float variable passing Score with a value of 0 and an integer variable score with a value of 85. The integer score is implicitly transformed to a float before the comparison in the if statement condition, score >= passing score.
  • We define an unsigned int variable shift amount with a value of 2 and an integer variable intValue2 with a value of 10. The integer intValue2 is implicitly transformed to an unsigned int before the left shift operation in the equation intValue2 shift amount.
  • We declare two integer variables: signedNum, which has the value -5, and unsignedNum, which has the value 10. The integer signedNum is implicitly transformed to an unsigned int for the addition when we combine signedNum and unsignedNum.
  • The calculateSum function is defined to take two integer inputs and return the sum of those two values. We call calculateSum(3, 2) inside the main function and put the result's value in the float variable result6. Here, a float is implicitly created from the function's return value, which is an integer.
  • We declare the integer variable num2 and give it the value of 5. The integer num2 is implicitly transformed to size_t for the sizeof operator in the statement sizeof(num2) using the sizeof operator.
  • Finally, we use printf statements to output each operation's results.

NOTE: C has an implicit type conversion feature that automatically changes data types while performing operations or assignments. For creating effective C programs, it's crucial to comprehend these conversions. Implicit type conversion must be used carefully, as it occasionally produces unexpected outcomes or data loss. When you require additional control over the conversion or to make your code more readable and bug-resistant, explicit type casting should be used.

Complexity Analysis:

Time Complexity:

The amount of statements that are run sequentially in the main function is the key factor in determining the time complexity of the program. The program comprises numerous arithmetic operations, assignments, function calls, comparisons, bitwise shifts, and print statements. Each of these processes typically has a constant time complexity, which implies that regardless of the input quantity, they take the same amount of time.

Since the program's overall time complexity is O(1), its execution time is constant and unaffected by the input size or any iterative loops.

Space Complexity:

The memory required to store variables and function call frames in the stack determines the program's space complexity. The program uses numerous variables of various data types, including integers, floats, doubles, and size_t. Each variable's space complexity is fixed because it is independent of input size.

Because of this, the program's overall space complexity is O(1), which means that regardless of the amount of input, memory usage is constant.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA