hartnn
  • hartnn
Write a program in C which outputs its own source code.
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
hartnn
  • hartnn
i don't want the exact code, just discuss different logic with which we can do this....
KonradZuse
  • KonradZuse
Sounds pretty crazy...
hartnn
  • hartnn
yes...

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

KonradZuse
  • KonradZuse
Lets see now....
KonradZuse
  • KonradZuse
And the source code has to actually do something itself huh?
hartnn
  • hartnn
no restriction...it can do something else....or it can just output its own source code...
KonradZuse
  • KonradZuse
Cuz technically you could just write a whole bunch of nonsense as a string ansd then print that :P.
KonradZuse
  • KonradZuse
Idk why you would ever need to do this, is this some BS school assigment?
hartnn
  • hartnn
not assignment...just curious to know how..
KonradZuse
  • KonradZuse
Yeah I'm stumped on this one atm....
KonradZuse
  • KonradZuse
I asked on the code ranch for you, so lets see what they think up....
KonradZuse
  • KonradZuse
or just creating a billion print statements lol.
hartnn
  • hartnn
lol, a printf inside a printf inside a printf .....and so on..infinite printfs
KonradZuse
  • KonradZuse
na more like int i = 0; printf("int i = 0);
KonradZuse
  • KonradZuse
etc.
KonradZuse
  • KonradZuse
but you could literally do printf("code code code code more code yay code"); If the code doesn't have to run... 1 GIANT print statement...
anonymous
  • anonymous
Dont know if this will work... but u can try file functions (read and write or standard output)... (assuming u are going to save the source code file first) just ask your code to read that file (source.c) and maybe buffer it in some array .. and then print it :P
anonymous
  • anonymous
errr... do u want this code to be platform independent?
KonradZuse
  • KonradZuse
I was also thinking that, but you would have to say what you want to write and read in :p.
rsmith6559
  • rsmith6559
How about taking argv[0], appending ".c", if the file exists, open it, getc/putc and close it?
across
  • across
This is like asking to build a screwdriver using a screwdriver. :P
KonradZuse
  • KonradZuse
I found this http://en.wikipedia.org/wiki/Quine_(computing)
hartnn
  • hartnn
quine is interesting, but not in c. rsmith, can u elaborate ?
anonymous
  • anonymous
Yeah, quine is an odd area of CS. This code will print everything but the include line. #include int main() { char *s="int main() { char *s=%c%s%c; printf(s,34,s,34); }"; printf(s,34,s,34); }
rsmith6559
  • rsmith6559
C is a compiled language. The executing file isn't the source file. Therefore, the program becomes basically, a simple open a file read it in, output it and close the file. A simple guess for the source code file name is the executable file's name with ".c" appended.
KonradZuse
  • KonradZuse
They have c++ you can change what's needed youself...e
DLS
  • DLS
Hint: You can save the code onto a textfile and thereafter display it directly via simple input functions :)
DLS
  • DLS
http://en.wikipedia.org/wiki/Quine_(computing)

Looking for something else?

Not the answer you are looking for? Search for more explanations.