A challenged co-worker of mine challenged me to come up with a perverse
example of a conforming Standard C program. This is by no means a gold
medal winner in a C obfuscation contest, but nevertheless it
demonstrates some arguably serious abuse of the preprocessor. If you can
make it even more perverse, I'd appreciate it.
And for those of you who might ask what my C question is ... is this
Standard C?
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#define UNSIGNED unsigned
#define CHAR char
#define INT int
#define MAIN main
#define VOID void
#define RETURN return
#define STATIC static
#define BEGIN {
#define END }
#define IF if(
#define THEN ){
#define ELSE }else{
#define ELSEIF }else if(
#define ENDIF }
#define FOR for(
#define LOOP ){
#define ENDLOOP }
#define IS ==
/*lint -save -e723 Suspicious use of = */
/*(OK since I know what I am doing)*/
#define ASSIGN =
/*lint -restore*/
#define LT <
#define GTE >=
#define BUMP +=
#define CUT -=
#define PRINTF printf
#define PUTS puts
#define FFLUSH fflush
#define GETS gets
#define STDOUT stdout
#define STRLEN strlen
#define TOUPPER toupper
#define SIZE_T size_t
STATIC CHAR *MAKE_UPPER(CHAR *S);
INT MAIN(VOID)
BEGIN
INT I;
CHAR NAME[4096];
PRINTF("ENTER YOUR NAME: ");
FFLUSH(STDOUT);
PRINTF("HI %s!\n", MAKE_UPPER(GETS(NAME)));/*lint !e421 Caution -- */
/*function */
/*'gets(char *)'*/
/*is considered*/
/*dangerous*/
/*(OK, since no one*/
/*has a name greater*/
/*in length than 4095*/
/*characters).*/
PUTS("LET'S TEST A FOR LOOP WITH BUMP");
FOR I ASSIGN 0; I LT 6; I BUMP 1
LOOP
IF I IS 4
THEN
PRINTF("I IS 4\n");
ELSEIF I IS 5
THEN
PRINTF("I IS 5\n");
ELSE
PRINTF("I IS %d\n", I);
ENDIF
ENDLOOP
PUTS("LET'S TEST A FOR LOOP WITH CUT");
FOR I ASSIGN 5; I GTE 0; I CUT 1
LOOP
IF I IS 4
THEN
PRINTF("I IS 4\n");
ELSEIF I IS 5
THEN
PRINTF("I IS 5\n");
ELSE
PRINTF("I IS %d\n", I);
ENDIF
ENDLOOP
PRINTF("BYE %s!\n", MAKE_UPPER(NAME));
RETURN 0;
END
STATIC CHAR *MAKE_UPPER(CHAR *S)
BEGIN
SIZE_T I;
SIZE_T LEN;
IF S IS NULL
THEN
RETURN NULL;
ENDIF
LEN = STRLEN(S);
FOR I ASSIGN 0; I LT LEN; I BUMP 1
LOOP
S[i] = (CHAR)TOUPPER((UNSIGNED CHAR)(S[i]));
ENDLOOP
RETURN S;
END
Thanks
--
Jerret