Skip to content

Instantly share code, notes, and snippets.

@aprell
Created October 2, 2012 14:16
Show Gist options
  • Save aprell/3819480 to your computer and use it in GitHub Desktop.
Save aprell/3819480 to your computer and use it in GitHub Desktop.
Protected blocks in C
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// Make sure that before is executed before a dependent block or statement
// and after is executed afterwards
#define PROTECTED(before, after) \
for (int __i = ((before), 0); !__i; (after), __i++)
int main(void)
{
int i, sum = 0;
#define N 100
PROTECTED(assert(sum == 0), assert(sum == N*(N+1)/2)) {
for (i = 1; i <= N; i++) {
sum += i;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment