2

I'm just starting with C and I want to make a program that displays the alphabet in lowercase, on a single line, by ascending order, starting from the letter ’a’. And it should be protyped this way void ft_print_alphabet(void); I'm trying this code but it's not working.

void    ft_putchar(char c);

void    ft_print_alphabet(void)
{
    char    letter;

    letter = 'a';
    while (letter <= 'z')
    {
        ft_putchar(letter);
        letter++;
    }
}

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

I compile it using gcc ( Since it's what we must use ) like the following: gcc -o ftpp ftpp.c But I keep getting this error

Undefined symbols for architecture x86_64:
  "_ft_putchar", referenced from:
      _ft_print_alphabet in ft_print_alphabet-3d7c19.o
ld: symbol(s) not found for architecture x86_64
Hi There
  • 167
  • 1
  • 5
  • 12
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Mahesh Jul 19 '18 at 15:14
  • 1
    Can we see how `ft_putchar()` is defined? Why don't you just use `printf()` ? – SystemGlitch Jul 19 '18 at 15:14
  • @SystemGlitch we're not allowed to use printf, only write, I'm really a newbie so would mind teaching me how to define it exactly? – Hi There Jul 19 '18 at 15:16
  • @Mahesh Ah, I didn't see that, but I still would like a more direct answer so I will keep this question up if there are no problems with it. – Hi There Jul 19 '18 at 15:17
  • @HiThere `write(1, &letter, 1)` should work. One being the file descriptor for `stdout` by default. – SystemGlitch Jul 19 '18 at 15:18
  • 1
    @SystemGlitch It's better to use [`STDOUT_FILENO`](https://stackoverflow.com/a/12902707/7851115) because it gives meaning to the file descriptor. –  Jul 19 '18 at 15:33
  • 1
    Or you can simply use write(1, "abcdefghijklmnopqrstuvwxyz", 26); – Artory Jul 19 '18 at 15:53

3 Answers3

6

This is probably what you want, just implement your ft_putchar with write

#include <unistd.h>

void ft_putchar(char c) { write(STDOUT_FILENO, &c, 1); }

void ft_print_alphabet(void) {
  char letter;

  letter = 'a';
  while (letter <= 'z') {
    ft_putchar(letter);
    letter++;
  }
}

int main(void) {
  ft_print_alphabet();
  return 0;
}
Valerio Santinelli
  • 1,592
  • 2
  • 27
  • 45
2

The standard function for printing a character is putchar. You need to do #include <stdio.h> to use it.

#include <stdio.h>

void    ft_print_alphabet(void)
{
    char    letter;

    letter = 'a';
    while (letter <= 'z')
    {
        putchar(letter);
        letter++;
    }
}

int main(void)
{
    ft_print_alphabet();
    return 0;
}
anatolyg
  • 26,506
  • 9
  • 60
  • 134
0

This is just for funsies, and you shouldn’t worry too much about it. That code isn’t fully portable.

The system compiler on IBM mainframes still, for the sake of backwards compatibility, defaults to EBCDIC (code page 1047) rather than ASCII. Your program on that compiler would produce

abcdefghi«»ðýþ±°jklmnopqrªºæ¸Æ¤µ~stuvwxyz

Any C compiler that conforms to the standard has to encode the digits '0' through '9' with consecutive values. Because of that one exception, the letters 'a' through 'z' or 'A' through 'Z' do not need to be consecutive in the execution character set.

A more realistic situation you’ll need to worry about is internationalization. The correct “alphabetical order” (or collation order) for the current locale is not always the same order as the current character encoding! In fact, this is more often not the case.

Davislor
  • 14,674
  • 2
  • 34
  • 49