I have an assignment in C where i have to create a small program that would calculate some things. The only thing im struggling with is that according to said assignment, i need to let user declare x as 1.825*10^2 using scanf function. However, when doing that my program just exits.
Here’s the relevant part of my code
#include <stdio.h>
#include <math.h>
// VARIANT 2
int main()
{
float x, y, z, ans1, ans2, answer;
printf("Declare x:\n");
scanf("%f", &x);
printf("Declare y:\n");
scanf("%f", &y);
printf("Declare z:\n");
scanf("%f", &z);
printf("%f %f %f \n", x,y,z);
}
Im declaring X in terminal as 1.825*pow(10,2). My guess is that scanf doesnt allow/read expressions – but the assignment is clear that i need to declare X via scanf.
>Solution :
First, what you’re describing is not "declaring" x. You’re describing reading in a value for x.
The %f format specifier for scanf expects a decimal floating point constant to be read, and the string 1.825*pow(10,2) is not such a constant.
The way you would specify such a value would be 1.825e2.