Self writing program and long string assignment in C

In summary: Instead, printf prints the first occurrence of the first character in the string, followed by the second occurrence of the first character, and so on. This means that S1S1S2S2 would print as "program part before text definition", not "the whole program".In summary, the text must be entered directly into the source code, and special characters must be escaped with a backslash \.
  • #1
Lojzek
249
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
  • #2
How is the text entered into the program? Is it by human input?
 
  • #3
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.
 
  • #4
  • #6
Yeah it's probably quine. Apparently I misread the first line of the OP.
 
  • #7
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).
 

1. What is a self writing program?

A self writing program is a computer program that is able to modify its own code and generate new code without human intervention. This allows the program to adapt and improve its own functionality without the need for manual updates.

2. How does a self writing program work?

A self writing program works by utilizing code generation techniques, such as using templates or algorithms, to create new code based on specific criteria or inputs. The program then incorporates this new code into its existing codebase, effectively modifying itself.

3. What are the benefits of using a self writing program?

Some potential benefits of using a self writing program include increased efficiency and flexibility, as the program is able to adapt and improve itself without the need for human intervention. This can also lead to faster development and easier maintenance of the program.

4. What is a long string assignment in C?

A long string assignment in C is a method of assigning a string of characters to a variable in the C programming language. This is often used when the string contains a large amount of text or special characters, as it allows for more efficient and readable code compared to manually typing out each character.

5. How do you perform a long string assignment in C?

In C, a long string assignment can be performed by using the double quotes ("") to enclose the string of characters and assigning it to a variable using the equal sign (=). For example: char *str = "This is a long string assignment in C";

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top