Thread: Global Variable Usage -- Failure in assignment

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    Wink 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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    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. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    It just makes scanf write 8 bytes of memory instead of the 4 you get with %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.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Thanks a lot for the clarification tabstop and xuftugulus. I appreciate the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static variable usage
    By shwetha_siddu in forum C Programming
    Replies: 1
    Last Post: 04-02-2009, 12:33 AM
  2. deleting and re-assigning a global variable pointer
    By Canadian0469 in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2008, 03:47 PM
  3. out variable assignment
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 06-15-2008, 07:12 PM
  4. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM