Valeria
Valeria

Reputation: 21

How to print the alphabet with the 'write' function?

I have to print the alphabet using the write() function. I can not use printf() to see the output.

I have this so far:

#include <stdio.h>
#include <unistd.h>

char ft_print_alphabet(char);

int main()
{
    char ch;

    for(ch = 'a'; ch <= 'z'; ch++) {
        write(1, %ch, 26);
    }

    return(0);
}

I have tried to do this but I do not understand the write() function at all.

Upvotes: 1

Views: 5366

Answers (1)

Thibaud
Thibaud

Reputation: 1095

Write take the output id here you want 1 for default output on terminal it take a pointer of char, here you have only a char so pass the adress and write will get a pointer of size one char and last it take the number of char you want to print, here you display them one by one write do not by default print carriage return

int main()
{
    char ch;

    for(ch = 'a'; ch <= 'z'; ch++) {
        write(1, &ch, 1);
    }

    return(0);
}

Upvotes: 2

Related Questions