Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Strings in c.

  1. Aug 23, 2009 #1
    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?
  2. jcsd
  3. Aug 23, 2009 #2


    User Avatar
    Science Advisor
    Homework Helper

    "Hello world.\nMy name is dE_logics.\nI love PF, it is the best forum.\n\nEver."
    is just as valid a string as
    "It's me!"
  4. Aug 23, 2009 #3
    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.
  5. Aug 23, 2009 #4
    Ok, thanks everyone!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook