0
Write a C program to get float datatype as input and print the output as integer datatype.
I tried this code but I guess I am wrong 😒😣!!! #include <stdio.h> #include <conio.h> void main() { float a; int b; b = (int)a; printf("Enter a float number"); scanf("%f",&b); printf(b); return 0; }
5 Réponses
+ 4
This is because after assigning type casted value "a" to to integer variable "b", you are again telling compiler to cast "b" to type float by using "%f" format specifier.
Here is the fix 👇
https://code.sololearn.com/ceQKfNCo578U/?ref=app
P.S. in the fix, I have removed excess or non-standard code from your original program.
+ 1
I think you should use "a" in scanf instead of "b" and then typecast it to int
+ 1
Thank you so much Arsenic. It was of great help. 🤗
0
I tried Name the way you told:-
#include <stdio.h>
#include <conio.h>
void main()
{
float a;
int b;
b = (int)a;
printf("Enter a float number");
scanf("%f",&a);
printf("Value in integer: %f\n",&a);
return 0;
}
But I am getting the value as zero after inputting any float value.