Quine in programming terms is a program which outputs its own Source code. These are self-replicating programs and are very popular in the programming community. So, in this article we will see how we can achieve this.
This problem may sound complex, but this is actually quite easy and simple. So, the way we proceed with this is by opening the source file from the program and print out each and every character.
Sounds simple, doesn't it? Lets code it.
Things you need to know for this : Usage of File Pointers

Procedure: Lets break down the approach step by step
So, if you execute the following statement, the output will be the location as shown
This problem may sound complex, but this is actually quite easy and simple. So, the way we proceed with this is by opening the source file from the program and print out each and every character.
Sounds simple, doesn't it? Lets code it.
Things you need to know for this : Usage of File Pointers

Procedure: Lets break down the approach step by step
- Open the current file (fopen()) using a file pointer (in Read Mode)
- Read characters one after the other and print them until you reach the end of the file (EOF)
- Close the file
So, if you execute the following statement, the output will be the location as shown
printf(__FILE__); Output: C:\Users\DSP\Desktop\selfProgram.cNow, you know everything there is to know about this. So, let's write the program.
#include <stdio.h> main() { FILE *fp; // file pointer declaration char c; // variable to store the characters from the file fp = fopen(__FILE__,"r"); // opens the file in Read mode // Location argument specified by _FILE_ macro do { c = fgetc(fp); //read a character and store in c putchar(c); // print the char on to the screen } while(c!=EOF); // continue till we reach End Of File fclose(fp); // close the file to avoid file corruption printf("\n"); }And, the output as you can expect will be the same as shown.
So,
That is all for this article. If you like this article you might like
our other articles too about Programming. So, check them out. Also stay
tuned for a lot of other interesting articles.
As, always, Have a happy reading and Stay Awesome !
-------------------------------------------------------------------------------------------------
Follow our blog posts @ Follow. So that you won't miss any interesting post and also to be the first to know the answers to many interesting questions.
Follow us on our Facebook page @ Fre Blogg
Head over to my You Tube channel for a lot of interesting tutorials @ You Tube
0 comments:
Post a comment
Please Enter your comment here......