C 选项卡的可变长度(\t)
我是一个初学者,在编码和试图完成我的家庭作业 虽然我已经解决了我的问题,但在这样做时,我遇到了\t的意外行为。其中,我得到的单个\t选项卡长度不同。以下是我的代码:C 选项卡的可变长度(\t),c,printf,C,Printf,我是一个初学者,在编码和试图完成我的家庭作业 虽然我已经解决了我的问题,但在这样做时,我遇到了\t的意外行为。其中,我得到的单个\t选项卡长度不同。以下是我的代码: #include<stdio.h> int calculate_intpart_qutent(int dividend, int diviser); int main() { int a; int b; int choice; do { printf("
#include<stdio.h>
int calculate_intpart_qutent(int dividend, int diviser);
int main()
{
int a;
int b;
int choice;
do
{
printf("Enter the Dividend (a) :\t");
scanf("%d", &a);
printf("\nEnter the Divisor (b) :\t");
scanf("%d", &b);
printf("\n\nThe quotient is:\t%d", calculate_qutent(a, b));
printf("\n\nDo you want to try again?(Y\\N):\t");
choice = getchar();
if (choice == '\n') choice = getchar();
printf("\n\n\n");
} while (choice=='y'|| choice=='Y');
}
int calculate_intpart_qutent(int dividend, int diviser)
{
return (dividend/diviser);
}
#包括
整数计算整数部分(整数被除数,整数除数);
int main()
{
INTA;
int b;
智力选择;
做
{
printf(“输入股息(a):\t”);
scanf(“%d”和“&a”);
printf(“\n输入除数(b):\t”);
scanf(“%d”和“b”);
printf(“\n\n商为:\t%d”,计算量(a,b));
printf(“\n\n是否重试?(Y\\n):\t”);
choice=getchar();
如果(choice=='\n')choice=getchar();
printf(“\n\n\n”);
}while(choice='y'| choice='y');
}
整数计算整数部分整数(整数除数,整数除数)
{
回报(股息/除数);
}
以下是我的输出:
既然我在第一个printf语句中都使用了单制表符,为什么我在输出屏幕上得到不同的制表符长度?我做错什么了
请考虑我是C中的一名初学学生,然后再投票否决这个问题。任何帮助都将不胜感激
我正在使用Visual Studio 2017 RC
既然我在第一个printf语句中都使用了单制表符,为什么我在输出屏幕上得到不同的制表符长度
使用tab键可以指定打印的空间数
如果您希望在printf
中使用固定长度,请尝试类似%-30s
的方法。它保证打印的字符串有30个空格,其中-
表示左对齐
printf("%-30s", "Enter the Dividend (a) :");
scanf("%d", &a);
printf("\n%-30s", "Enter the Divisor (b) :");
scanf("%d", &b);
既然我在第一个printf语句中都使用了单制表符,为什么我在输出屏幕上得到不同的制表符长度
使用tab键可以指定打印的空间数
如果您希望在printf
中使用固定长度,请尝试类似%-30s
的方法。它保证打印的字符串有30个空格,其中-
表示左对齐
printf("%-30s", "Enter the Dividend (a) :");
scanf("%d", &a);
printf("\n%-30s", "Enter the Divisor (b) :");
scanf("%d", &b);
“\t”字符将文本与下一个“\t”停止对齐。在您的情况下,制表符每8个字符停止一次
printf("Enter the Dividend (a) :\t");
“\t”之前的字符数等于24。因此,下一个“\t”将在8*4=32的位置对齐
printf("\nEnter the Divisor (b) :\t");
“\t”之前的字符数=23。因此,下一个“\t”将在8*3=24位置对齐
这不是问题,你也没有做错。您需要了解选项卡在终端中的行为(因为用户可以更改选项卡宽度)。您可以删除“\t”并在printf statment中使用固定长度,如@artm answer中所述 A'\t'字符将文本与下一个'\t'停止对齐。在您的情况下,制表符每8个字符停止一次
printf("Enter the Dividend (a) :\t");
“\t”之前的字符数等于24。因此,下一个“\t”将在8*4=32的位置对齐
printf("\nEnter the Divisor (b) :\t");
“\t”之前的字符数=23。因此,下一个“\t”将在8*3=24位置对齐
这不是问题,你也没有做错。您需要了解选项卡在终端中的行为(因为用户可以更改选项卡宽度)。您可以删除“\t”并在printf statment中使用固定长度,如@artm answer中所述 谢谢你,先生!对我来说很重要。请将此问题标记为重复问题。谢谢,先生!对我来说很重要。请将此问题标记为重复问题。从技术上讲,是“选项卡在所用终端中的行为”,而不是“在C中”。从技术上讲,是“选项卡在所用终端中的行为”,而不是“在C中”。