I am having problems with the code in C; I cannot display numbers using the write() function; here is the code

  #include char fy(char c) { write(1, & c, 1); } int main(void) { // your code goes here int z = 0; int n = 9; while (z <= n) { fy(z); z++; } } 

comes out: a few obscure characters through printf() impossible. It is necessary only through write() what is the problem?

and how can i fix it?

    2 answers 2

    That's right, you bring the usual integer values ​​to characters and get characters with a code from 0 to 9, in order to turn the number into a character correctly, add the character code '0' to the number.

     char fy(char c) { с+='0'; write(1, &c, 1); } 
    • aah ASCII ... spasibo - Dumitru Strelet
    • @Dumitru Strelet Thank you for not gurgling and you can't put it in your pocket. If the answer you are satisfied, then tick it under the tsiferka with arrows. This will be the same thank you. - Max ZS

    In fact, in C, I do not recall such a function. There is fwrite .

    The question is what, in what form and where you want to withdraw.

    If a single character in the console -

     fwrite(&c,sizeof(c),1,stdout); 

    But if you want to output numbers from 0 to 9 as characters , then you need to convert them -

     char с = z + '0'; 

    Obviously, this only works for numbers from 0 to 9.

    If this is not what you need - clarify your question.

    • 2
      There is such a function, from the ancients, writes in file handle. - VladD
    • @VladD At least, neither in the C in a Nutshell directory, nor in en.cppreference.com/w is described ... - Harry
    • Here it is: linux.die.net/man/2/write (maybe this is a POSIX standard?) - VladD