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!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top