Printing source code of a C program itself Last Updated : 26 Jul, 2023 Comments Improve Suggest changes Like Article 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. Comment More infoAdvertise with us Next Article Printing source code of a C program itself K kartik Follow Improve Article Tags : C Language Similar Reads Output of C Programs | Set 1 Predict the output of below programs. Question 1c#include<stdio.h> int main() { int n; for(n = 7; n!=0; n--) printf("n = %d", n--); getchar(); return 0; }Output: Above program goes in infinite loop because n is never zero when loop condition (n != 0) is checked.Question 2c #include<stdio.h 2 min read Output of C Program | Set 21 Predict the output of following C programs. Question 1 C #include<stdio.h> #define fun (x) (x)*10 int main() { int t = fun(5); int i; for(i = 0; i < t; i++) printf("GeeksforGeeks\n"); return 0; } Output: Compiler Error There is an extra space in macro declaration which causes fun 2 min read Output of C Programs | Set 5 Predict the output of below programs Question 1 c int main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } return 0; } Output: Can't be predictedExplanation: The condition in while loop is 1 so at first shot it looks infinite loop. Then there are break and con 5 min read Output of C Program | Set 27 Predict the output of following C programs. Question 1 C #include <stdio.h> int main(void) { int i; int power_of_ten[5] = { 00001, 00010, 00100, 01000, 10000, }; for (i = 0; i < 5; ++i) printf("%d ", power_of_ten[i]); printf("\n"); return 0; } In the above example, we ha 3 min read Output of C programs | Set 8 Predict the output of below C programs. Question 1: c #include<stdio.h> int main() { int x = 5, p = 10; printf("%*d", x, p); getchar(); return 0; } Output: 10 Explanation: Please see standard printf function definition int printf ( const char * format, ... ); format: String that cont 3 min read Like