MCS-201 · Unit 4

Arrays and Strings

Section 7 of 9

Block 1 · Unit 4Arrays and Strings9 min · Section 7 of 9

4.7Formatting Strings and Arrays of Strings

Covers IGNOU MCS-201 §4.9, 4.10

Reports and tables need text in neat columns, and programs often handle many strings at once, such as a list of names. This section covers the printf options for strings and the two-dimensional character array used to hold several strings.

Width and precision with %s

Syntax
%[-][width][.precision]s
PartMeaning
widthMinimum number of character positions used; shorter text is padded with spaces
-Left-justify within the width (the default is right-justified)
.precisionMaximum number of characters printed from the string
*Take the width or precision from an int argument

With char s[] = "PROGRAMMING"; (square brackets added to show the field edges):

StatementOutput
printf("[%s]", s);[PROGRAMMING]
printf("[%15s]", s);[ PROGRAMMING]
printf("[%-15s]", s);[PROGRAMMING ]
printf("[%.5s]", s);[PROGR]
printf("[%15.5s]", s);[ PROGR]
printf("[%-15.5s]", s);[PROGR ]

Arrays of strings

An array of strings is a two-dimensional char array: the first size is the number of strings, the second is the maximum length of each string including its '\0'.

char names[3][10] = {
            0    1    2    3    4    5    6    7    8    9
names[0]   'm'  'a'  'r'  't'  'i'  'n'  '\0'
names[1]   'p'  'h'  'i'  'l'  '\0'
names[2]   'c'  'o'  'l'  'l'  'i'  'n'  's'  '\0'

names[i] (one subscript) is the whole i-th string and can be used with %s; names[i][j] (two subscripts) is a single character.

Key takeaways

  • Width sets the minimum field size; precision sets the maximum characters printed; - left-justifies.
  • * takes the width or precision from an argument.
  • An array of strings is a 2D char array: rows are strings, columns are characters.
  • names[i] is a string; names[i][j] is a character.

Practice questions

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

Question 1 · multiple choice

What does printf("[%.3s]", "COMPUTER"); print?

Show answer

Answer: B. [COM]

A precision of 3 prints at most the first three characters. It takes them from the start, not the end.

Question 2 · multiple choice

What does printf("[%6s]", "AB"); print?

Show answer

Answer: C. [ AB]

The width 6 pads on the left by default, so the text is right-justified. Left justification needs %-6s.

Question 3 · multiple choice

For char names[5][10];, what is the longest name (in visible characters) each row can safely hold?

Show answer

Answer: D. 9

Each row has 10 bytes, and one is needed for '\0', leaving 9 visible characters.

Question 4 · multiple choice

Given char names[3][10] = {"martin", "phil", "collins"};, what is names[2][0]?

Show answer

Answer: A. ‘c’

Row 2 is “collins” and column 0 is its first character, ‘c’. names[2] alone would be the whole string.

Question 5 · multiple choice

What does this program print?

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

int main(void)
{
    char s[] = "hello";
    printf("[%-8.2s]\n", s);
    return 0;
}
Show answer

Answer: C. [he ]

Precision 2 keeps “he”, and the width 8 with - pads six spaces on the right.

Question 6 · short answer

Write the statement that prints only the first 4 characters of char city[] = "Chandigarh"; right-justified in a field of 10, and show the output using · for spaces.

Show worked answer

printf("%10.4s", city);. The precision .4 selects “Chan”, and the width 10 pads on the left with 10 − 4 = 6 spaces, so the output is ······Chan.

↑ ↓ move↵ openTab switch type