C complex number and printf - c

How to print ( with printf ) complex number? For example, if I have this code:
#include <stdio.h>
#include <complex.h>
int main(void)
{
double complex dc1 = 3 + 2*I;
double complex dc2 = 4 + 5*I;
double complex result;
result = dc1 + dc2;
printf(" ??? \n", result);
return 0;
}
..what conversion specifiers ( or something else ) should I use instead "???"

printf("%f + i%f\n", creal(result), cimag(result));
I don't believe there's a specific format specifier for the C99 complex type.

Let %+f choose the correct sign for you for imaginary part:
printf("%f%+fi\n", crealf(I), cimagf(I));
Output:
0.000000+1.000000i
Note that i is at the end.

Because the complex number is stored as two real numbers back-to-back in memory, doing
printf("%g + i%g\n", result);
will work as well, but generates compiler warnings with gcc because the type and number of parameters doesn't match the format. I do this in a pinch when debugging but don't do it in production code.

Using GNU C, this works:
printf("%f %f\n", complexnum);
Or, if you want a suffix of "i" printed after the imaginary part:
printf("%f %fi\n", complexnum);

Related

Simple division of 2/12 with double causing errors

I don't know what is causing this error, I try dividing 2/12 using double but it gives me a completely wrong number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
double a;
a = 2/12;
printf("\n a = %lf \n", a);
return 0;
}
This returns -272632568 and makes no sense. Thanks for your help.
First of all, you should consider the declaration:
double a = 2 / 12; // (integer)
It'll just give you zero and then assign to double since they're integers. If you use rather:
double a = 2.0 / 12.0; // explicitly defining
Then you'll get right precision output by using this statement:
printf("\n a = %f\n", a);
Or,
printf("\n a = %lf\n", a);
You used %d in your format string, however %d means integer not double.
You want to use %f inplace of %d
See here for details: https://www.gnu.org/software/libc/manual/html_node/Table-of-Output-Conversions.html#Table-of-Output-Conversions

Is there something in C that prevent my large numbers from turning into "6.95278e-310". I would like the full number to be printed [duplicate]

How can I display a double like
5000683
Instead of 5.000683e6 in C?
I have tried %d, %g and %f, but to no avail.
It looks like %f works just fine:
#include <stdio.h>
int main()
{
double d = 5000683;
printf("%f\n", d);
printf("%.0f\n", d);
return 0;
}
The output of this code will be
5000683.000000
5000683
The second printf() statement sets the precision to 0 (by prefixing f with .0) to avoid any digits after the decimal point.

c code doesnt work on turbo c++ with the float numbers

#include <stdio.h>
#include <math.h>
int main(void)
{
double r,d,x,y,pi;
clrscr();
printf("Input the value of r and degree: ");
//scanf("%f",&r);
//scanf("%f",&d);
r = 12;
d = 195;
pi = 3.14;
x = r * cos(d * pi/180);
y = r * sin(d * pi/180);
printf("The polar form is: (%f,%f)", x, y);
getch();
}
In the 1st case with defined values of r and d the output comes correct but in 2nd case when I give the input the output doesn't match with the original answer. The code worked on codeblocks but isn't working on turbo c++.
what am I doing wrong in turbo c++?
Mis-matched format specifier. Use "%lf" with a double *
double r,d,x,y,pi;
...
//scanf("%f",&r);
//scanf("%f",&d);
scanf("%lf",&r);
scanf("%lf",&d);
Better codes checks for success before using r.
if (scanf("%lf",&r) != 1) Handle_Error();
Output is Cartesian coordinates. #Jens. I'd also recommend a '\n'.
// printf("The polar form is: (%f,%f)", x, y);
printf("The Cartesian form is: (%f,%f)\n", x, y);
It appears Turbo-C requires "l" when printing double, so use
printf("The Cartesian form is: (%lf,%lf)\n", x, y);
Little reason to use a low precision pi. Suggest
pi = acos(-1.0);
In:
//scanf("%f",r);
//scanf("%f",d);
You need to pass the addresses of the variables, &r and &d.

Adding doubles in ansi C produces unexpected results

For my school project we are required to do math with doubles. My current code produces some unexpected results.
/* Hello World program */
#include<stdio.h>
int main()
{
double result = 0.0;
double x;
x = 10.0;
result = x + 10.0;
printf("%d", result);
return 0;
}
Upon running, this code prints: "-1267258024"
I don't understand why this happens? Why does the code not print 20.0?
Thanks!
EDIT: I'm so dumb. %d is for floats. Thank you!
The line
printf("%d", result);
indicates that you want to print an integer.
You probably want
printf("%f", result);
There are also things like %lf (acts the same as %f) and %Lf (works for long doubles) which you can read about on this answer.
A complete list of formatting options can be found here.

What's wrong with my structs?

Ok, i'm writing in c here. Compiling in mingw gcc.
I'm trying to do something really simple. create a vector struct containing 3 floats x,y,z.
then I want to be able to do some math with them.
This is my short test program:
#ifndef _PHYSICS_C_
#define _PHYSICS_C_
#define SUCCESS 0
#define FAILURE 1
typedef struct {
float x;
float y;
float z;
}vector;
int add ( vector* a, vector* b, vector* destination ){
(*destination).x = (float)( ((*a).x) + ((*b).x) );
(*destination).y = (float)( ((*a).y) + ((*b).y) );
(*destination).z = (float)( ((*a).z) + ((*b).z) );
return SUCCESS;
}
int main(int argc, char** argv){
printf("creating vectors\n\n");
vector a = {1.0f,5.0f,3.0f};
vector b = {2.0f,3.0f,6.0f};
vector destination;
printf("adding vectors\n\n");
if(add(&a, &b, &destination) == SUCCESS){
printf("result: (%d, %d, %d)\n\n",destination.x,destination.y,destination.z);
} else {
printf("the program failed somehow...\n\n");
}
printf("Press any key to continue...\n");
getchar();
return SUCCESS;
}
#endif
When I compile and run it, it should return (3, 8, 9) the sum of vectors a and b.
instead it returns (0, 1074266112, 0)...
I can't figure out what is wrong.
for some reason I think that I must somehow be writing over memory I'm not supposed to.
x,y,z are floats but you are trying to print them as integers.
try:
printf("result: (%f, %f, %f)\n\n",destination.x,destination.y,destination.z);
check man printf or your documentation to see all of the specifiers for printf.
%d expects int. Use %f or %g for floats/doubles.
Identifiers starting with an underscore followed by an uppercase letter are reserved; don't use them in your own code.
Include guards (#ifndef _PHYSICS_C_ ...) are for header files, not for .c files.
printf requires #include <stdio.h>.
You're returning the value SUCCESS from main(). That's ok, since SUCCESS happens to be 0, but it would be clearer to use either EXIT_SUCCESS (declared in <stdlib.h> or just return 0;.
You add function always returns SUCCESS. It might as well be a void function (and the test for its value in main is not useful). Unless you anticipate adding error checking later on.
The casts in your add function are unnecessary; the expression is already of type float. And (*foo).bar is better written as foo->bar. For example, the first assignment can be simplified to destination->x = a->x + y->x;.
The real problem (which has already been pointed out) is that you're using a "%d" format for values of type float.
It's usual to use double rather than float. It has more precision, and modern hardware is often optimized for double-precision operations.
If you enable warnings in your compiler, it will probably tell you about some of these problems.
You are printing float with format specifier %d which is intended for signed int. Use %f or %g or %e instead .
Also, why don't you do:
destination->x = a->x + b->x;
Its much easier on the eyes. (although not a problem).

Resources