C Strings: What is the Difference with Text Streams?

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
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?
 
Physics news on Phys.org
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!