
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What are implicit and explicit type conversions in C language?
Converting one data type into another data type is called type conversions.
- Implicit type conversion
- Explicit type conversion
Implicit type conversion
The compiler provides implicit type conversions when operands are of different data types.
It is automatically done by the compiler by converting smaller data type into a larger data type.
int i,x; float f; double d; long int l;
Here, the above expression finally evaluates to a ‘double’ value.
Example
Following is an example for implicit type conversion −
int x; for(x=97; x<=122; x++){ printf("%c", x); /*Implicit casting from int to char %c*/ }
Explicit type conversion
Explicit type conversion is done by the user by using (type) operator.
Before the conversion is performed, a runtime check is done to see if the destination type can hold the source value.
int a,c; float b; c = (int) a + b
Here, the resultant of ‘a+b’ is converted into ‘int’ explicitly and then assigned to ‘c’.
Example
Following is an example for explicit type conversion −
int x; for(x=97; x<=122; x++){ printf("%c", (char)x); /*Explicit casting from int to char*/ }
Let us see the difference between the two types of conversions with examples −
Example (Implicit conversion)
#include<stdio.h> main(){ int i=40; float a; //Implicit conversion a=i; printf("implicit value:%f
",a); }
Output
Implicit value:40.000000
Example (Explicit Conversion)
#include<stdio.h> main(){ int i=40; short a; //Explicit conversion a=(short)i; printf("explicit value:%d
",a); }
Output
Explicit value:40
- Related Articles
- What are explicit type conversions in C#?
- What are implicit type conversions in C#?
- What is the difference between implicit and explicit type conversion in C#?
- Explicit Type Casting in Python Language
- What are the differences between implicit and explicit waits in Selenium with python?
- What are the differences between Widening Casting (Implicit) and Narrowing Casting (Explicit) in Java?
- Explicit type casting operator in C++
- Implicit return type int in C
- Implicit Threading and Language-based threads
- How does Implicit coercion differ from Explicit coercion in JavaScript?
- Explain the conversions of expressions of stacks in C language
- What is enumerated data type in C language?
- What are reading and writing characters in C language?
- What implicit objects are supported by JSP?
- What are different operators and expressions used in C language?
