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/

相关文章:

php - 为什么这个 PDO Insert 执行了两次?

c - 这段代码缺少什么,对吗?

python - 在Python中迭代列表并连接字母顺序

c - 关于 BYTE 类型的 strlen() 的警告

c - 'sizeof' 对不完整类型问题的无效应用

C 快速排序函数不断崩溃

java - 按钮闪光

c++ - C++中的字符串、字符比较

c++ - 使用别人的库,该库在名称中具有值

c - 如何从 c 中的字符串中删除 "\n"?