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 |
Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number
struct Foo { char *pName; char *pAddress; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); obj->pName = malloc(100); obj->pAddress = malloc(100); strcpy(obj->pName,"Your Name"); strcpy(obj->pAddress, "Your Address"); free(obj); printf("%s", obj->pName); printf("%s", obj->pAddress); } a. Your Name, Your Address b. Your Address, Your Address c. Your Name Your Name d. None of the above
main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }
find simple interest & compund interest
void ( * abc( int, void ( *def) () ) ) ();
Is the following code legal? typedef struct a { int x; aType *b; }aType
Program to Delete an element from a doubly linked list.
4 Answers College School Exams Tests, Infosys,
#include"math.h" void main() { printf("Hi everybody"); } if <stdio.h> will be included then this program will must compile, but as we know that when we include a header file in "" then any system defined function find its defination from all the directrives. So is this code of segment will compile? If no then why?
write a program in c language to get the value of arroy keys pressed and display the message which arrow key is pressed?
#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }
Write a complete program that consists of a function that can receive two numbers from a user (M and N) as a parameter. Then print all the numbers between the two numbers including the number itself. If the value of M is smaller than N, print the numbers in ascending flow. If the value of M is bigger than N, print the numbers in descending flow. may i know how the coding look like?
main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }