problems with printf

This is a discussion on problems with printf within the C Programming forums, part of the General Programming Boards category; i have the following code: Code: #include <stdio.h> float z(float x,float y) { double g; g = -x*x + -y*y ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    problems with printf

    i have the following code:

    Code:
    #include <stdio.h>
    
    float z(float x,float y)
    {
    	double g;
    	g = -x*x + -y*y + 2;
    	printf("%f", &g);
    	return g;
    }
    
    
    int main()
    {
    	float x;
    	float y;
    	double s;
    
    
    	x = 0;
    	y = 0;
    	s = z(x,y);
    	return 0;
    }
    When I try compiling with gcc on a linux mint distribution, I get the following warning:
    Lab5Exercise1.c: In function ‘z’:
    Lab5Exercise1.c:11: warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘double *’

    Since it is just a warning, an output file was created which returns 0.000000 when it should be 2 if my math is correct.

    Before this, i had a loop to test several coordinates pair into the function, but the returned values where really off from where they should be, so i made this basic test code.

    Thx in advance, Donaldgx

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,803
    You want to print a double, so print it, not a pointer to a double:
    Code:
    printf("%f", g);
    You probably confused this with scanf, where you want to read a double, so you need to pass a pointer to a double so that your double variable is modified.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,832
    "%f" in printf expect the value of a double, not the pointer of a double. That's what your compiler is saying.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Thanks for the help, it works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting printf to fprintf and fixing code problems
    By nabhatt in forum C Programming
    Replies: 1
    Last Post: 03-01-2012, 08:32 PM
  2. printf("04x",short) - negitive hex giving me problems
    By KairosDrasis in forum C Programming
    Replies: 3
    Last Post: 12-09-2008, 10:51 AM
  3. cout to printf problems
    By hallo007 in forum C++ Programming
    Replies: 9
    Last Post: 09-27-2006, 10:22 AM
  4. Replies: 14
    Last Post: 01-12-2006, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21