Introduction to C / C++ Programming
Character Strings

Reference

The material in this page of notes is organized after "C Programming, A Modern Approach" by K.N. King, Chapter 13. Some content may also be from other sources.

Overview

String Literals

Passing String Literals to Functions

the string literal "Please enter . . . " will be stored somewhere in memory, and the address will be passed to printf. The first argument to printf is actually defined as a char *.

Continuing a String Literal

If a string literal needs to be continued across multiple lines, there are three options:

  1. If the new line character is included between the double quotes, then it is included as part of the string:
  2.     printf( "This will
             print over three
             lines, ( and will include extra tabs or spaces. )" );
  3. Escaping the newline character with the backslash will cause it to be ignored:
  4.     printf( "This will \
             print over a single \
             line, ( but will still include extra tabs or spaces. )" );
  5. However the best approach is to note that when two strings appear adjacent to one another, C will automatically concatenate them into a single string:
  6.     printf( "This will "
             "print over a single "
             "line, ( without extra tabs or spaces! )" );

Operations on String Literals

String Variables

String variables are typically stored as arrays of chars, terminated by a null byte.

Initializing String Variables

String variables can be initialized either with individual characters or more commonly and easily with string literals:

char s1[ ] = { 'h', 'e', 'l', 'l', 'o', '\0' };

char s2[ ] = "hello"; // Array of size six

char s3[ 20 ] = "hello"; // Fifteen null bytes

char s4[ 5 ] = "hello"; // Five chars, sacrifices the null byte

char s5[ 3 ] = "hello"; // Illegal

Character Arrays versus Character Pointers

Character arrays and character pointers are often interchangeable, but there can be some very important differences. Consider the following two variables:

char s6[ ] = "hello", *s7 = "hello";

Reading and Writing Strings

Writing Strings Using printf and puts

Reading Strings Using scanf, gets, and fgets

Reading Strings Character by Character

Accessing the Characters in a String

Characters in a string can be acessed using either the [ ] notation or a char pointer.

Using the C String Library

size_t strlen ( const char * str );

char * strcpy ( char * destination, const char * source );

char * strncpy ( char * destination, const char * source, size_t num );

char * strcat ( char * destination, const char * source );

int strcmp ( const char * str1, const char * str2 );

int atoi (const char * str);
double atof (const char* str);
long int atol ( const char * str );
long long int atoll ( const char * str );

sprintf( char [ ], format [ , data . . . ] );

sscanf( char *, format [ , address . . . ] );

See also strchr, strrchr, strstr, and strtok for searching and tokenizing strings.

String Idioms

Searching for the End of a String

Copying a String

		void strcpy( const char * s, char * d ) {

			do {
				*d++ = *s++;
			} while( *s );

			return;
		}

Arrays of Strings

Command Line Arguments