Skip to content

Instantly share code, notes, and snippets.

@bennyyip
Forked from d0k/wtab.c
Last active March 17, 2017 06:07
Show Gist options
  • Select an option

  • Save bennyyip/a3f3e11a66a6a26e9cfcb438fdf0664a to your computer and use it in GitHub Desktop.

Select an option

Save bennyyip/a3f3e11a66a6a26e9cfcb438fdf0664a to your computer and use it in GitHub Desktop.
C program to generate LaTeX truth tables
/* ./wtab n
* creates a truth table for LaTeX with n bits
*/
#include <stdio.h>
#include <stdlib.h>
#define SIZE unsigned long
#define MAXBITS sizeof(SIZE)*8-1
int main(int argc, char *argv[]) {
SIZE i;
int j, cols;
if (argc != 2 || (cols = atoi(argv[1])) <= 0 || cols > MAXBITS) {
fprintf(stderr, "wrong column count (max %d)\n", MAXBITS);
return EXIT_FAILURE;
}
for (i = 0; i < (1 << cols); i++) {
for(j = cols-1; j >= 0; j--)
printf("%c & ", ((i & (1 << j)) >> j)?'T':'F');
putchar('\n');
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment