在这段代码中,为什么 Write() 不适用于 Int?
In this code Why Write() doesn't work with 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;
}
当您使用 printf
时,我猜您使用了:
printf("%d %d %d",a,b,c);
所以你明确地告诉函数将变量解释为数字,并打印它们。当您使用 write 时,它假定您使用的是 char
。这意味着这将与:
printf("%c %c %c",a,b,c);
试试看 - 您会发现仍然是空白。那是因为您没有将变量解释为字符,因此将数字 1..9 转换为它们的 ASCII 字母值。这些不是普通字符,将显示为空白。
如果您在 main
中使用 char
而不是 int
,这将是相同的。将普通整数转换为打印所述整数的 ASCII 值的最佳选择是通过 Kiran 的回答,
myInt += '0'; //Only works for numbers than 0..9. You may has well have used char to save space.
因为数字的所有 ASCII 字符都是连续的。
我不明白为什么这段代码不起作用。它与 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;
}
当您使用 printf
时,我猜您使用了:
printf("%d %d %d",a,b,c);
所以你明确地告诉函数将变量解释为数字,并打印它们。当您使用 write 时,它假定您使用的是 char
。这意味着这将与:
printf("%c %c %c",a,b,c);
试试看 - 您会发现仍然是空白。那是因为您没有将变量解释为字符,因此将数字 1..9 转换为它们的 ASCII 字母值。这些不是普通字符,将显示为空白。
如果您在 main
中使用 char
而不是 int
,这将是相同的。将普通整数转换为打印所述整数的 ASCII 值的最佳选择是通过 Kiran 的回答,
myInt += '0'; //Only works for numbers than 0..9. You may has well have used char to save space.
因为数字的所有 ASCII 字符都是连续的。