MCS-201 · Unit 4

Arrays and Strings

Section 6 of 9

Block 1 · Unit 4Arrays and Strings11 min · Section 6 of 9

4.6Strings: Declaration, Initialisation and Input/Output

Covers IGNOU MCS-201 §4.7, 4.8

Names, addresses and messages are text. C has no separate string type: a string is a character array that ends with a special marker. Understanding that marker, the null character, explains every rule and every bug involving strings in C.

What a string is

The null character '\0' marks the end of a string. It is different from the digit character '0' (code 48). A string of n visible characters therefore needs n + 1 bytes.

char name[8] =
index:    0    1    2    3    4    5    6    7
value:   'P'  'R'  'O'  'G'  'R'  'A'  'M'  '\0'

'A' is one character (1 byte of data); "A" is a string of two bytes, 'A' and '\0'.

Declaring and initialising strings

DeclarationBytesValid string?
char str[] = "UNIX";5Yes: size counted automatically, '\0' included
char str[5] = {'u','n','i','x','\0'};5Yes
char str[9] = "unix";9Yes: the unused 4 bytes are also '\0'
char str[4] = "unix";4No: exactly fills the array, no '\0'
char str[4] = {'u','n','i','x'};4No: no '\0'
char str[3] = "unix";—Invalid: too many characters

A string cannot be assigned with = after its declaration (s = "hello"; is an error for an array s); use strcpy from <string.h>.

Printing and reading strings

printf("%s", name) prints characters up to, but not including, the '\0'. puts(name) prints the string followed by a newline.

For input there are two standard tools:

FunctionReadsNotes
scanf("%9s", name)One word: stops at the first space, tab or newlineNo & (the array name is already an address). Always give a maximum width: array size − 1
fgets(line, sizeof line, stdin)A whole line, including spacesNever overflows; keeps the newline '\n' if it fits

Key takeaways

  • A string is a character array terminated by '\0' (value 0, not the digit '0').
  • n characters need n + 1 bytes; a string constant initialiser adds the '\0'.
  • %s in scanf reads one word; fgets reads a line and limits the length.
  • printf("%s") and puts print up to the '\0'.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the minimum array size needed to store the string “INDIA”?

Show answer

Answer: B. 6

Five letters plus the terminating '\0' need 6 bytes. Size 5 would leave no room for the terminator.

Question 2 · multiple choice

Which character terminates every C string?

Show answer

Answer: C. '\0'

The null character '\0' has value 0. '0' is the digit zero with code 48, and '\n' is a newline.

Question 3 · multiple choice

The user types Navninder Singh for scanf("%19s", name);. What is stored in name?

Show answer

Answer: D. Navninder

%s stops at the first whitespace, so only the first word is stored. Reading the full line needs fgets.

Question 4 · multiple choice

What does this program print?

C · str_index.c
#include <stdio.h>

int main(void)
{
    char name[10] = "IGNOU";
    printf("%c %c %s\n", name[0], name[4], name);
    return 0;
}
Show answer

Answer: A. I U IGNOU

name[0] is ‘I’ and name[4] is the fifth letter, ‘U’; %s prints the whole string. Index 3 would be ‘O’.

Question 5 · multiple choice

Why should gets not be used?

Show answer

Answer: C. It cannot limit the input length, so it can overflow the array

gets has no size argument, so long input overwrites memory beyond the array; it was removed from the language in C11. fgets takes the size.

Question 6 · short answer

Explain the difference between char a[] = "CAT"; and char b[] = {'C', 'A', 'T'};, and what happens if each is printed with %s.

Show worked answer

a has 4 elements: ‘C’, ‘A’, ‘T’ and the null character '\0' added by the string constant, so it is a valid string and printf("%s", a) prints CAT. b has exactly 3 elements and no '\0', so it is a character array but not a string; printf("%s", b) keeps reading memory past the array until it happens to find a 0 byte, which is undefined behaviour and may print extra garbage characters.

↑ ↓ move↵ openTab switch type