How to output single character in C with write?

14,035

Solution 1

You have found your function, all you need now is to pass it proper parameters:

int handle = open("myfile.bin", O_WRONLY);
//... You need to check the result here
int count = write(handle, "b", 1); // Pass a single character
if (count == 1) {
    printf("Success!");
}

I did indeed want to use stdout. How do I write a version to display the whole alphabet?

You could use a pre-defined constant for stdout. It is called STDOUT_FILENO.

If you would like to write out the whole alphabet, you could do it like this:

for (char c = 'A' ; c <= 'Z' ; c++) {
    write(STDOUT_FILENO, &c, 1);
}

Demo.

Solution 2

Let's see the man page of write(), which says,

ssize_t write(int fd, const void *buf, size_t count);

Description

write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd.

As per your requirement, you need to pass an address of a buffer containing b to print to standard output.

Let's see some code along with, shall we?

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

int main(void) {
    char  b = 'b';
    write(STDOUT_FILENO, &b, 1);
    return 0;
}

Let me explain. Here, the STDOUT_FILENO is the file descriptior for standard output as defined in unistd.h, &b is the address of the buffer containing 'b' and the number of bytes is 1.

Share:
14,035

Related videos on Youtube

ButterMyBitter
Author by

ButterMyBitter

Teaching myself to program, one step at a time!

Updated on June 04, 2022

Comments

  • ButterMyBitter
    ButterMyBitter about 2 years

    Could someone instruct me on how to print a single letter, for example "b" in C while using only the write function (not printf).

    I'm pretty sure it uses

    #include <unistd.h>
    

    Could you also tell me how the write properties work? I don't really understand

    int  write(  int  handle,  void  *buffer,  int  nbyte  );
    

    Could some of you guys toss in a few C beginner tips as well?

    I am using UNIX.

    • Sergey Kalinichenko
      Sergey Kalinichenko almost 9 years
      There is no need to apologize for your question.
    • Weather Vane
      Weather Vane almost 9 years
      The buffer would be the address of the char myb ='b' and the nbyte size would be 1.
  • Sourav Ghosh
    Sourav Ghosh almost 9 years
    no doubt, but just because the mention of printf(), i thought OP wanted to print to stdout, not to file. In that case, it would have been fprintf(). Please correct me if i'm wrong.
  • Sergey Kalinichenko
    Sergey Kalinichenko almost 9 years
    @SouravGhosh Now that you have mentioned it, I am not sure if OP really wants to write to stdout or not.
  • ButterMyBitter
    ButterMyBitter almost 9 years
    I did indeed want to use stdout. The answer above worked for me while this one did not, however. Question - how would I do the write version to display the whole alphabet? I know char c and such would have to do with it. Also, what's "c++;" used for in this and most scenarios?
  • ButterMyBitter
    ButterMyBitter almost 9 years
    Cool! What is the c++ part used for? I can't find it anywhere. Also, any way to do it without for and maybe replaced by a while?
  • Sergey Kalinichenko
    Sergey Kalinichenko almost 9 years
    @ButterMyBitter This is an increment operation on the character. Rewriting a for loop as a while loop is a good exercise that is purely mechanical. It's good to try to do independently.
  • ButterMyBitter
    ButterMyBitter almost 9 years
    Thanks for the help. Much appreciated.
  • 0___________
    0___________ almost 3 years
    write(STDOUT_FILENO, (char[]){'b'}, 1);
  • Zibri
    Zibri almost 3 years
    @0___________ he wanted it in a variable :P