c - 在这段代码中,为什么 Write() 不适用于 Int?

标签 c while-loop char int

我不明白为什么这段代码不起作用。它与 printf 一起工作正常,但我无法让它与 write 一起工作......

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

int ft_putchar(char a,char b,char c){
    write(1,&a,1);
    write(1,&b,1);
    write(1,&c,1);
    return(0);
}

int main()
{
 int x = 0;
 int y, z;

 while(x <= 9){
     y = x + 1;
     while(y <= 9){
         z = y + 1;
         while(z <= 9){
             ft_putchar(x,y,z);
             z++;
         }
         y++;
     }
     x++;
 }
    return 0;
}

没有错误输出

最佳答案

在写入之前,您需要将 ASCII 转换为其等效的 digit

example 5 = '5' +'0'

到目前为止,您正在将 ASCII 值写入终端。

int ft_putchar(char a,char b,char c){

    a += '0';
    b += '0';
    c += '0';

    write(1,&a,1);
    write(1,&b,1);
    write(1,&c,1);
    return(0);
}

i want to print them like this but in the end it should have nothing 578, 579, 589, 678, 679, 689, 789, instead of 789, it should be 789 im using c= ','; write(1,&c,1); c= ' '; write(1,&c,1);

您需要将分隔符传递给ft_putchar 函数,

int ft_putchar(char a,char b,char c, char del){

    a += '0';
    b += '0';
    c += '0';
    write(1,&a,1);
    write(1,&b,1);
    write(1,&c,1);
    write(1,&del,1);
    return(0);
}

int main()
{
 int x = 0;
 int y, z;

 while(x <= 9){
     y = x + 1;
     while(y <= 9){
         z = y + 1;
         while(z <= 9){

            if (x == 7 && y == 8 && z == 9)
             ft_putchar(x,y,z, ' ');
            else
             ft_putchar(x,y,z, ',');
             z++;
         }
         y++;
     }
     x++;
 }
    return 0;
}

关于c - 在这段代码中,为什么 Write() 不适用于 Int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56982891/

相关文章:

arrays - 返回前 n 个奇数的方法

Python 猜谜游戏 - while 循环只允许整数

ios - 检测输入到 UITextfield 的亚洲语言的 unicode 字符

c++ - 字符串在转换为 char* C++ 时被截断

java - 有什么办法可以让每张打印品都有不同的图案

c - 流上有哪些可能的错误条件会导致ferror()被设置?

c - 如何使用箭头键和 Enter 按钮选择数字

c - 释放内存时出现段错误

java - 在 while 循环中使用日历?

c++ - 如何在C中有效地存储字符串映射?