Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GMP gmp_printf()

Tags:

c

gmp

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.

like image 308
Zevan Avatar asked Dec 16 '22 21:12

Zevan


1 Answers

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.

like image 191
ArjunShankar Avatar answered Jan 02 '23 11:01

ArjunShankar