user2775215
user2775215

Reputation: 5

Float read as double

My code looks like

#include <stdio.h>
#include <math.h>
int main (void)
{ 
float x1, x2, x3;
float y1, y2, y3;
float Cos0, i, j, k, innerProduct;
float Xlength, Ylength;
x1=0;
x2=0;
x3=0;
y1=0;
y2=0;
y3=0;
Cos0=0;
i=0;
j=0;
k=0;
innerProduct=0;
Xlength=0;
Ylength=0;

printf("Please insert six floating point numbers \n");

scanf("%f%f%f%f%f%f", x1, x2, x3, y1, y2, y3);

Xlength=sqrt((x1*x1)+(x2*x2)+(x3*x3));

Ylength=sqrt((y1*y1)+(y2*y2)+(y3*y3));

i=x1+y1;

j=x2+y2;

k=x3+y3;

innerProduct=((x1*y1)*(x2*y2)*(x3*y3));

Cos0=(innerProduct)/((Xlength*Ylength));

return 0;
}

And I get the following compile errors "warning: format %f expects argument of type 'float *' but argument "x" has type double"

where x is one of the digits, this happens for all 6 arguments in the scanf command, I have specified all of the variables as floats, and it doesn't like that.

How do I fix this? Thanks!

Upvotes: 0

Views: 744

Answers (2)

sleske
sleske

Reputation: 83635

scanf does not work like that. You must pass pointers to the variables that will hold the input.

Also, in the "template" (the first parameter), the placeholders must somehow be separated - otherwise scanf will not know how to split up the input it gets.

This will work:

scanf ("%f %f %f %f %f %f", &x1, &x2, &x3, &y1, &y2, &y3);

For more information, read the docs. For example, http://www.gnu.org/software/libc/manual/html_node/Formatted-Input-Functions.html says:

Function: int scanf (const char *template, ...)

The scanf function reads formatted input from the stream stdin under the control of the template string template. The optional arguments are pointers to the places which receive the resulting values.

Note the "pointers" part.


Another question is: "Why does the warning complain about a double, while I passed in a float"? This is answered in the comp.lang.c FAQ, question 15.10:

Q: I have a varargs function which accepts a float parameter. Why isn't va_arg(argp, float) working?

A: In the variable-length part of variable-length argument lists, the old "default argument promotions" apply: arguments of type float are always promoted (widened) to type double, and types char and short int are promoted to int. Therefore, it is never correct to invoke va_arg(argp, float); instead you should always use va_arg(argp, double). Similarly, use va_arg(argp, int) to retrieve arguments which were originally char, short, or int. (For analogous reasons, the last "fixed" argument, as handed to va_start, should not be widenable, either.) See also questions 11.3 and 15.2.

Note that scanf is a variadic function (or varargs function in short).

Upvotes: 3

David Duncan
David Duncan

Reputation: 1215

You're missing the ampersands in front of the 2nd-7th arguments to scanf(), which I'd imagine is a very common mistake. scanf()'s additional arguments are pointers to where to store the values found; it needs a reference in order to be able to save that data.

So without the ampersands, there is indeed a type mismatch. That said, it's not obvious to me why it's mentioning double.

Upvotes: 0

Related Questions