---
title: "Display characters between given ranges"
subject: "Computer Programming"
chapter: 5
key: "semester_1/computer_programming:characters-between-ranges"
source: https://www.bctengineer.com/q/semester_1/computer_programming/characters-between-ranges
site: BCT Engineering
---

# Display characters between given ranges

Computer Programming · Chapter 5: Control statements

- Asked in: 2073 Shrawan, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/characters-between-ranges
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-5

## Question

Write a C program to display all characters between a given ranges.

## Short answer

Read two characters, swap if out of order, then loop from start to end printing each character separated by spaces.

## Answer

Characters are stored as ASCII codes, so a `char` variable can be used in a `for` loop like an integer. The loop starts at the first character and goes up to the last one, printing each. If the user gives the range in reverse order, the two are swapped.

```c
#include <stdio.h>
int main(void)
{
    char start, end, ch;
    printf("Enter starting and ending characters: ");
    scanf(" %c %c", &start, &end);
    if (start > end)
    {
        ch = start; start = end; end = ch;   /* swap if given in reverse */
    }
    for (ch = start; ch <= end; ch++)
        printf("%c ", ch);
    printf("\n");
    return 0;
}
```
Output for the range `D` to `L`:
```text
Enter starting and ending characters: D E F G H I J K L
```

The same program with `a` and `e` prints `a b c d e`, and with `0` and `9` prints the digits.

## Check yourself

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

1. Why can a char drive a for loop?
   A. Always letters
   B. Stored as ASCII codes
   C. Converted by printf
   D. Unsigned
   Answer: B. Chars are small integers. (Key point: Char as int)

2. Why swap?
   A. Uppercase
   B. Sort
   C. Range given in reverse
   D. Memory
   Answer: C. start > end would run no loop. (Key point: Swap)

3.  Why " %c" with a leading space?
   A. Adds space
   B. Reads two
   C. Type
   D. Skips whitespace/newline
   Answer: D. Space skips whitespace. (Key point: Space in scanf)

4. D to L prints?
   A. D E F G H I J K L
   B. D L
   C. E to K
   D. L to D
   Answer: A. Increment each char. (Key point: Output)

## Related in Computer Programming

- [Rewrite string characters alphabetically](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-alphabetical-order) — Computer Programming, chapter 7
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Reverse array and display via pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-reverse-array) — Computer Programming, chapter 9
- [Display records sorted by name from file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-sorted-records-by-name) — Computer Programming, chapter 10
- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11
- [Reverse a string using functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-reverse-with-count-function) — Computer Programming, chapter 7, asked 2×

## Related in other subjects

- [Display N characters using default arguments](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3An-chars-default-args-program) — Object Oriented Programming, chapter 3
- [8086 ALP: display only alphabetic characters](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-alphabetic-only) — Microprocessor, chapter 3
- [Display 0 to 9 on seven segment](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-seven-segment-0-9) — Embedded System, chapter 9, asked 2×
- [Write and display 10 employees using file](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aemployee-file-program) — Object Oriented Programming, chapter 8
