C Strings: What is the Difference with Text Streams?

In summary, c-strings are stored as arrays of characters, with each character typically being 8 bits that can store an integer from 0-255. Special characters can be encoded using a backslash, such as \n for a line ending or \0 for a null-terminator. This allows for strings to contain multiple lines separated by \n, and a string can also be terminated with multiple null-terminators. These special characters are just mapped to specific numerical values and do not take up much space in the array.
  • #1
dE_logics
742
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
  • #2
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!"
 
  • #3
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.
 
  • #4
Ok, thanks everyone!
 

Related to C Strings: What is the Difference with Text Streams?

1. What is the difference between C strings and text streams?

C strings are a sequence of characters in the C programming language, whereas text streams are a way of representing data that can be read or written from a file or the console.

2. How are C strings and text streams used in programming?

C strings are commonly used for manipulating text data in C programs, while text streams are used for reading and writing data to and from files or the console.

3. Can C strings be converted to text streams and vice versa?

Yes, C strings can be converted to text streams using functions such as fopen() and fputs(), and text streams can be converted to C strings with functions like fgets() and sscanf().

4. What are the advantages and disadvantages of using C strings and text streams?

The advantage of using C strings is that they are simple and efficient for manipulating text data. However, they can be tedious to work with and may not be suitable for handling large or complex data. Text streams, on the other hand, provide a more versatile way of working with data, but they may have a higher memory and processing overhead.

5. Are there any alternative methods for handling text data besides C strings and text streams?

Yes, there are other methods for handling text data such as using string libraries, regular expressions, or other data structures like linked lists. Each method has its own advantages and disadvantages, and the choice depends on the specific needs of the program.

Similar threads

  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
4
Replies
118
Views
7K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
3
Replies
73
Views
5K
Back
Top