0
#include <stdio.h>

void print_alphabet(void)
{
    char a[] = "abcdefghijklmnopqrstuvwxyz";
    int i = 0;
    int n;
    for (n = 0; n <= 9; n++)
    {
        while (a[i] != '\0')
        {
            putchar(a[i]);
            i++;
        }
        putchar('\n');
    }
    return;
}

int main(void)
{
    print_alphabet();
    return 0;
}

I am trying to print the alphabet 10 times with each series on a different line but, when I compile and execute my code, I only get one line of the complete alphabet and 9 blank new line.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • 6
    You should indent your code properly. Curly braces all placed in the first column makes this unreadable. – Thomas Jager Jul 30 '22 at 02:55
  • 5
    Hint: what is the value of `i` at the start of the second iteration of the `for` loop? – dbush Jul 30 '22 at 02:57
  • @TomKarzes `i` doesn't get iterated past the position of the trailing '\0'. This is well defined behavior. – Allan Wind Jul 30 '22 at 03:12
  • @AllanWind Oops, right, it just gets stuck at the null character. – Tom Karzes Jul 30 '22 at 06:48
  • Have you tried running your code line-by-line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) If you run your code line-by-line while monitoring the value of `i`, then the error in your code should become obvious. – Andreas Wenzel Jul 30 '22 at 07:13
  • @ThomasJager sorry about the readability, i'm currently learning how to code. The programme i'm enrolled in, requires code to be written in that specific style. – Somtee Tunechi Onu Jul 30 '22 at 10:40
  • @SomteeTunechiOnu I would be skeptical about any place that teaches you to code like that. That's horrific style and would be unacceptable in any project you'd work on. You can't tell what's nested where, it's a jumble of equally indented statements, with random curly braces at the beginning (as if they're considered unimportant). – Thomas Jager Jul 30 '22 at 13:32

2 Answers2

3

You don't set i inside the for loop, so the condition of the while loop a[i] != '\0' remains false for n > 1. This means the body of the while loop doesn't get executed again. Use another for loop instead:

#include <stdio.h>

void print_alphabet(void) {
    const char a[] = "abcdefghijklmnopqrstuvwxyz";
    for (unsigned char n = 0; n < 10; n++) {
        for(unsigned char i = 0; a[i]; i++) {
            putchar(a[i]);
        }
        putchar('\n');
    }
    return;
}

int main(void) {
    print_alphabet();
    return 0;
}
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
0

The previous answer is correct but if you want to use your original approach with the while loop, you just need to add the statement i=0; inside the for loop, just before the while statement.

pdp8
  • 206
  • 1
  • 6