aboutsummaryrefslogtreecommitdiffstats
path: root/cil/test/small1/vararg1.c
diff options
context:
space:
mode:
authorxleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2009-03-29 09:47:11 +0000
committerxleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2009-03-29 09:47:11 +0000
commita5f03d96eee482cd84861fc8cefff9eb451c0cad (patch)
treecbc66cbc183a7c5ef2c044ed9ed04b8011df9cd4 /cil/test/small1/vararg1.c
parenta9621943087a5578c995d88b06f87c5158eb5d00 (diff)
downloadcompcert-a5f03d96eee482cd84861fc8cefff9eb451c0cad.tar.gz
compcert-a5f03d96eee482cd84861fc8cefff9eb451c0cad.zip
Cleaned up configure script.
Distribution of CIL as an expanded source tree with changes applied (instead of original .tar.gz + patches to be applied at config time). git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@1020 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e
Diffstat (limited to 'cil/test/small1/vararg1.c')
-rw-r--r--cil/test/small1/vararg1.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/cil/test/small1/vararg1.c b/cil/test/small1/vararg1.c
new file mode 100644
index 00000000..cc710a7b
--- /dev/null
+++ b/cil/test/small1/vararg1.c
@@ -0,0 +1,47 @@
+
+/* VA.C: The program below illustrates passing a variable
+ * number of arguments using the following macros:
+ * va_start va_arg va_end
+ * va_list va_dcl (UNIX only)
+ */
+
+#include <stdio.h>
+#include <stdarg.h>
+int average( int first, ... );
+union vararg_average {
+ int ints; /* We only pass ints to this one */
+};
+
+#include "testharness.h"
+
+int main( void )
+{
+ /* Call with 3 integers (-1 is used as terminator). */
+ if(average( 2, 3, 4, -1 ) != 3) E(1);
+ if(average( 5, 7, 9, 11, 13, -1 ) != 9) E(2);
+ if(average( -1 ) != 0) E(3);
+
+ SUCCESS;
+}
+
+
+
+/* Returns the average of a variable list of integers. */
+int average( int first, ... )
+{
+ int count = 0, sum = 0, i = first;
+ va_list marker;
+
+ va_start( marker, first ); /* Initialize variable arguments. */
+ while( i != -1 )
+ {
+ sum += i;
+ count++;
+ i = va_arg( marker, int);
+ }
+ va_end( marker ); /* Reset variable arguments. */
+ return( sum ? (sum / count) : 0 );
+}
+
+// Put this intentionally at the end
+#pragma ccuredvararg("average", sizeof(union vararg_average))