Self

From CS371p

Jump to: navigation, search

Written by Kenneth Logan

Contents

INTRO

Self is a program that outputs exactly its own source code and nothing else without reading any input.

Programmers often try to write the shortest possible quine for many programming languages.

For example,the shortest program for Scheme is:

((lambda (x) (list x (list 'quote x))) '(lambda (x) (list x (list 'quote x))))

In our case (for CS371p), we need to do this for C++.

The trick for C++ is to have two things.

1. A string that you want to output.

2. A print statement that prints the string which results in the same string as the program itself.

HINTS

Using printf is probably the best way to output your text.

SPECIAL ASCII CHARACTERS

quotes -> 34

newline -> 10

For example:

char *s = "test1%c%ctest2%c";

printf(s, 10, 34, 34);


TESTING YOUR PROGRAM

When you write your program, run the following commands to see if your program is the same:

g++ self.c++ -o self.exe

./self.exe > foo_exe

cat self.c++ > foo_cat

diff foo_cat foo_exe

If nothing is output after the diff, then your program works!

Otherwise, take a look at what the diff is saying and debug your code.


ONE LAST COMMENT

When I wrote this code, I used printf, and therefore I needed an include statement. G++ gives you an error if you have code after a library is being included so don't forget to add a new line after the #include.

Personal tools
papers