C Strings: What is the Difference with Text Streams?

AI Thread Summary
In C programming, string constants like "hello\n" are stored as arrays of characters, ending with a null terminator '\0' to signify the end of the string. This allows for strings to include multiple lines, as demonstrated by examples like "Hello world.\nMy name is dE_logics.\nI love PF, it is the best forum.\n\nEver." A string can indeed contain line breaks represented by '\n', making it valid to have multi-line strings. C-strings are composed of characters, typically 8 bits each, allowing for a range of 0-255 in numerical values. Special characters can be encoded using backslashes, such as \0 for the null terminator, \n for line endings, and \r for carriage returns. For instance, declaring char *str = "my\0name" will result in only "my" being printed due to the null terminator stopping the output at that point.
dE_logics
Messages
742
Reaction score
0
when a string constant like

"hello\n"

appears in a C program, it is stores as an array of characters containing the characters in the string and terminated with a '\0' to mark the end.

This is what a book says.

Is this string different from a text stream which consists of many lines?...or can a string contain many lines separated by \n?
 
Technology news on Phys.org
Sure,
"Hello world.\nMy name is dE_logics.\nI love PF, it is the best forum.\n\nEver."
is just as valid a string as
"Hello\n"
or
"It's me!"
 
dE_logics said:
This is what a book says.

Is this string different from a text stream which consists of many lines?...or can a string contain many lines separated by \n?

c-strings are stored as arrays of characters, and a character is typically 8 bits which can store an integer from 0-255. The letters, numbers, and symbols all have a mapping to numerical values but that only uses up about 46 of the 256 available numbers.

By using a backslash you can conveniently encode some other special characters. For example,

\0 = null-terminator, maps to number 0
\n = line ending
\r = carriage return (goes back to overwrite the current line with following text)
\\ = puts a single slash in

So for example, if you make:

char *str = "my\0name";

then print out "str", it will just print out "my" because it assumes the first null-terminator is the end of the string.
 
Ok, thanks everyone!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top