This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 7 years ago.
I know that I could use printf(%Nd, foo) if N is constant and I know it
But the problem is that N is in variable and calculated in program
I could do it with combining sprintf and printf:
sprintf(formatstr, "%%%dd", N);
printf(formatstr, foo);
But is there any cleaner way?
You can put a * in place of the field width. This means the next parameter will specify the width:
printf("%0*d", size, foo);
Related
This question already has answers here:
format string used in printf function in C programming
(2 answers)
Closed 7 months ago.
when you write code such as
printf("Average score: %f\n", (score[0] + score[1] + score[2]) / 3.0);
what is the point of the %f , is it needed?, i know that sometimes you change the letter after it, what are ways you can use the %?
that's a print formatter. "f" is for floats (decimals). the % is used when you want to put a variable value in a printf statement. So % followed by a letter (f,d,c,s,p, etc) indicates the type.
This question already has answers here:
Initializing variable length array [duplicate]
(3 answers)
Closed 2 years ago.
I want to initialise my matrix to 0, but it does not work because the first part of the matrix is a variable.
int playerCards[playerNum][10] = {0};
I want to make so all the values in the matrix are zero. I have tried the line above, but it tells me that i cannot initialise a the array. For reference, playerNum is an integer between 2 and 6, chosen by the user.
You can use memset():
int playerCards[playerNum][10];
memset(playerCards, 0, sizeof playerCards);
sizeof works for VLAs: in this case playerCards is evaluated, which does not make a difference, but also means that the size is computed at runtime as sizeof(int) * playerNum * 10 using the value of playerNum at the time of instantiation of playerCards.
This question already has answers here:
How to format strings using printf() to get equal length in the output
(6 answers)
Closed 5 years ago.
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.
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");
Use the - flag, as described in the ref:
- Left-justify within the given field width; Right justification is the default (see width sub-specifier).
This question already has answers here:
printf string, variable length item
(2 answers)
Closed 7 years ago.
I need to
printf(%?d)
Where '?' is some int. How can I do it?
I'm using pure c.
I've tried to work with const char* array. But there was no result.
See the printf man page:
int width = 16;
int value = 42;
printf("%*d\n", width, value);
Output:
42
LIVE DEMO
This question already has answers here:
What is the "-->" operator in C++?
(29 answers)
Closed 8 years ago.
I have a question, what mean "-->" in C? For example:
int a, b, c, x;
a=2001;
b=1000;
c=2;
x=a-b*c;
printf("First: %i", x-->0);
It will print "1".
But:
printf("Second: %i", x-->0);
will print "0". Why when I use it second time, it print "0"?
x --> 0 is to be read (x--) > 0.
x-->0 is parsed as (x--) > 0.