Is it possible to write a code that prints 'its own code' when executed?
Just a thought.

Recommended Answers

All 18 Replies

just copy paste the code into the quotes and add \n for line breaks

That will not print the code as it is, will it?

just copy paste the code into the quotes and add \n for line breaks

That will not print the code as it is, will it?

Define "as it is". If you meant as you typed then yes, it will. If however you meant the very code you wrote, I don't think it's possible since you code has pretty much been compiled to your object code/assembly. Even the most advance disassemblers would by far convert it into assembly code but cannot back track into the original code, which may either be C++, Delphi or Pascal.

the latter is what i intended..
so no code can be regenerated once compiled?

>>so no code can be regenerated once compiled?
No.

Your code will not look like a code as you see once compile because it will be turned into machine code which could be varied depending on what compiler you use. The reason is that a compiler has its own code optimization algorithm and that could change the result after compiling your code. You could assume that each compiler would use the same algorithm, but that is not always true.

> Is it possible to write a code that prints 'its own code' when executed?
Yes, it's called a quine.

commented: Nice +4

Oh nice one. Thanks :)

Yes, it's called a quine.

Out of curiosity, I check that link & from what I'm making of it, it's limited to specific programs where programmers *intelligently* type in the same code in strings.
It's always good to know something new & I thank you for it, though the OP wanted something else.

> it's limited to specific programs where programmers *intelligently* type in the same code in strings
Not always, but that is the most common method in the absence of reflection. The simplest C++ quine that Ed has come up with does not use that method.

#include <fstream>
#include <iostream>

int main()
{
    std::ifstream ifs(__FILE__);

    if (ifs)
        std::cout << ifs.rdbuf();
}

> though the OP wanted something else
Edward respectfully disagrees. A quine is exactly what the OP asked for: a program that prints its own source code. A disassembler is not the same thing.

A quine is exactly what the OP asked for: a program that prints its own source code.

I beg to differ going by OPs comments:

the latter is what i intended..
so no code can be regenerated once compiled?

I think he meant a compiled executable being able to print its code.

std::ifstream ifs(__FILE__);

I assume your program prints it's own code using the __FILE__ parameter which is the path to the source code, kindly correct me if I'm wrong.

> I beg to differ going by OPs comments:
Then we will differ. Ed does not want to get into a pointless argument. The OP can pick whichever answer is a better fit, and ask for clarification as necessary. ;)

> I assume your program prints it's own code using the __FILE__ parameter which is the path to the source code
That is how it works, yes.

I know a way but it'd be goofy I did it for one of my homework assignments.

I know a way but it'd be goofy I did it for one of my homework assignments.

And which assembly you convert into? ;) Also, did you do code optimization before you convert it? :) Or you really did print out your code after the code has been compiled? :)

Thank you guys..
Quine was what i was looking for.

But a quine does not print its code by openin the code as a file. Its is done by programming logic.
I came through an example which i have placed below

#include <stdio.h>

int
main (void)
{
  char *s1="#include <stdio.h>%c%cint%cmain (void)%c{%c";
  char *s2="  char *s%c=%c%s%c;%c  char *s%c=%c%s%c;%c";
  char *s3="  char n='%cn', q='%c', b='%c%c';%c";
  char *sp="  printf(";
  char *s4="%ss1,n,n,n,n,n);%c";
  char *s5="%ss2,'1',q,s1,q,n,'2',q,s2,q,n);%ss2,'3',q,s3,q,n,'p',q,sp,q,n);%c";
  char *s6="%ss2,'4',q,s4,q,n,'5',q,s5,q,n);%ss2,'6',q,s6,q,n,'7',q,s7,q,n);%c";
  char *s7="%ss2,'8',q,s8,q,n,'9',q,s9,q,n);%ss2,'0',q,s0,q,n,'x',q,sx,q,n);%c";
  char *s8="%ss3,b,q,b,b,n);%ss4,sp,n);%ss5,sp,sp,n);%c";
  char *s9="%ss6,sp,sp,n);%ss7,sp,sp,n);%ss8,sp,sp,sp,n);%c";
  char *s0="%ss9,sp,sp,sp,n);%ss0,sp,sp,n,n,n);%c  return 0;%c}%c";
  char *sx="--- This is an intron. ---";
  char n='\n', q='"', b='\\';
  printf(s1,n,n,n,n,n);
  printf(s2,'1',q,s1,q,n,'2',q,s2,q,n);  printf(s2,'3',q,s3,q,n,'p',q,sp,q,n);
  printf(s2,'4',q,s4,q,n,'5',q,s5,q,n);  printf(s2,'6',q,s6,q,n,'7',q,s7,q,n);
  printf(s2,'8',q,s8,q,n,'9',q,s9,q,n);  printf(s2,'0',q,s0,q,n,'x',q,sx,q,n);
  printf(s3,b,q,b,b,n);  printf(s4,sp,n);  printf(s5,sp,sp,n);
  printf(s6,sp,sp,n);  printf(s7,sp,sp,n);  printf(s8,sp,sp,sp,n);
  printf(s9,sp,sp,sp,n);  printf(s0,sp,sp,n,n,n);
  return 0;
}
But a quine does not print its code by openin the code as a file. Its is done by programming logic.

That's what is stated. True.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.