简体   繁体   English

在 C 中使用 printf 打印空格数

[英]print number of spaces using printf in C

I was wondering how can I do it ,to print certain number of spaces using printf in CI was thinking something like this,but also my code doesn't print after the first printf statement,my program compiles perfectly fine tho.I'm guessing I have to print N-1 spaces but I'm not quite sure how to do so.我想知道我该怎么做,在 CI 中使用 printf 打印一定数量的空格是在想这样的事情,但我的代码在第一个 printf 语句之后也没有打印,我的程序编译得非常好。我猜我必须打印 N-1 个空格,但我不太确定该怎么做。

Thanks.谢谢。

#include <stdio.h>
#include <limits.h>
#include <math.h>

int f(int);

int main(void){
    int i, t, funval,tempL,tempH;
    int a;

    // Make sure to change low and high when testing your program
    int low=-3, high=11;
    for (t=low; t<=high;t++){
        printf("f(%2d)=%3d\n",t,f(t));                 
    }
    printf("\n");
    if(low <0){
        tempL = low;
        tempL *=-1;
        char nums[low+high+1];
        for(a=low; a <sizeof(nums)/sizeof(int);a+5){
            printf("%d",a);
        }
    }
    else{
        char nums[low+high];
        for(a=low; a <sizeof(nums)/sizeof(int);a+5){
            printf("%d",a);
        }
    }

    // Your code here...
    return 0;
}


int f(int t){
    // example 1
    return (t*t-4*t+5);

    // example 2
    // return (-t*t+4*t-1);

    // example 3
    // return (sin(t)*10);

    // example 4
    // if (t>0)
    //  return t*2;
    // else
    //  return t*8;
}

the output should be something like this:输出应该是这样的:

   1       6       11      16      21      26     31
   |       |       |       |       |       |       |  

Printing n spaces打印n空格

printf has a cool width specifier format that lets you pass an int to specify the width. printf有一个很酷的宽度说明符格式,可以让您传递一个int来指定宽度。 If the number of spaces, n , is greater than zero:如果空格数n大于零:

printf("%*c", n, ' ');

should do the trick.应该做的伎俩。 It also occurs to me you could do this for n greater than or equal to zero with:我也想到你可以用大于或等于零的n来做到这一点:

printf("%*s", n, "");

Printing 1, 6, 11, ... pattern打印 1, 6, 11, ... 图案

It's still not fully clear to me what you want, but to generate the exact pattern you described at the bottom of your post, you could do this:我仍然不完全清楚您想要什么,但要生成您在帖子底部描述的确切模式,您可以这样做:

for (i=1; i<=31; i+=5)
    printf("%3d   ", i);
printf("\n");
for (i=1; i<=31; i+=5)
    printf("  |   ");
printf("\n");

This outputs:这输出:

  1     6    11    16    21    26    31   
  |     |     |     |     |     |     |   

Had your objective been :你的目标是:

Start printing at a specified width using printf使用 printf 以指定的宽度开始打印

You could achieve it like below :你可以像下面那样实现它:

printf("%*c\b",width,' ');

Add the above stuff before printing actual stuff, eg.在打印实际内容之前添加上述内容,例如。 before a for-loop.在 for 循环之前。

Here the \\b positions the cursor one point before the current position thereby making the output appear to start at a particular width, width in this case.这里的\\b位置光标一个点的当前位置之前,从而使得输出出现在一个特定的宽度开始, width在这种情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM