0

This is a small portion of my code:

char *a; asprintf(&a,"%%%ds",Max_FnLen); printf(a,files[i-1]->d_name); free(a); printf("%s",KNRM); if ( (i % (180/Max_FnLen)) == 0) printf("\n"); 

Its running fine but I want to left align the output but it is coming right aligned by default can anyone help me with this.

1
  • 1
    To use a variable to specfy the width, use e.g. printf("%.*s", len, str). The * specifies a variable in the argument list is to be used for the width (or precission). Commented Sep 11, 2017 at 11:32

2 Answers 2

0

Use "-" modifier like: printf("%-20s\n", "short line");

In your case:

char *a; asprintf(&a,"%%-%ds",Max_FnLen); printf(a,files[i-1]->d_name); free(a); printf("%s",KNRM); if ( (i % (180/Max_FnLen)) == 0) printf("\n"); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much @MrCryo . It was a huge help!
%-.*s should be sufficient I think. (I might not remember the correct ordering, but you’ve got the idea)
0

Use the - flag, as described in the ref:

- Left-justify within the given field width; Right justification is the default (see width sub-specifier).

1 Comment

It works if we are using a constant value to fix the spaces but as I am using a variable it's not working

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.