468,771 Members | 1,496 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

complex type

With what specifier do I use to print a variable of a complex type?
The man page for printf doesn't even say.
- nethlek
Nov 13 '05 #1
4 10517
Mantorok Redgormor wrote:
With what specifier do I use to print a variable of a complex type?
The man page for printf doesn't even say.


#include <complex.h>
#include <stdio.h>

int main(void)
{
complex c = 5 + 3*I;
printf("%g + %gi\n", creal(c), cimag(c));
return 0;
}

--
Martin Ambuhl

Nov 13 '05 #2
ne*****@tokyo.com (Mantorok Redgormor) wrote:
With what specifier do I use to print a variable of a complex type?
The man page for printf doesn't even say.


Unfortunately there is no printf format conversion specifier for
printing complex numbers directely, but ISO/IEC 9899:1999 says:

6.2.5 Types
[...]
13 Each complex type has the same representation and alignment
requirements as an array type containing exactly two elements of the
corresponding real type; the first element is equal to the real part,
and the second element to the imaginary part, of the complex number.
[...]

Thus you can printf the value of an object of complex type like an array
of 2 of the underlying real type, for example:

#include <stdio.h>
#include <complex.h>

int main( void )
{
double complex cmplvar = 5.0 + 3.0 * I;
printf ("cmplvar = %f+%fi\n", cmplvar[0], cmplvar[1] );
return 0;
}

Untested code; the implementation at hand doesn't support complex types.

HTH
Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #3
Irrwahn Grausewitz wrote:
ne*****@tokyo.com (Mantorok Redgormor) wrote:
With what specifier do I use to print a variable of a complex type?
The man page for printf doesn't even say.


Unfortunately there is no printf format conversion specifier for
printing complex numbers directely, but ISO/IEC 9899:1999 says:

6.2.5 Types
[...]
13 Each complex type has the same representation and alignment
requirements as an array type containing exactly two elements of the
corresponding real type; the first element is equal to the real part,
and the second element to the imaginary part, of the complex number.
[...]

Thus you can printf the value of an object of complex type like an array
of 2 of the underlying real type, for example:

#include <stdio.h>
#include <complex.h>

int main( void )
{
double complex cmplvar = 5.0 + 3.0 * I;
printf ("cmplvar = %f+%fi\n", cmplvar[0], cmplvar[1] );


"cmplvar" is not an array (or pointer), so you can't use the subscript
operator on it, despite the guarantees about representation and
alignment quoted above. You can do nasty things with casts to make
your example work, but creal() and cimag() are the right solution in
this case.

Jeremy.
Nov 13 '05 #4
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> wrote:
Irrwahn Grausewitz wrote:
printf ("cmplvar = %f+%fi\n", cmplvar[0], cmplvar[1] );
"cmplvar" is not an array (or pointer), so you can't use the subscript
operator on it, despite the guarantees about representation and
alignment quoted above. You can do nasty things with casts to make
your example work,

Indeed.
but creal() and cimag() are the right solution in
this case.


Definitely! What was I thinking? Fortunately Martin Ambuhl posted a
correct example. My apologies for posting nonsense.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by AL | last post: by
9 posts views Thread by Kanchana D S | last post: by
3 posts views Thread by katis | last post: by
reply views Thread by C.W.Holeman II | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.