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 |
void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp; printf(“%d”,k); }
#include <stdio.h> main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr<least ) ?*ptr :least; printf("%d",least); }
main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }
main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }
{ int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }
plz send me all data structure related programs
how to return a multiple value from a function?
main() { int i; float *pf; pf = (float *)&i; *pf = 100.00; printf("\n %d", i); } a. Runtime error. b. 100 c. Some Integer not 100 d. None of the above
void main() { printf(“sizeof (void *) = %d \n“, sizeof( void *)); printf(“sizeof (int *) = %d \n”, sizeof(int *)); printf(“sizeof (double *) = %d \n”, sizeof(double *)); printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *)); }
#define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); }
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.
void pascal f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } void cdecl f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } main() { int i=10; f(i++,i++,i++); printf(" %d\n",i); i=10; f(i++,i++,i++); printf(" %d",i); }