aboutsummaryrefslogtreecommitdiffstats
path: root/examples/02-prod.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-06-19 02:50:31 +0200
committerMattias Andrée <maandree@kth.se>2016-06-19 02:50:31 +0200
commita049db580776ed41bb4583bfb331d97c5a80900a (patch)
tree07c663214541da3ec94f0913b2ab78334bf7b6f6 /examples/02-prod.c
parentdoc: vulnerabilities concerning cryptographic applications (diff)
downloadlibzahl-a049db580776ed41bb4583bfb331d97c5a80900a.tar.gz
libzahl-a049db580776ed41bb4583bfb331d97c5a80900a.tar.bz2
libzahl-a049db580776ed41bb4583bfb331d97c5a80900a.tar.xz
Add examples: sum, prod, avg, median
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'examples/02-prod.c')
-rw-r--r--examples/02-prod.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/examples/02-prod.c b/examples/02-prod.c
new file mode 100644
index 0000000..8b7af40
--- /dev/null
+++ b/examples/02-prod.c
@@ -0,0 +1,36 @@
+/* Calculates the product of $@ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <zahl.h>
+
+int
+main(int argc, char *argv[])
+{
+ z_t prod, factor;
+ jmp_buf env;
+ char *buf;
+ int i;
+
+ if (setjmp(env))
+ return zperror(argv[0]), 1;
+
+ zsetup(env);
+ zinit(prod);
+ zinit(factor);
+ zsetu(prod, 1);
+
+ for (i = 1; i < argc; i++) {
+ zsets(factor, argv[i]);
+ zmul(prod, prod, factor);
+ }
+
+ printf("%s\n", buf = zstr(prod, NULL, 0));
+ free(buf);
+
+ zfree(factor);
+ zfree(prod);
+ zunsetup();
+ return 0;
+}