'C' error in printf warning: format ‘%lf’ expects type ‘double’ ...
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
'C' error in printf warning: format ‘%lf’ expects type ‘double’ ...
I'm writing a program to plot a set of cosine and sine functions, so I want to output the calculated values to a file so I can plot them with gnuplot.
Here is the code:
#include <stdio.h>
#include <math.h>
//#define PI 3.14159;
int main() {
double a1;
double x[100];
double l = 20.0;
double result[100];
int a2, n = 11;
double arg;
double PI = 3.14159;
FILE *f;
//double *ptr =
int i =0;
int j = 0;
f = fopen("output.txt", "w");
fprintf(f, "n x value");
while(n <= 100) {
a1 = n; /* To get the program to do 2 different operations for odd and even numbers
we declare 2 variables as an int and a double so when we divide them
by 2 if the value is even then a1 and a2 will be equal as integer division will
return an integer and will round and non-integer values */
a2 = n;
a1 = a1/2;
a2 = a2/2;
printf("a1 = %lf\n a2=%d\n", a1, a2);
if(a1 == a2) {
//n is even so do sine
while (i!=100) {
arg = ( ( (x[i]) * (n) * (PI) ) / (l) );
result[i] = sin(arg);
fprintf(f, "%d %lf %lf\n", n, x, result[i]); //This is the error
x[i] = x[i] +0.2;
i++;
}
}
else {
//n is odd so do cosine
while(j<=100) {
arg = ( ( (x[i]) * (n) * (PI) ) / (l) );
result[i] = cos(arg);
x[i] = x[i] +0.2;
j++;
}
}
n++;
}
fclose(f);
n++;
}
However it gets an error on line 35 - the fprintf function.
sine_and_cosine_2.c: In function ‘main’:
sine_and_cosine_2.c:35: warning: format ‘%lf’ expects type ‘double’, but argument 4 has type ‘double *’
The thing is if I try and compile this on the my universitys linux server it compiles fine as have all the times I've tried to print an array element, it's only on my laptop which runs Ubuntu 9.10 with the GCC compiler. The compiler code I use is:
gcc sine_and_cosine_2.c -lm -o sine_and_cosine_2
I'm a little bit baffled, as always help would be greatly appreciated.
Use code tags next time, so your code will be readable.
Code:
double x[100];
Code:
fprintf(f, "%d %lf %lf\n", n, x, result[i]); //This is the error
probably you meant x[i] not x as the 4'th arg to fprintf.
Quote:
The thing is if I try and compile this on the my universitys linux server it compiles fine as have all the times I've tried to print an array element, it's only on my laptop which runs Ubuntu 9.10 with the GCC compiler.
Aren't you lucky to have a better compiler on that Ubuntu system!
On all the others it might compile without error, but it would run incorrectly.
Just out of interest when it says 4th argument I thought it meant result[i] because 'f' would be the first, n the 2nd, x the 3rd and result[i] the 4th.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.