C Program to Display Program Name and Its Own Source Code

Souce code

C program to display program name and its own source code has been given here. It prints the whole code character by character and the name of the program.



Page content(s):

1. Program & Output




1. Program & output to Display the program name and Its Own Source Code

Code has been copied
/**************************************
           alphabetacoder.com
    C program to display source program 
	name and its own source code
***************************************/

#include<stdio.h>

int main(int argc, char * argv[]) {
    // delclare variables
    FILE * f;
    char ch;

    // display source program name
    printf("Source program name: %s\n\n", argv[0]);

    // open the source in read mode 
    f = fopen(__FILE__, "r");

    // read each character from source file
    // print each character until End of file (EOF)
    while ((ch = fgetc(f)) != EOF)
        printf("%c", ch);

    // close the file
    fclose(f);

    return 0;
}

Output


Source program name: C:\test.exe

/**************************************
           alphabetacoder.com
    C program to display source program
        name and its own source code
***************************************/

#include<stdio.h>

int main(int argc, char *argv[])
{
    // delclare variables
    FILE *f;
    char ch;

    // display source program name
    printf("Source program name: %s\n\n", argv[0]);

    // open the source in read mode
    f = fopen(__FILE__,"r");

    // read each character from source file
    // print each character until End of file (EOF)
    while((ch = fgetc(f)) != EOF)
        printf("%c", ch);

    // close the file
    fclose(f);

    return 0;
}