Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 8. C Characters and Strings > Section 8.2 Fundamentals of Strings and C...

Section 8.2 Fundamentals of Strings and Characters

  • Characters are the fundamental building blocks of source programs. Every program is composed of a sequence of characters that—when grouped together meaningfully—is interpreted by the computer as a series of instructions used to accomplish a task.

  • A program may contain character constants. A character constant is an int value represented as a character in single quotes.

  • The value of a character constant is the character’s integer value in the machine’s character set.

  • A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +, -, *, / and $. String literals, or string constants, in C are written in double quotation marks.

  • A string in C is an array of characters ending in the null character ('\0').

  • A string is accessed via a pointer to the first character in the string. The value of a string is the address of its first character. Thus, in C, it is appropriate to say that a string is a pointer—in fact, a pointer to the string’s first character. In this sense, strings are like arrays, because an array is also a pointer to its first element.

  • A character array or a variable of type char * can be initialized with a string in a definition.

  • When defining a character array to contain a string, the array must be large enough to store the string and its terminating null character.

  • A string can be stored in an array using scanf. Function scanf will read characters until a space, tab, newline or end-of-file indicator is encountered.

  • For a character array to be printed as a string, the array must contain a terminating null character.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint