Short answer – yes, long answer: not how you want it.
You can use the %* form of printf, which accepts a variable width. And, if you use ‘0’ as your value to print, combined with the right-aligned text that’s zero padded on the left..
printf("%0*d\n", 20, 0);
produces:
00000000000000000000
With my tongue firmly planted in my cheek, I offer up this little horror-show snippet of code.
Some times you just gotta do things badly to remember why you try so hard the rest of the time.
#include <stdio.h> int width = 20; char buf[4096]; void subst(char *s, char from, char to) { while (*s == from) *s++ = to; } int main() { sprintf(buf, "%0*d", width, 0); subst(buf, '0', '-'); printf("%s\n", buf); return 0; }
Related Posts:
- How to print an unsigned char in C?
- Reading float using scanf in c
- Reading float using scanf in c
- How many spaces for tab character(\t)?
- Correct format specifier for double in printf
- Scanf/Printf double variable C
- Strings and character with printf
- printf() formatting for hexadecimal
- Printing hexadecimal characters in C
- What is the argument for printf that formats a long?
- What is the printf format specifier for bool?
- How to printf “unsigned long” in C?
- How I can print to stderr in C?
- Is there a printf converter to print in binary format?
- What does “%.*s” mean in printf?
- How to print a char array in C through printf?
- What does %s and %d mean in printf in the C language?
- How to printf a memory address in C
- Data argument not used by format strings in C
- How can one print a size_t variable portably using the printf family?
- %i or %d to print integer in C using printf()?
- How can you print multiple variables inside a string using printf?
- error: aggregate value used where an integer was expected
- C’s printf and fprintf(stdout,) are not printing
- Is there a way to have printf() properly print out an array (of floats, say)?
- How do you allow spaces to be entered using scanf?
- How to write to a file using open() and printf()?
- Using floats with sprintf() in embedded C
- Printf width specifier to maintain precision of floating-point value
- Which of sprintf/snprintf is more secure?
- Does C have a string type?
- Execution of printf() and Segmentation Fault
- printf format specifiers for uint32_t and size_t
- How do you read scanf until EOF in C?
- How to display hexadecimal numbers in C?
- Simple C scanf does not work? [duplicate]
- Two decimal places using printf( )
- How do you format an unsigned long long int using printf?
- What primitive data type is time_t? [duplicate]
- How to format strings using printf() to get equal length in the output
- How to format strings using printf() to get equal length in the output
- What do \t and \b do?
- What does %s mean inside a string literal?
- write() to stdout and printf output not interleaved?
- warning: implicit declaration of function
- What is the difference between float and double?
- What is the difference between ++i and i++?
- How do function pointers in C work?
- “Expected expression before ‘ { ‘ token”
- How to generate a random int in C?
- C pointers and arrays: [Warning] assignment makes pointer from integer without a cast
- How to print the array?
- How to solve the error: assignment to expression with array type
- segmentation fault : 11
- What does “1e” mean?
- Getting “conflicting types for function” in C, why?
- In C programming, what is `undefined reference`error, when compiling?
- Passing by reference in C
- double free or corruption (fasttop)
- error: expected declaration or statement at end of input in c
- Map like structure in C: use int and struct to determine a value
- #31 expression must have integral type
- How do you pass a function as a parameter in C?
- Simple C program to read a file line by line
- What is the cause of flexible array member not at end of struct error?
- Best C/C++ Network Library
- How can I get argv[] as int?
- Why I do get “Cannot find bound of current function” when I overwrite the ret address of a vulnerable program?
- Warning: assignment from incompatible pointer type
- How do malloc() and free() work?
- Using true and false in C
- valgrind – Address —- is 0 bytes after a block of size 8 alloc’d
- C convert floating point to int
- Flushing buffers in C
- Undefined Reference issues using Semaphores
- What is time(NULL) in C?
- C++ – Too Many Initializers for Arrays
- Reading string by char till end of line C/C++
- How to use EOF to run through a text file in C?
- How does one represent the empty char?
- Removing trailing newline character from fgets() input
- How can one see content of stack with GDB?
- Is there any difference between 1U and 1 in C?
- segmentation fault: 11 in C code
- Proper way to empty a C-String
- malloc(): memory corruption
- Is there an alternative sleep function in C to milliseconds?
- Get size of pointer in C
- switch case: error: case label does not reduce to an integer constant
- warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
- warning: return makes pointer from integer without a cast but returns integer as desired
- Dynamic vs static array in c
- Suggestions needed: Effective Java to C source code converter
- The difference between char * and char[] [duplicate]
- How to clear input buffer in C?
- Implicit function declarations in C
- Convert long long to string in C?
- how to stop a loop arduino
- How to normalize a mantissa
- Compiler warning – suggest parentheses around assignment used as truth value