I'm writing a simple code, just to teach myself C. I get the following errors when I compile:
21: warning: format ‘%lf’ expects type ‘double’, but argument 5 has type ‘complex double’
28: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘double’
30: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘complex double’
I guess my question is what does the * denote, and what to I declare my variable to be (as in %?) to avoid it.
It also transpires that I get the wrong answer at the end of it all. Would this be due to the above?
Update:So, that's my code. I was having trouble writing even this before, which is why it's quite slow to get going. I wanted to go through each step before I combined them. It was a case of a missing "&" in the second error, thank you! It makes sense of the Euler formula perfectly the first time, but then gives the wrong solution the second time around. By typing "1 0 1", the second part reduces to the first, but putting in a value of pi gives 1, rather than the -1 it gave above. It seems bizarre to me that this would be the case. I can only assume it finds a fault in my "1*(cos(arg)+I*sin(arg)) + B1*(cos(-arg)+I*sin(-arg)))" bit.
Copyright © 2024 1QUIZZ.COM - All rights reserved.
Answers & Comments
Verified answer
You seem to be using printf/scanf family of functions. It's not immediately obvious how to fix these errors without the text of the program that generated them, but I'll try to guess.
line 21 and line 30 must be using a printf() family function with a complex double number. Unlike C++, C does not have a built-in way to output a complex number. You will have to break it up into real and imaginary (or abs and arg) parts yourself:
#include <stdio.h>
#include <complex.h>
int main()
{
double complex c = 5 + 3 * I;
// printf("%f\n", c); // this gives the error like yours
printf("%f + %fi\n", creal(c), cimag(c));
}
The error in your line 28 seems to be coming from a function of scanf() family. Format specifier %lf in such functions requires pointer arguments:
#include <stdio.h>
int main()
{
double f;
// scanf("%lf\n", f); // this gives an error like yours
scanf("%lf\n", &f);
}
Well, you didn't post the lines in question from your code, but here's a brief discussion about pointers.
If you have a variable defined like this:
double mydouble = 7.8;
You can do this:
printf("%lf", mydouble);
If however you declare a pointer to a double like this:
double *mydouble; /* the '*' in front of the variable denotes a pointer */
mydouble = new double; /* allocate memory to store the double */
You would then have to do this:
printf("%lf", *mydouble); /* Notice the '*' to dereference the pointer which gets the actual value */