我不明白为什么这段代码不起作用。它与 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 of789,
it should be789
im usingc= ','; 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/