Solved

Just for fun III - Prints self out ...

Posted on 1998-08-19
8
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 6

Expert Comment

by:snoegler
ID: 1252179
that is a bit shorter :)
0
 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

627 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