CE Home
Navigation
Go Back   CrazyEngineers Forum > CE : Technical Discussions > Computer Science & IT Engineering
Notices


Advertisements
Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)
Old 24th August 2008, 08:47 PM
Good Administrator
 
The_Big_K's Avatar
 
Join Date: 26th November 2005
Location: Terra-Firma
I'm a Crazy Electrical Engineer
Posts: 5,673
Send a message via Yahoo to The_Big_K
Lightbulb 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.
The_Big_K is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)
Old 25th August 2008, 02:26 PM
Good Administrator
 
The_Big_K's Avatar
 
Join Date: 26th November 2005
Location: Terra-Firma
I'm a Crazy Electrical Engineer
Posts: 5,673
Send a message via Yahoo to The_Big_K
Default Re: C Challenge: Self-Printing Program

Hah, No one?

Too tough?
The_Big_K is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)
Old 25th August 2008, 02:40 PM
ash
Moderator
 
ash's Avatar
 
Join Date: 12th July 2007
Location: IIUM, Malaysia
I'm a Crazy Communications Engineer
Posts: 1,668
Default 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.
| New to CE? Click here! | Join our CE Bot project! | Problems? Questions? PM or mail me at ash{at]crazyengineers{dot]com |
ash is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)
Old 25th August 2008, 04:31 PM
CE - Addict
 
anuragh27crony's Avatar
 
Join Date: 12th January 2006
Location: INDIA
I'm a Crazy Computer Engineer
Posts: 341
Send a message via Yahoo to anuragh27crony
Default 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";
anuragh27crony is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)
Old 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
Posts: 75
Send a message via AIM to bayazidahmed Send a message via MSN to bayazidahmed Send a message via Yahoo to bayazidahmed
Default Re: C Challenge: Self-Printing Program

Quote:
Originally Posted by The_Big_K View Post
Hah, No one?

Too tough?
Nah! Too easy.
bayazidahmed is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)
Old 26th August 2008, 12:43 AM
Good Administrator
 
The_Big_K's Avatar
 
Join Date: 26th November 2005
Location: Terra-Firma
I'm a Crazy Electrical Engineer
Posts: 5,673
Send a message via Yahoo to The_Big_K
Default Re: C Challenge: Self-Printing Program

Alright, alright!

Why don't you help others come up with a solution then?
The_Big_K is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)
Old 30th November 2008, 04:51 PM
CE - Regular Member
 
Ashutosh_shukla's Avatar
 
Join Date: 16th November 2008
Location: India
I'm a Crazy Computer Science & Engineering Engineer
Posts: 54
Send a message via Yahoo to Ashutosh_shukla
Default 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();
}
Ashutosh_shukla is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)
Old 2nd December 2008, 04:50 PM
CE - Newbie
 
Join Date: 18th November 2008
I'm a Crazy information technology Engineer
Posts: 4
Default Re: C Challenge: Self-Printing Program

Execution of above code always says that "file cannot be opened"
anusha.p.s is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)
Old 2nd December 2008, 08:06 PM
CE - Regular Member
 
Ashutosh_shukla's Avatar
 
Join Date: 16th November 2008
Location: India
I'm a Crazy Computer Science & Engineering Engineer
Posts: 54
Send a message via Yahoo to Ashutosh_shukla
Default Re: C Challenge: Self-Printing Program

you save it as prog1.c in bin folder of turbo c then run it
Ashutosh_shukla is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)
Old 4th December 2008, 08:58 AM
CE - Newbie
 
Join Date: 4th December 2008
I'm a Crazy Computer Engineer
Posts: 1
Default 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
muts is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
program to print its code, self printing program

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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