Skip to content

Instantly share code, notes, and snippets.

@Isweet
Created October 22, 2015 00:56
Show Gist options
  • Save Isweet/9042f3ee6984ac2a9ef9 to your computer and use it in GitHub Desktop.
Save Isweet/9042f3ee6984ac2a9ef9 to your computer and use it in GitHub Desktop.
A powerful demonstration of the ternary operator!
#!/bin/bash
cat << EOF > ./ternary.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]) {
int a, b, c, d, e, f, g;
srand(time(NULL));
a = rand() % 10 + 1;
b = rand() % 10 + 1;
c = rand() % 10 + 1;
d = rand() % 10 + 1;
e = (f = (a > b ? a : b)) > (g = (c > d ? c : d)) ? f : g;
printf("a = %d, b = %d, c = %d, d = %d, max = %d\n", a, b, c, d, e);
return 0;
}
EOF
gcc ./ternary.c -o ternary; for i in `seq 1 10`; do sleep 1 && ./ternary; done; rm -rf ./ternary ./ternary.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment