Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assigning an expression to variables in C language

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading