Self writing program and long string assignment in C

Click For Summary

Discussion Overview

The discussion revolves around creating a self-replicating C program, often referred to as a quine. Participants explore how to store and output a long string of text within the program without reading from a file, focusing on the challenges of handling multiline strings and the implications of character handling in C.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant proposes splitting a string into two parts, S1 and S2, to construct a self-replicating program.
  • Another participant questions how the text is entered into the program, suggesting it might be through human input.
  • A participant clarifies that the text should be typed directly into the source code, emphasizing the challenge of adding characters that affect the program's output.
  • One participant mentions the concept of "line continuation" in programming, suggesting that it can be achieved in C++ using a backslash followed by a newline.
  • Another participant references the concept of a quine, indicating a potential overlap with the original question.
  • A later reply acknowledges a misunderstanding of the original post and reflects on the challenges of using printf to handle special characters in strings.

Areas of Agreement / Disagreement

Participants express a mix of agreement and confusion regarding the nature of the problem, with some recognizing it as a quine while others focus on the technical challenges of string handling in C. The discussion remains unresolved regarding the best approach to implement the self-writing program.

Contextual Notes

Participants note limitations related to handling special characters in strings and the implications of multiline text storage. There is also a mention of the need for careful character management in the output process.

Lojzek
Messages
246
Reaction score
1
I would like to write a C program that writes itself to the screen (without reading from disk).
My idea is to assign text to a string S, split it into two parts S1 and S2 and then write each part twice. If S1 is made equal to program part before text definition and S2 equal to program part after, then the program would write S1S1S2S2, which would be equal to the whole program.

However I don't know how to put a text into the string S. I tried to use strcpy, but it does not work if the text is too long for a single line. Is there a way to store a several line long text into a string without spliting the text (the whole text should be written inside a single "").
 
Technology news on Phys.org
How is the text entered into the program? Is it by human input?
 
Yes, text should be typed directly into the source code. Of course it would be much easier if it would be read from a file, but then this task would not be interesting. The tricky part is that any character added to the text is simultaneously added to the whole program, so we must find a way to output more information that we explicitly define.
 
Yeah it's probably quine. Apparently I misread the first line of the OP.
 
Thanks for the answers (and sorry for not checking for similar posts on this forum). I read the example on wikipedia and now understand how this program can be constructed.
However my first idea about printing S1S1S2S2 would not work: I forgot that printf does not print all characters in a quotation (for example \ used for line continuation or before special characters).
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 21 ·
Replies
21
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
3K