5

I've just started messing with GMP and I can't seem to get numbers to print properly. Here is what I'm trying:

#include <stdio.h>
#include <stdlib.h>
#include "gmp.h"
int main(){
  mpz_t  n;
  mpz_init (n);
  mpz_set_ui(n, 2);

  gmp_printf("attempt 1: %d \n", n);
  gmp_printf("attempt 2: %Z \n", n);

  return 0;
}

I know this must be something really simple... but I'm just not seeing it.

My output is:

attempt 1: 1606416528 
attempt 2: Z 

I think I might just be using mpz_set_ui wrong...

EDIT:

%Zd works I also tried %n which I thought would work, but doesn't... definitely need some help on this.

Zevan
  • 10,097
  • 3
  • 31
  • 48
  • What type is *mpz_t* ? Is a struct, an union, a simple typedef to an integer ? – A.G. Jul 24 '12 at 11:51
  • @A.G. - Its an opaque type declared by GMP. The idea is that one never really has to know its internals. It is supposed to represent an arbitrary precision integer (i.e. can be a lot wider than the machine's ISA natively supports) – ArjunShankar Jul 24 '12 at 11:59

1 Answers1

13

You are using mpz_set_ui right.

gmp_printf("attempt 1: %d \n", n);
gmp_printf("attempt 2: %Z \n", n);

Both of the above don't work because it should actually be:

gmp_printf("attempt 3: %Zd \n", n);

because this is how gmp_printf requires it.

There's a rather complete treatment of formatted output strings in GMP here.

ArjunShankar
  • 23,020
  • 5
  • 61
  • 83