Printing source code of a C program itself Last Updated : 26 Jul, 2023 Comments Improve Suggest changes 1 Likes Like Report Printing the source code of a C program itself is different from the Quine problem. Here we need to modify any C program in a way that it prints the whole source code. Recommended: Please try your approach on {IDE} first, before moving on to the solution.ApproachUse predefined macro __FILE__ to get the location of the file.Open the source code file in reading mode and get the file pointer fptr. Check if the file is successfully opened or not.Read all the contents of the file using the do-while loop and fgetc.Close the file using the fclose function.1. FILE Macro Method(Printing File Name)We can use the concepts of file handling to print the source code of the program as output. The idea is to display the content from the same file you are writing the source code. The location of a C programming file is contained inside a predefined macro __FILE__. Below is the C program to use __FILE__: C // C program to display the // location of the file #include <stdio.h> // Driver code int main() { // Prints location of C this C code. printf("%s", __FILE__); } The output of the above program is the location of this C file. ImplementationThe following program displays the content of this particular C file(source code) because __FILE__ contains the location of this C file in a string. 2. Display the Program C // C program that prints its source code. #include<stdio.h> // Driver code int main(void) { // We can append this code to any C program // such that it prints its source code. char c; FILE *fp = fopen(__FILE__, "r"); do { c = fgetc(fp); putchar(c); } while (c != EOF); fclose(fp); return 0; } Output: Note: The above program may not work online compiler as fopen might be blocked. Create Quiz Comment K kartik Follow 1 Improve K kartik Follow 1 Improve Article Tags : C Language Explore C BasicsC Language Introduction6 min readIdentifiers in C3 min readKeywords in C2 min readVariables in C4 min readData Types in C3 min readOperators in C8 min readDecision Making in C (if , if..else, Nested if, if-else-if )7 min readLoops in C6 min readFunctions in C5 min readArrays & StringsArrays in C4 min readStrings in C5 min readPointers and StructuresPointers in C7 min readFunction Pointer in C5 min readUnions in C3 min readEnumeration (or enum) in C5 min readStructure Member Alignment, Padding and Data Packing8 min readMemory ManagementMemory Layout of C Programs5 min readDynamic Memory Allocation in C7 min readWhat is Memory Leak? How can we avoid?2 min readFile & Error HandlingFile Handling in C11 min readRead/Write Structure From/to a File in C3 min readError Handling in C8 min readUsing goto for Exception Handling in C4 min readError Handling During File Operations in C5 min readAdvanced ConceptsVariadic Functions in C5 min readSignals in C language5 min readSocket Programming in C8 min read_Generics Keyword in C3 min readMultithreading in C9 min read Like