writeq(Term) is equivalent to printf("%DIMQvw", Term) or write_term(Term, [attributes(full),quoted(true),numbervars(true), variables(raw),depth(full),transform(false)]).
Note that as usual, the output is buffered, so it may need to be flushed either by closing the stream, by writing again or by using flush/1.
Note also that although it is possible to print suspensions and external handles, these are printed in their printed representation as Prolog terms with functors such as '$&'/2 (for handles). They will be read back in as such Prolog terms, rather than as their original type.
Success:
?- writeq(output, "string"),nl(output),
writeq(output, head:-body).
"string"
head :- body
yes.
?- writeq(*(^(1,2),+(3,4))).
1 ^ 2 * (3 + 4)
yes.
Error:
writeq(S, a(b,c)). (Error 4).
writeq("string", a(b,c)). (Error 5).
writeq(9, X + 2). (Error 192).
writeq(atom, X + 2). (Error 193).