Argument coercion is a feature of function prototypes by which the compiler implicitly converts the datatype of the arguments passed during the function call to match the datatype in the definition of the function.

It follows Argument promotion rules. Therefore a lower datatype maybe converted into a higher datatype but vice-versa is not true. This is because when a higher datatype is converted to a lower datatype it can result into loss or truncation of data.
The Promotion hierarchy for fundamental datatypes in C++ is:

Example: Take for Example the code given below consists of an add function that expects double arguments. But even when integer arguments are passed it works correctly. In case long double arguments are passed the code will give an error.
#include <iostream> using namespace std; double add(double a, double b) { return a + b; } int main() { // Passing double arguments, as expected cout << "Sum = " << add(2.4, 8.5) << endl; // Passing int arguments, when double is expected // This will lead to Argument Coercion cout << "Sum = " << add(16, 18) << endl; return 0; } |
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Why copy constructor argument should be const in C++?
- Variable Length Argument in C
- C function argument and return values
- Difference between Argument and Parameter in C/C++ with Examples
- Draw an ellipse divided by straight line into two colored part in C++ Graphics
- Types of C files after its compilation
- What happens if we mix new and free in C++?
- VS Code | Build, Run and Debug in C++
- VS Code | Compile and Run in C++
- Food Ordering System in C
- Difference between sizeof(int *) and sizeof(int) in C/C++
- Runtime and Compile-time constants in C++
- Efficient ways to compare a variable with multiple values
- Overloading the Comma Operator
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

