473,770 Members | 6,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with a A Perverse Standard C Program

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(CHA R *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(CHA R *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
Oct 10 '06 #1
9 2042
Jerret Johnson wrote:
And for those of you who might ask what my C question is ... is this
Standard C?
Yes. Perfectly legal ANSI C. GCC gives a warning about using gets().

This one is my all-time favorite.

#define BeginProgram void main(int argc, char *argv[])
#define CloseBrace }
#define CommandLineArgu ment -1
#define Declare int i,j,n,Flag=1;
#define EndOfProgram return;
#define False 0;
#define ForLoop ;for
#define GetCommandLineA rgument n=atoi(argv[1]);
#define i F1ag
#define If if
#define Increment ++
#define Is ==
#define LessThan *(c&64)*
#define LessThanOrEqual To !=
#define Modulo %
#define OpenBrace {
#define PossibleFactor j
#define PossiblePrime i
#define Possib1ePrime (c=getchar())
#define PrimeNumber (c^(!i*n%64));
#define Print putchar
#define SetTo =
#define SmallestPrime 2
#define True 1
#define Variables char c;
#define Zero i%j

BeginProgram
OpenBrace
Declare Variables
GetCommandLineA rgument

ForLoop (PossiblePrime SetTo SmallestPrime ;
Possib1ePrime LessThanOrEqual To CommandLineArgu ment ;
Increment PossiblePrime)
OpenBrace
F1ag SetTo True
ForLoop (PossibleFactor SetTo SmallestPrime ;
PossibleFactor LessThan PossiblePrime ;
Increment PossibleFactor)
If (PossiblePrime Modulo PossibleFactor Is Zero)
F1ag SetTo False

If (Flag Is True)
Print PrimeNumber
CloseBrace

EndOfProgram
CloseBrace
Oct 10 '06 #2
On Tue, 10 Oct 2006 00:06:13 -0700, Jerret Johnson <jj*******@aol. com>
wrote:
>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.
Fortunately you can also use the proprocessor to effortlessly
reconstruct the C program from your 'perversity'.

Best wishes,
Roland Pibinger
Oct 10 '06 #3
jmcgill said:
This one is my all-time favorite.

#define BeginProgram void main(int argc, char *argv[])
So much for Standard C.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 10 '06 #4
Jerret Johnson said:

<snip>
And for those of you who might ask what my C question is ... is this
Standard C?

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).*/
Not okay, because it can be attacked by a malicious user (cf Internet Worm).
It also fails to take into account the possibility that gets() returns
NULL.

<snip>
S[i] = (CHAR)TOUPPER(( UNSIGNED CHAR)(S[i]));
The first cast is unnecessary. The second is sensible.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 10 '06 #5

"Richard Heathfield" <in*****@invali d.invalidwrote in message
news:E5******** *************** *******@bt.com. ..
Jerret Johnson said:

<snip>
And for those of you who might ask what my C question is ... is this
Standard C?

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).*/

Not okay, because it can be attacked by a malicious user (cf Internet
Worm).
It also fails to take into account the possibility that gets() returns
NULL.
?

Then why does MAKE_UPPER begin with this?

IF S IS NULL
THEN
RETURN NULL;
ENDIF

It appears to me that it takes the possibility into account, and then passes
the NULL along to printf()...

;)
Rod Pemberton
Oct 10 '06 #6

"Jerret Johnson" <jj*******@aol. comwrote in message
news:GD******** ********@newsfe 03.lga...
A challenged co-worker of mine challenged me to come up with a perverse
example of a conforming Standard C program.
Why? (whiny)...
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.
Someone a while back tried to turn C into Pascal or BASIC using the
preprocessor. So, there are two directions you could go. It might look
really interesting if you chose BASIC and managed to add line numbers...
You could then hide all the defines in a local include for your prank.

Rod Pemberton
Oct 10 '06 #7

Jerret Johnson wrote:
A challenged co-worker of mine challenged me to come up with a perverse
example of a conforming Standard C program.
Actually I've done something similar to "help" C code look a little
less like a mangy porcupine in a blender.

#define If if(
#define Then ) {
#define Else } else {
#define Equals ==
#define Is ==
#define Isnt !=
#define Procedure void
#define Function
#define NoArgs ( void )
#define For(var,lo,hi) for(var=(lo);va r <=(hi);var++)
#define var {
#define begin {
#define end }
#define code
#define EndFor }
so you can write the arguably prettier and clearer:

Procedure CrapOnMem( char Buf[], int Len )
var
int i;
code
For( i, 0, Len - 1 )
If i Isnt 22 Then
Buf[i] = '%';
EndIf
EndFor
end
Of course this causes most C aficionados to raise an eyebrow, or sneer,
or feint barfing.

Oct 10 '06 #8
Richard Heathfield wrote:
jmcgill said:
>This one is my all-time favorite.

#define BeginProgram void main(int argc, char *argv[])

So much for Standard C.
Fair enough, but I still get a kick out of this program. It does
something really interesting, and not at all obvious.
Oct 10 '06 #9
Rod Pemberton said:
"Richard Heathfield" <in*****@invali d.invalidwrote in message
news:E5******** *************** *******@bt.com. ..
>Jerret Johnson said:

<snip>
And for those of you who might ask what my C question is ... is this
Standard C?

PRINTF("HI %s!\n", MAKE_UPPER(GETS (NAME)));/*lint !e421 Caution --
*/
<snip>
>It also fails to take into account the possibility that gets() returns
NULL.

?
!
Then why does MAKE_UPPER begin with this?

IF S IS NULL
THEN
RETURN NULL;
ENDIF

It appears to me that it takes the possibility into account, and then
passes the NULL along to printf()...
I was talking about the whole statement. Passing NULL to printf as a match
for %s invokes undefined behaviour.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1720
by: John | last post by:
I am new to c++ programming and i just want to make a program that takes a users name, then converts it to a hex string, then puts it together in a fixed order. An example output would be: Name: John Output: 84721305 this is my generation code so far #include <conio.h>
6
4349
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing result in any way. Who can help me, I thank you very very much. list.cpp(main program) //-------------------------------------------------------------------------- - #pragma hdrstop #pragma argsused
2
1405
by: sunfox | last post by:
Please help!! I have a difficulty in writing an assignment which is related to Visual C++ V6.0. Can anybody here assist me to write a program which is able to run under DOS? The program will be used in Car Park Management, Library Management and Yellow Page Management. For the purpose of fully understanding explanatory notes should be added for each Source Code. Many thanks in advance!!!!
1
3721
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
4
2140
by: Sreekanth | last post by:
Hi all, I have implemented a timing out version of fgets function call. I am pasting the entire code below. I have following doubts: 1. The code which I have written does it follow standard C programming conventions. ( I am pretty familar with java styles but not with c :-( ). 2. In order to read character by character from stdin , I have made use
18
5103
by: pipito | last post by:
Hi...i am a beginner in C programming... my question is like this... im using the scanf in C to accept input data in output area, printf("name: ");scanf("%s",name); printf(Address: ");scanf("%s",address); printf("age: ");scanf("%d",age); when i run the program, i use a spacebar in the input data, the following string after i press the space bar doesnt read anymore or it
1
1941
by: simonalexander | last post by:
I have got a homework task to do and I have started the work but I cannot finish it.Can someone please help me finish the code. The help given is much appreciated. The actual specifications are here: The task for this project is to write a simulator program for a robot designed to move packages around in a warehouse environment. The input to your program (from standard input) will contain a map of the environment in its original...
21
2552
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to run, However, i simply created this program in arrays and it runs good except i cant figure out how to compute the standard deviation. The coding is below. Any help will be appreciated. 1) The Program will prompt the user for six grades to be...
9
1934
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is like this : (very simple..........) 010001010110001101010101010101010101010101 #include<stdio.h>
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.