Write a program to show the effect of mismatch of data types.

#include<stdio.h>
#include<conio.h>
int main()
{
	int a;
	printf("Enter value of 'A':-");
	scanf("%c",&a);
	printf("A=%c",a);
	getch();
}

In the above program, the variable a is defined as an integer, but when the input is taken using scanf function, the format specifier used is %c, which is for taking input of characters, not integers. This causes a mismatch in the data types, and the input is not stored in a as expected.

When the program is run, it prompts the user to enter a value for a. If the user enters a digit, say 5, the program will read it as a character and store its ASCII value (53) in a. As a result, when the value of a is printed using printf, it is displayed as the corresponding character, not as the integer 5.

To fix the program, the format specifier used in scanf should be changed to %d, which is the correct format specifier for reading integer values.

Thanks

Write a program to show the effect of mismatch of data types.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top