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 kartik Follow Improve Article Tags : C Language Similar Reads C program to detect tokens in a C program As it is known that Lexical Analysis is the first phase of compiler also known as scanner. It converts the input program into a sequence of Tokens. A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.For Example: 1) Keyword 5 min read 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 Output of C Programs | Set 6 Predict the output of below programs Question 1 c int main() { unsigned int i=65000; while ( i++ != 0 ); printf("%d",i); return 0; } Output: 1 Explanation: It should be noticed that there's a semi-colon in the body of while loop. So even though, nothing is done as part of while body, the c 4 min read Output of C Program | Set 17 Predict the output of following C programs. Question 1 C #include<stdio.h> #define R 10 #define C 20 int main() { int (*p)[R][C]; printf("%d", sizeof(*p)); getchar(); return 0; } Output: 10*20*sizeof(int) which is "800" for compilers with integer size as 4 bytes. The pointer p is de- 2 min read Output of C Program | Set 29 Question 1 How many times main() will get called? C main() { printf("\n main Called Again"); main(); } Answer : Infinite loop Description : There is no condition in the main() to stop the recursive calling of the main() hence it will be called infinite no of times. Question 2 Guess the out 6 min read Output of C programs | Set 61 (Loops) Prerequisite : Loops in C Q.1 What is the output of this program? CPP #include <iostream> using namespace std; int main() { int i, j, var = 'A'; for (i = 5; i >= 1; i--) { for (j = 0; j < i; j++) printf("%c ", (var + j)); printf("\n"); } return 0; } Options a)A B C D 3 min read Like