相关疑难解决方法(0)

在C中获得终端宽度?

我一直在寻找一种从我的C程序中获取终端宽度的方法.我一直想出的是:

#include <sys/ioctl.h>
#include <stdio.h>

int main (void)
{
    struct ttysize ts;
    ioctl(0, TIOCGSIZE, &ts);

    printf ("lines %d\n", ts.ts_lines);
    printf ("columns %d\n", ts.ts_cols);
}
Run Code Online (Sandbox Code Playgroud)

但每次我尝试我得到

austin@:~$ gcc test.c -o test
test.c: In function ‘main’:
test.c:6: error: storage size of ‘ts’ isn’t known
test.c:7: error: ‘TIOCGSIZE’ undeclared (first use in this function)
test.c:7: error: (Each undeclared identifier is reported only once
test.c:7: error: for each function it appears in.)
Run Code Online (Sandbox Code Playgroud)

这是最好的方法吗,还是有更好的方法?如果不是,我怎么能让它工作?

编辑:固定代码是

#include <sys/ioctl.h>
#include <stdio.h>

int main (void)
{
    struct winsize …
Run Code Online (Sandbox Code Playgroud)

c linux terminal width

84
推荐指数
3
解决办法
7万
查看次数

C程序输出没有正确格式化(使用\ t)

我只是想知道和混淆为什么我的输出搞砸了.代码如下.我是初学者,请原谅我缺乏技巧.任何帮助将非常感谢.

#include <stdio.h>

int main(int argc, char *argv[]) 
{

    for (int i = 1; i <= 10; i++)
    {

        for (int j = 1; j <= i; j++)
        {
            printf("*");
        }
        printf("\t");
        for (int k = 1; k <= 11-i; k++)     
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

给出这个输出:

在此输入图像描述

我想要的输出是这样的:
在此输入图像描述

c

17
推荐指数
4
解决办法
1258
查看次数

标签长度可变(\ t)

我是编码的初学者,并试图完成我的作业.

虽然我已经解决了我的问题,但是在这样做时我偶然发现了\ t的意外行为.我得到单个\ t的不同标签长度.这是我的代码:

  #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);

}
Run Code Online (Sandbox Code Playgroud)

这是我的输出:

在此输入图像描述

由于我在两个第一个printf语句中都使用了单选项卡,为什么我在输出屏幕上获得不同的选项卡长度?我在这里做错了什么?

在你投票并埋葬这个问题之前,请考虑我是C的初学者.任何帮助都感激不尽.

我正在使用Visual Studio 2017 RC.

c printf

1
推荐指数
1
解决办法
1365
查看次数

标签 统计

c ×3

linux ×1

printf ×1

terminal ×1

width ×1