How do you write a program which produces its own source
code as its output?
Answers were Sorted based on User's Feedback
Answer / lijun li
#include <stdio.h>
main()
{
printf("%s\n", __FILE__);
FILE* fp = fopen(__FILE__, "r");
char buf[4096];
while (!feof(fp))
{
fgets(buf, 4096, fp);
printf("%s",buf);
}
fclose(fp);
}
Is This Answer Correct ? | 11 Yes | 4 No |
Answer / aditya raj
/*In above solution, data.txt contains d source code. But
its not a good solution. Plz post some better solution */
main(s)
{
s="main(s){s=%c%s%c;printf(s,34,s,34);}";
printf(s,34,s,34);
}
Is This Answer Correct ? | 9 Yes | 4 No |
Answer / aditya raj
#include<stdio.h>
main()
{
char n;
FILE *f;
f=fopen("data.txt","r");
while((fscanf(f,"%c",&n))!=EOF)
printf("%c",n);
fclose(f);
}
Is This Answer Correct ? | 3 Yes | 1 No |
Answer / splurgeop
// PROGRAM which prints itself
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<fstream.h>
void main()
{
char ch;
clrscr();
fstream fout;
fout.open("itself.c",ios::in);
if(!fout)
{
printf("\n cant open");
exit(0);
}
while(ch!=EOF)
{
ch=fout.get();
cout<<ch;
}
fout.close();
}
//the file name is itself.c
Is This Answer Correct ? | 9 Yes | 9 No |
C statement to copy a string without using loop and library function..
write a program to count the number the same (letter/character foreg: 's') in a given sentence.
How to return multiple values from a function?
main() { int i = 0xff ; printf("\n%d", i<<2); } a. 4 b. 512 c. 1020 d. 1024
To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']
Write a C program to print ‘Campus Force training’ without using even a single semicolon in the program.
3) Int Matrix of certain size was given, We had few valu= es in it like this. =97=97=97=97=97=97=97=97=97=97=97 1 = | 4 | | 5 | &= nbsp; | 45 =97=97=97=97=97=97=97=97=97=97=97 &n= bsp; | 3 | 3 | 5 | = | 4 =97=97=97=97=97=97=97=97=97=97=97 34 |&nbs= p; 3 | 3 | | 12 | &= nbsp; =97=97=97=97=97=97=97=97=97=97=97 3 | &nbs= p; | 3 | 4 | = | 3 =97=97=97=97=97=97=97=97=97=97=97 3 | = ; | | | = ; 3 | =97=97=97=97=97=97=97=97=97=97=97 &= nbsp; | | 4 | = ; | 4 | 3 We w= ere supposed to move back all the spaces in it at the end. Note: = If implemented this prog using recursion, would get higher preference.
Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it. You are allowed to destroy the array if you like.
21 Answers ABC, eBay, Goldman Sachs, Google, HUP, Microsoft, TATA,
int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); }
write a origram swaoing valu without 3rd variable
main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; }
There are 21 people in a room. They have to form groups of 3 people each. How many combinations are possible? Write a C program to print the same.