Solved

Just for fun III - Prints self out ...

Posted on 1998-08-19
8
178 Views
Last Modified: 2010-04-15
Simple exercise: Program that prints itself out (Only executable version is available at runtime!!!)

Objective: a suitable add-on for copyright software? (just kidding)

There is a solution ...
0
Comment
Question by:newexpert
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 2

Expert Comment

by:kellyjj
ID: 1252176
Are you talking about printing source code? or the
binary???  
0
 
LVL 6

Accepted Solution

by:
snoegler earned 0 total points
ID: 1252177
#include <stdio.h>
char *text[]={"char *text[]={","%s%c%s%c,%c%s%c,%c%s%c,%c%s%c,%c%s%c,%c%s%c};%c%c","void main(){","puts(text[5]);printf(text[1],text[0],34,text[0],34,34,text[1],34,34,text[2],34,34,text[3],34,34,text[4],34,34,text[5],34,13,10);","puts(text[2]);puts(text[3]);printf(text[4]);}","#include <stdio.h>"};
void main(){
puts(text[5]);printf(text[1],text[0],34,text[0],34,34,text[1],34,34,text[2],34,34,text[3],34,34,text[4],34,34,text[5],34,13,10);
puts(text[2]);puts(text[3]);printf(text[4]);}

I've tested it, it works - but it's pretty ugly :)
0
 
LVL 84

Expert Comment

by:ozo
ID: 1252178
The standard example, fron the comp.lang.c FAQ, is:

char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,34,s,34);}
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1252179
that is a bit shorter :)
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 1

Author Comment

by:newexpert
ID: 1252180
How about the one I had in mind (it even does some extra things):
#include "pself.h"
PH(int main(void) {)
PA(int i; int sum = 0; for (i = 1; i <= 100; i++) sum += i;)
PB(return sum; })

The actual code that does the hard work is conveniently hidden in pself.h
#define PH(x) x puts("#include \"pself.h\"\nPH("#x")");
#define PA(x) x; puts("PA("#x")");
#define PB(x) puts("PB("#x")"); x

0
 
LVL 84

Expert Comment

by:ozo
ID: 1252181
Hmm, if you allow that, then how about just this:

#include "pself.h"

with pself.h containg:
main(){
  int i; int sum = 0;
  for (i = 1; i <= 100; i++) sum += i;
  puts("#include \"pself.h\"\n");
  return sum;
}

Or this:

X

compiled with
cc -DX='main(){puts("X");}'


0
 
LVL 1

Author Comment

by:newexpert
ID: 1252182
Good point.  Wonder how hard would it be to create a .h which source dump any .c file that includes it?
0
 
LVL 1

Author Comment

by:newexpert
ID: 1252183
Where did the last word "it" go?
0

Featured Post

Too many email signature changes to deal with?

Are you constantly being asked to update your organization's email signatures? Do they take up too much of your time? Wouldn't you love to be able to manage all signatures from one central location, easily design them and deploy them quickly to users. Well, you can!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to save 10 147
Windows Updates failing due to Diskpart not configured correctly 8 103
Raspberry Pi 3 to send text message 9 65
gdb doesn't stop on breakpoint 2 45
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now