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
Solved

Just for fun III - Prints self out ...

Posted on 1998-08-19
8
181 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.

860 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