ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
How do you write a program which produces its own source 
code as its output?
 Question Submitted By :: Swapna
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How do you write a program which produces its own source code as its output?
Answer
# 1
//      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 ?    5 Yes 6 No
Splurgeop
 
  Re: How do you write a program which produces its own source code as its output?
Answer
# 2
Can u write above program in C
 
Is This Answer Correct ?    1 Yes 4 No
Chandu
 
 
 
  Re: How do you write a program which produces its own source code as its output?
Answer
# 3
#include <stdio.h>
main()
 {
   FILE *fd;
   int c;

   fd= fopen("./file.c","r");
   while ( (c=fgetc(fd)) != EOF)
   {
     printf("%c", c);
   }
   
   fclose(fd);
 }
 
Is This Answer Correct ?    7 Yes 3 No
Ram
 
  Re: How do you write a program which produces its own source code as its output?
Answer
# 4
#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 ?    4 Yes 1 No
Lijun Li
 
  Re: How do you write a program which produces its own source code as its output?
Answer
# 5
#include<stdio.h>
int main(int argc,char *argv[])
{
 FILE *fp;
 char ch;
 fp=fopen(argv[0],"r");
 while(!feof(fp))
 {
   ch=fgetc(fp);
   printf("%c",ch);
 }
}

Compile the program as cc -o filename filename.c
and run as
./filename
 
Is This Answer Correct ?    0 Yes 2 No
Yogesh
 
  Re: How do you write a program which produces its own source code as its output?
Answer
# 6
#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 ?    0 Yes 0 No
Aditya Raj
 
  Re: How do you write a program which produces its own source code as its output?
Answer
# 7
/*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 ?    1 Yes 1 No
Aditya Raj
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }  1
main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); }  1
main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); }  1
what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);  1
Write a procedure to implement highlight as a blinking operation  1
#define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); }  1
void main() { int i=i++,j=j++,k=k++; printf(“%d%d%d”,i,j,k); }  1
Is the following code legal? typedef struct a { int x; aType *b; }aType  1
#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); }  1
union u { union u { int i; int j; }a[10]; int b[10]; }u; main() { printf("\n%d", sizeof(u)); printf(" %d", sizeof(u.a)); // printf("%d", sizeof(u.a[4].i)); } a. 4, 4, 4 b. 40, 4, 4 c. 1, 100, 1 d. 40 400 4 HCL1
main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); }  1
void ( * abc( int, void ( *def) () ) ) ();  1
main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }  1
What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }  1
main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); }  1
#define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); }  1
Is the following code legal? typedef struct a aType; struct a { int x; aType *b; };  1
main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }  1
void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); }  1
main() { extern out; printf("%d", out); } int out=100;  1
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com