C Strings: What is the Difference with Text Streams?

Click For Summary

Discussion Overview

The discussion revolves around the differences between C strings and text streams, particularly focusing on how strings can contain multiple lines and the implications of special characters in C strings.

Discussion Character

  • Exploratory
  • Technical explanation

Main Points Raised

  • One participant notes that a string constant in C is stored as an array of characters, terminated by a null character '\0'.
  • Another participant asserts that strings can contain multiple lines, providing an example of a multi-line string.
  • A further contribution explains the storage of characters in C strings, detailing the use of special characters like '\0', '\n', and '\r' and their implications for string termination and formatting.

Areas of Agreement / Disagreement

Participants do not reach a consensus on whether a string is fundamentally different from a text stream, as the discussion includes varying interpretations of how strings can represent multiple lines.

Contextual Notes

There are unresolved aspects regarding the definitions of strings and text streams, as well as the implications of special character encoding in C strings.

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!
 

Similar threads

  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 40 ·
2
Replies
40
Views
4K
Replies
89
Views
7K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 33 ·
2
Replies
33
Views
6K
  • · Replies 20 ·
Replies
20
Views
2K