|
|
 |
|

24th August 2008, 08:47 PM
|
Good Administrator
Join Date: 26th November 2005
I'm a Crazy Electrical Engineer
|
C Challenge: Self-Printing Program
Source: Programming Challenges - Self-Printing Program - Cprogramming.com
Here's a nice challenge for all you programming freaks.
Write a program that, when run, will print out its source code. This source code, in turn, should compile and print out itself.
Discuss the code with fellow CEans in this thread. Googling is good, but it doesn't add value to your knowledge.
|
|
|

25th August 2008, 02:26 PM
|
Good Administrator
Join Date: 26th November 2005
I'm a Crazy Electrical Engineer
|
Re: C Challenge: Self-Printing Program
Hah, No one?
Too tough?
|
|
|

25th August 2008, 02:40 PM
|
Moderator
Join Date: 12th July 2007
I'm a Crazy Communications Engineer
|
Re: C Challenge: Self-Printing Program
That sounds doable  Have a function to copy itself and read out its source code.
*rubs chin*
__________________
Keep it simple. Keep it real. 
|
|
|

25th August 2008, 04:31 PM
|
CE - Addict
Join Date: 12th January 2006
I'm a Crazy Computer Engineer
|
Re: C Challenge: Self-Printing Program
Good question...this question i have encountered....during my placements.....
Simple technique is ...to use %s in a pointer to char which contains itself while printing.
char *temp="include<stdio.h> void main() { char *temp=%s ...and so on";
|
|
|

26th August 2008, 12:24 AM
|
CE - Regular Member
Join Date: 19th March 2007
Location: Jeddah, Saudi Arabia
I'm a Crazy Electronics and Communication Engineer
|
Re: C Challenge: Self-Printing Program
Quote:
Originally Posted by The_Big_K
Hah, No one?
Too tough?
|
Nah! Too easy.
|
|
|

26th August 2008, 12:43 AM
|
Good Administrator
Join Date: 26th November 2005
I'm a Crazy Electrical Engineer
|
Re: C Challenge: Self-Printing Program
Alright, alright!
Why don't you help others come up with a solution then? 
|
|
|

30th November 2008, 04:51 PM
|
CE - Regular Member
Join Date: 16th November 2008
I'm a Crazy Computer Science & Engineering Engineer
|
Re: C Challenge: Self-Printing Program
If the following code is saved as PROG1.C then on running this code output is the code itself.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char str[80];
int i=0;
clrscr();
fp=fopen("PROG1.C","r");
if(fp==NULL)
{
printf("File could not be opened.");
getch();
exit(1);
}
while(fgets(str,80,fp)!=NULL)
{
i++;
printf("%d",i);
printf("%s",str);
}
getch();
}
|
|
|

2nd December 2008, 04:50 PM
|
CE - Newbie
Join Date: 18th November 2008
I'm a Crazy information technology Engineer
|
Re: C Challenge: Self-Printing Program
Execution of above code always says that "file cannot be opened"
|
|
|

2nd December 2008, 08:06 PM
|
CE - Regular Member
Join Date: 16th November 2008
I'm a Crazy Computer Science & Engineering Engineer
|
Re: C Challenge: Self-Printing Program
you save it as prog1.c in bin folder of turbo c then run it
|
|
|

4th December 2008, 08:58 AM
|
CE - Newbie
Join Date: 4th December 2008
I'm a Crazy Computer Engineer
|
Re: C Challenge: Self-Printing Program
Quote:
#include <stdio.h>
char *program = "#include <stdio.h>%cchar *program = %c%s%c;%cint main()%c{%c
printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);%c return 0;%c}%c";
int main()
{
printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);
return 0;
}
|
tell me about that
|
|
|
 |
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +5.5. The time now is 12:23 PM.
Powered by vBulletin® Version 3.7.4 Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Member comments are owned by the poster. Copyright © 2005-2008 CrazyEngineers.com. All rights reserved.
|
|
| Advertisements |
|
|
|
|