How to achieve left align in the C language?
In C language, you can achieve left alignment using the printf function. Just use the %- in the format control string to specify left alignment.
For example, the following code will output a left-aligned string:
#include <stdio.h>
int main() {
char str[] = "Hello, world!";
printf("%-20s\n", str);
return 0;
}
The output is:
Hello, world!
In the above code, the % signifies the beginning of format control, – indicates left alignment, 20 signifies a field width of 20 characters, and s represents a string type. By using %-20s format control for strings, the string “str” is output with left alignment and a field width of 20 characters. If the length of the string is less than 20 characters, spaces will be used on the right side to fill up to the specified field width.