You are here: Home / Topics / What would happen if we try to assign a variable with an incorrect value of datatype?

What would happen if we try to assign a variable with an incorrect value of datatype?

Filed under: C Programming on 2022-07-05 21:43:51

If we try to assign a variable with an incorrect value of datatype, then the compiler will (most probably) generate an error- the compile-time error. Or else, the compiler will convert this value automatically into the intended datatype of the available variable.
Example,

#include
int main() {
// assignment of the incorrect value to the variable
int a = 20.397;
printf(“Value is %d”, a);
return 0;
}


The output generated here will be:
20
As you can already look at this output- the compiler of C will remove the part that is present after the decimal. It is because the data types are capable of storing only the whole numbers.

About Author:
P
Parvesh Kanani     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.