MAD
MAD

Reputation: 31

Why is this code not printing the lower-case alphabet as expected?

The problem is asking to fill an array with lowercase letters and then print them. What I'm getting as output are uppercase starting with F and going 5 places past Z. Why?

#include <stdio.h>
#include <string.h>

int main(int argc, const char **argv)
{   
    char alphabet[27];
    char i;

    for(i = 'a'; i <= 'z'; i++)
        printf("%c", &alphabet[i]);

    return 0;
}

Upvotes: 0

Views: 228

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 754530

I think this code implements what is asked for — fill an array with the letters of the alphabet and then print it.

#include <stdio.h>

int main(void)
{   
    char alphabet[27];
    int i;

    for (i = 'a'; i <= 'z'; i++)
        alphabet[i - 'a'] = i;
    alphabet[i - 'a'] = '\0';
    printf("alphabet: %s\n", alphabet);

    return 0;
}

(Incidentally, I check these things before I post them, and in this case, I'm very glad I did. I originally had alphabet[i] = '\0'; after the loop, but this is reported as error: array subscript is above array bounds [-Werror=array-bounds] — and that's right! Even simple code that "can't go wrong" can go wrong. My GCC 7.1.0 command line — which works with many other versions of GCC, was: gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition x3.c -o x3. I recommend using most of those options — at minimum, use -Wall -Wextra -Werror.)

This is similar to the code in the question, but probably isn't the way I'd actually write the code, in part because of the gotcha that nearly got me.

Another way of doing it would be:

#include <stdio.h>

int main(void)
{   
    char alphabet[26];
    int i;

    for (i = 0; i < 26; i++)
        alphabet[i] = i + 'a';
    printf("alphabet: %.*s\n", 26, alphabet);

    return 0;
}

Note that alphabet is not a string; it is not null-terminated. The %.*s conversion specification in conjunction with the 26 argument ensures that at most 26 characters are printed; it doesn't matter that alphabet is not a string. Of course, using a slightly larger array and alphabet[i] = '\0'; after the loop — analogous to the other code — would also work cleanly without the extra complexity in the printf() format string.

Left to my own devices, I'd probably use the null-terminating version of the second example program.

Read the POSIX specification of printf() for the details. It does have some (clearly marked) extensions over standard C printf(), but it serves for both POSIX and standard C. Then re-read it. And re-re-read it. And do that daily for a week, and then weekly for a month, and then monthly for a year, and then yearly ever after. It will repay the effort. And similarly for scanf(), which is even harder to use right than printf().

Upvotes: 0

Junbang Huang
Junbang Huang

Reputation: 1977

There are two problems with your codes.

  1. You never initialize char alphabet[27]. You will never get what you want it you are initializing it.

  2. Assume you initialize it, you can do the following

    for(i = 'a'; i <= 'z'; i++) printf("%c", &alphabet[i - 'a']);

  3. If you just want to print the alphabet, you can just simply do the following:

    for(i = 'a'; i <= 'z'; i++) printf("%c", i);

Upvotes: 1

AppWriter
AppWriter

Reputation: 245

The value of 'a' is 97. What you want to do is set i to 0 initially. Look here: http://www.asciitable.com/.

Also, you want to add values to your array before using it in the loop. Something like this:

int index = 0;
while(index < 26) {
    alphabet[index] = index + 'a'; //or alphabet[index] = index + 97
    ++index;
}

Upvotes: 0

Related Questions