---
title: "Correct program: character arrays"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:correct-program-char-arrays"
source: https://www.bctengineer.com/q/semester_1/computer_programming/correct-program-char-arrays
site: BCT Engineering
---

# Correct program: character arrays

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2067 Asar, 8 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/correct-program-char-arrays
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Rewrite program correctly and write output of the given program written in C programming language below:
```
# include<stdio.h>
# include<conio.h>
# include<string.h>
void main( )
{
  char ar1[11] = {'I','o','E',' ','P','U','L','C','H','O','W','K'};
  char ar2[15] = "IoE, Pulchowk";
  char ar3[11] = {{'I'},{'O'},{'E'},
  {' '},{'P'},{'U'},{'L'},{'C'},{'H'},{'O'},{'W'},{'K'}};
  clrscr( );
  {printf ("\nArray 1 = %s\n", ar1);
  printf ("\nArray 2 = %s\n", ar2);
  printf ("\nArray 1 = %s\n", ar3);
  return 0
}
```

## Short answer

Errors: ar1 size 11 with 12 initializers, missing null terminator; similar issue for ar3; return 0 missing ';'; extra '{'. Correct by increasing ar1 and ar3 sizes to at least 13. Output: Array 1 = IoE PULCHOWK, Array 2 = IoE, Pulchowk, Array 3 = IOE PULCHOWK.

## Answer

#### Errors in the given program
1. `char ar1[11]` is given 12 initialisers (`IoE PULCHOWK` has 12 characters). Excess initialisers are an error, and even a size of 12 would leave no space for the null character `'\0'`, so `%s` could run past the array (undefined behaviour). The size must be at least 13.
2. `ar3` has the same size problem. Also, the braces around every single character (`{'I'}`) are unnecessary; they are allowed only with a warning ("braces around scalar initializer").
3. `return 0` has no semicolon, and `void main()` cannot return a value. Use `int main()` or remove the `return`.
4. The extra `{` before the first `printf` is never closed, so the closing `}` of `main` is missing.
5. `conio.h` and `clrscr()` are Turbo C only (not standard C). They may be kept in Turbo C, but are removed here.
6. The third `printf` prints the label "Array 1" for `ar3`; it should be "Array 3".

(`gcc -Wall -std=c99` on the original reports the excess elements, the braces warnings and the missing `;`.)

#### Corrected program

```c
#include <stdio.h>

int main(void)
{
    char ar1[13] = {'I','o','E',' ','P','U','L','C','H','O','W','K'};
    char ar2[15] = "IoE, Pulchowk";
    char ar3[13] = {'I','O','E',' ','P','U','L','C','H','O','W','K'};

    printf("\nArray 1 = %s\n", ar1);
    printf("\nArray 2 = %s\n", ar2);
    printf("\nArray 3 = %s\n", ar3);
    return 0;
}
```

#### Output

```text

Array 1 = IoE PULCHOWK

Array 2 = IoE, Pulchowk

Array 3 = IOE PULCHOWK
```

`ar2` is "IoE, Pulchowk" (13 characters plus `'\0'` in a 15-element array, so the last element is also `'\0'`). `ar1` keeps the small `o`, while `ar3` has capital `O` in "IOE".

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why is char ar1[11] with 12 characters wrong?
   A. It is fine because C ignores extra characters
   B. There are more initialisers than the array size, and there is no room for '\0'
   C. char arrays must be of even size
   D. Only the %s format is wrong
   Answer: B. Excess initialisers are an error; for %s the array also needs one extra byte for '\0', so size at least 13. (Key point: Array size problem)

2. What is wrong with void main() { ... return 0 }?
   A. Nothing, main must always be void
   B. return 0 must come before printf
   C. Only the semicolon is missing
   D. void main cannot return a value and the return statement lacks a semicolon
   Answer: D. The fix is int main() with 'return 0;'. (Key point: Return in main)

3. What other syntax error is in the program?
   A. A missing #include <stdlib.h>
   B. An extra { before the first printf is never closed
   C. Missing parentheses in clrscr
   D. A missing closing quote in ar2
   Answer: B. The extra opening brace leaves main without its closing brace. (Key point: Unclosed brace)

4. What do the corrected program's three lines print?
   A. IOE PULCHOWK, IoE, Pulchowk, IOE PULCHOWK
   B. IoE PULCHOWK, IoE Pulchowk, IoE PULCHOWK
   C. IoE, Pulchowk printed three times
   D. IoE PULCHOWK, IoE, Pulchowk, IOE PULCHOWK
   Answer: D. ar1 has a small o, ar2 holds "IoE, Pulchowk" and ar3 has a capital O. (Key point: Output of arrays)

## Related in Computer Programming

- [Correct program: do-while and pow](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-sum-root) — Computer Programming, chapter 3
- [Correct program: Celsius to Fahrenheit](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-celsius-fahrenheit) — Computer Programming, chapter 3
- [Rewrite string characters alphabetically](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-alphabetical-order) — Computer Programming, chapter 7
- [Output of arithmetic expression program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aoutput-expression-xyz) — Computer Programming, chapter 3
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3
- [Pattern: PULCHOWK using unformatted output](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apattern-pulchowk) — Computer Programming, chapter 4

## Related in other subjects

- [Rewrite sentences correctly (pronoun, concord) set 2082](https://www.bctengineer.com/question.md?key=semester_5%2Fcommunication_english%3Arewrite-sentences-pronoun-kartik) — Communication English, chapter 3
- [Rewrite sentences correctly set 2081](https://www.bctengineer.com/question.md?key=semester_5%2Fcommunication_english%3Arewrite-sentences-pronoun-chaitra) — Communication English, chapter 3
- [Redundant network for IOE Pulchowk campus](https://www.bctengineer.com/question.md?key=semester_8%2Finternet_and_intranet%3Aredundant-network-ioe-pulchowk) — Internet and Intranet, chapter 5
- [Pyramid pattern output program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apyramid-program) — Object Oriented Programming, chapter 8
