Text in a Variable

So far, your variables have held numbers and single characters. But programs work with text all the time: names, messages, passwords. In programming, a piece of text is called a string, because it is a string of characters, one after another, like beads on a thread.

C has no special data type for text. A string is simply an array of characters, an array of type char. You create one like this:

char name[] = "Anna";

The text goes in double quotes. The square brackets stay empty, so the compiler counts the size of the array for you, just as with an array of numbers. How many elements does the array have? The text has 4 letters, but the array has 5 elements:

index:   0     1     2     3     4
char:   'A'   'n'   'n'   'a'   '\0'

The last element, '\0', is the null character: a backslash and a zero in single quotes, because it is one character. You won't see it in the output; its value is the number 0. It marks the place where the text ends, and the compiler adds it to the end of every text in double quotes by itself. Why is it needed? Nothing else tells printf and the other functions where the text ends (and later in this lesson, you'll see texts that are shorter than their array). So they go through a string character by character until they reach the '\0'.

To print a string, use the format specifier %s (s for string). After the format string, write the name of the array, without square brackets:

char name[] = "Anna";
printf("Hello, %s!\n", name);

This prints:

Hello, Anna!

printf prints the characters of name one by one and stops at the '\0'. Thanks to this end mark, a string can be printed all at once, while an array of numbers can't.

Watch the quotes. Single quotes hold one character of type char: 'A'. Double quotes hold a string: "A" is an array of two characters, 'A' and '\0'. If you mix them up, the program doesn't compile:

char letter = "A";
main.c:4:10: error: incompatible pointer to integer conversion initializing 'char' with an expression of type 'char[2]' [-Wint-conversion]

The message talks about pointers, which you'll meet later in the course. The important part is char[2]: where one char belongs, you gave an array of two characters. When you see this message, check your quotes.

Another common mistake is printf(name);. It looks simpler, but the program doesn't compile:

char name[] = "Anna";
printf(name);
main.c:5:12: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]

printf always takes the first thing in its parentheses as the format string, the instructions for what to print and how. If the name contained a %, printf would treat it as a format specifier. So always print a string through %s: printf("%s\n", name);.

Instructions

Below the welcome line, create a string player with the text "Luke", and print it with %s so that the output has the line Player: Luke.

How many elements does the array player have? Below that, store sizeof(player) in an int variable size and print it: Size: 5. sizeof gives the size in bytes, and one char takes exactly one byte, so for an array of characters, it's the number of elements. Why 5, when the name has only 4 letters?

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/10