C Program to Print its Own Source Code

C Program to Print its Own Source CodeEver wondered how to write a C program to print its own source code? Well, here is the source code of a C program that when executed will print its own source code. In other words, the output of this program is exactly same as its source code.

Here’s the program:

#include<stdio.h>

char *program=”#include<stdio.h>%cchar *program=%c%s%c;%cvoid main()%c{%cprintf(program,10,34,program,34,10, 10,10,10);%c}”;

void main()
{
printf(program,10,34,program,34,10,10,10,10);
}

20 Comments

  1. Hitesh
    December 14, 2008 at 12:31 PM

    Hey Srikanth, can u please send me such type of c programs @ hitesh.m01@gmail.com.
    this is a great site which helped me a lot
    great work is done by u for learners of c Keep it up.


  2. Slim0123
    December 31, 2008 at 1:32 PM

    how about one more the smallest quine ever

    main() { char *s=”main() { char *s=%c%s%c; printf(s,34,s,34); }”; printf(s,34,s,34); }


  3. JAIDEEP
    January 14, 2009 at 11:26 AM

    hello Srikanth,
    can u please mail me some game source codes


  4. Learning C
    February 9, 2009 at 9:37 PM

    Can u please explain the code????


  5. biswajit das
    February 20, 2009 at 4:00 PM

    Hi
    srikanth please send me c code which is destroy the another exe files of windows


  6. Jagadish Kumar
    June 3, 2009 at 9:40 PM

    #include
    main()
    {
    FILE *fp=fopen(__FILE__,”r”);
    char ch;
    printf(“Hello World\n”);
    while(!feof(fp))
    {
    ch=fgetc(fp);
    printf(“%c”,ch);
    }
    }

    This program also prints it out source code as output .


  7. aparajit
    November 8, 2009 at 5:34 PM

    can we use this program to get source code of exe file that has been coded in c language ,please mail it to aparajitlal.kumar@gmail.com

    with thanks.


  8. satish
    July 6, 2011 at 9:38 PM

    can it possible to run a .exe or .mp3 or .jpeg or etc… files through
    c or c++


  9. raju
    July 25, 2011 at 12:52 AM

    nice trick thanx


  10. mangesh
    July 25, 2011 at 12:53 AM

    thanx you sir


  11. SH@HID
    March 7, 2012 at 12:19 PM

    Sir,
    very nice programes
    very much useful for me
    Thaks for this


  12. Raj
    November 11, 2012 at 6:47 PM

    I am unable to understand this code(source code as output) please explain me.


  13. Raj
    November 11, 2012 at 7:01 PM

    Could you please tell me how to convert C program(source code) to software or application.


  14. Gangadhar
    November 29, 2012 at 2:47 PM

    what will be the output of followng c program:

    1.main()
    {
    int i=10;

    i=!i>14;
    printf(“i=%d”,i);
    }

    and second program is

    2.#define square(X)x*x

    main()
    {
    int i;
    i=64/square(4);
    printf(“%d”,i);
    }

    if possible pls explain it for me…
    thnks


    • Vignesh
      January 1, 2013 at 2:47 PM

      The second one is possible !
      The square(x)x (simply modified by the preprocessor) the x which is passed, For example if we pass the value of x as 4, it will be replaced by the preprocessor as 4, consider square(4)4*4:
      square(4)4 replaced by 4 and multiplied by 4 so the value is 16

      #include
      #define square(x)x*x

      int main()
      {
      int i;
      i=64/(square(4));
      printf(“%d”,i);
      return 0;
      }

      In the above code snippet (Square(4)) will be replaced by 16 so 64 divided by 16 is 4
      The Possible Output is 4.


  15. kRishna tEj
    December 26, 2012 at 8:37 PM

    HI, bro i’m a big fan of your site i had learnt so many thing from your blog
    i just want to ask you something ….
    1.You ever wondered to create page in your site which tutors the c language?
    2.where can i found a complete tutorial to c language from A to Z?.


    • Srikanth
      January 2, 2013 at 12:23 PM

      You can try: cprogramming.com


  16. madhu
    June 1, 2013 at 12:18 PM

    can u please explain me the code ….


  17. vishal
    May 1, 2014 at 6:24 AM

    hello sir

    sir i Iike so much this article related to creating a virus which given by you.

    please let me know how to create a virus to transfer any data. from D: directory to any portable USB

    thank u


Leave A Reply