# Thread: Global Variable Usage -- Failure in assignment

1. ## Global Variable Usage -- Failure in assignment

I was experimenting with global variables recently, specifically in a program that I'm writing to calculate a quadratic equation. However, the more general matter is the assignment of the global variables for the quadratic coefficients.

I will not post the entire code bit, but I've been able to narrow it down, and the following problem with assigning the global variables seems to depend on their type.

Code:
```double gA, gB, gC;

...

void getCoefficients(void) {

printf("Enter coefficients:\n");
printf("A: ");
scanf("%f", &gA);
printf("B: ");
scanf("%f", &gB);
printf("C: ");
scanf("%f", &gC);

printf("%f\n%f\n%f\n", gA, gB, gC); // not relevant to the function because of testing
//  purposes.

}```
In experimenting I placed the following printf() statement in the function to see the global variable values (I mainly did this because I don't have a debugger setup for my IDE) and this is what happens during runtime.

Code:
```Output:

Enter coefficients:
A: 2
B: -4
C: -3
0.000000
0.000000
0.000000
x1 = 1.50```
Bold indicates the values I input. However, I'm confused at why the globals are 0? I changed the data type of the globals to float, int, etc. and they worked then, but why does it cause a logic error when they are declared as doubles? I've searched the forums and I haven't found anything. If anyone's able to make sense of this or point me to a previous forum post or resource I can use to fix the problem then that would be of great help. Thank you.

2. Because you're lying to scanf. You're telling scanf (via %f) that gA, gB, and gC are floats. However, they are not. Consequently bad things happen. To read doubles with scanf, use %lf instead.

3. So there's a different format character for reading doubles using scanf() then there is in printing them? Your suggestion worked, I'm just wondering why in the printf() statement you can represent doubles as "%f" where in the scanf() the character is "%lf"? Thank you for assisting me tabstop.

4. The correct answer is that %f works for printing doubles because it is not possible to print floats. Due to some not-terribly-exciting technical reasons, floats passed to printf are converted to doubles (in a manner similar to the way that you can pass floats to the functions in math.h, or assign a float to a double). So printf will only ever see a double. However, scanf takes pointers to memory, so the size of the memory matters.

5. That makes sense. So is correct to say the reason you pass %lf to scanf() is because it's like a "long float" since a float is considered to be 32-bits wide on most computers where a double is 64 bits? so you add the long which adds 32-bits onto the 32-bit float? Since when accepting pointer, the memory must be the same? Correct me if I'm wrong.

6. The sizes may vary, but yes -- it is telling the compiler to use the larger size, since that's how large the variable is.

As a PS, I should mention that printf will let you use %lf to print a double, if you want to be consistent.

7. It just makes scanf write 8 bytes of memory instead of the 4 you get with &#37;f. It is always best to read the reference of a function before using it, even though it might be as simple as strlen. scanf has a little complex reference though, and sometimes it is hard to perfectly control it. Reading in a string and doing the conversion yourself ( double atof(char *s) ) is a situation much under your control.

8. Thanks a lot for the clarification tabstop and xuftugulus. I appreciate the help.