Skip to content

Instantly share code, notes, and snippets.

@njsmith
Last active October 7, 2016 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njsmith/f666671de31f35644bb91a815c9cad75 to your computer and use it in GitHub Desktop.
Save njsmith/f666671de31f35644bb91a815c9cad75 to your computer and use it in GitHub Desktop.
/* Silly little test file for some popular C99 features:
* - C++-style comments
* - variadic macros
* - 'static inline'
* - within-block variable declarations
* - designated initializers
* - stdint.h
* - stdbool.h
*
* Copyright (C) 2016 Nathaniel Smith <njs@pobox.com>
* Licensed under BSD-2 or MIT or Apache 2.0 at your option.
*/
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
// C++-style comment?
#define variadic_printf(...) fprintf(stdout, __VA_ARGS__)
/* static inline -- this is only one of several 'inline' variants supported by
* c99, but I think it's the only one we care about?
*/
static inline int add(int a, int b)
{
return a + b;
}
int main(int argc, char** argv)
{
variadic_printf("variadic macros: %s\n", "seem to work");
assert(add(1, 2) == 3);
/* within-block variable declarations */
int i = 1;
/* designated array initializers */
int arr[4] = { [2] = 2, [1] = 1 };
assert(arr[0] == 0);
assert(arr[1] == 1);
assert(arr[2] == 2);
assert(arr[3] == 0);
struct point
{
int x, y, z;
} point = { .y = 2, .x = 1 };
assert(point.x == 1);
assert(point.y == 2);
assert(point.z == 0);
/* stdint.h */
assert(sizeof(int32_t) == 4);
bool b = true;
assert(b != false);
b = 10;
assert(b == 1);
printf("Looks good!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment