26

I was wondering how can I do it ,to print certain number of spaces using printf in C I 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.

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
   |       |       |       |       |       |       |  
5
  • probably if(low <0){...char nums[low+high+1]; : low -> tempL, a+5 -> a+=5. and a < sizeof(..) is int < unsigned type, a <sizeof(nums)/sizeof(int) -> a < (int)sizeof(nums) Commented Sep 1, 2014 at 16:09
  • @BLUEPIXY I'm assigning the type at the top int a; if i don't do it i will get the C99 error since I'm coding on C89 Commented Sep 1, 2014 at 16:19
  • char nums[low+high+1]; But it is a lie because it is being used in the middle of the code. Commented Sep 1, 2014 at 16:21
  • @BLUEPIXY ohh okay so i should declare the array at the top of the code ,i tried to do so ,but since I dont know the size of the array it gave me an error Commented Sep 1, 2014 at 16:29
  • Also, It can not be used VLA(Variable length array) in C89. Commented Sep 1, 2014 at 16:32

2 Answers 2

68

Printing n spaces

printf has a cool width specifier format that lets you pass an int to specify the width. If the number of spaces, n, is greater than zero:

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:

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

Printing 1, 6, 11, ... pattern

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   
  |     |     |     |     |     |     |   
Sign up to request clarification or add additional context in comments.

6 Comments

@idgabbay ,oh that trick looks pretty cool! ,right now while trying to run my code it compiled and run,but it only printed int low=-3, high=11; for (t=low; t<=high;t++){ printf("f(%2d)=%3d\n",t,f(t)); } printf("\n"); do you know why my code isn't moving forward to the if statements?
@fuscode i'm happy to help but honestly i'm not exactly sure what your code is doing. your for loops aren't incrementing anything... they just say a+5 where it should be incrementing a somehow. can you clarify the code?
@idgabbay you see how the print example shoudl look like that I have 1,6,11 and so on?, well i wanted the a to be incrementing by 5 since teh difference between the number in the print out is 5
@fuscode For-loop condition will always fail because they compare the unsigned integer value and negative value of int.
@idgabbay I have a question, how can I store the numbers of f(t) in an array?
|
1

Had your objective been :

Start printing at a specified width using printf

You could achieve it like below :

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

Add the above stuff before printing actual stuff, eg. before a for-loop.

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.

2 Comments

Why not printf("%*c", width - 1, ' ');?
@Quadslab Possible, the objective here was to portray the use of the escape sequence \b.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.