printf(“%-25s, “%.*s\n”)

Jyothi
1 min readNov 23, 2024

--

This printf statement prints two strings: left aligned within a width of 25 characters and a substring with the specified length.

This “%-Ns” option prints the string in left aligned format in width of N characters.

The format “%.*s” is used to print a substring from a given string, where the length of the substring is passed along with printf call. The “*” in “.*” allows you to define the length at runtime. If the specified length exceeds the maximum length of the string, the entire string will be printed.

The “*” in format specifiers enable dynamic control of width and/or precision at runtime. “%.*s”,”%.*f”,”%.*e” formats dynamically control precision. “%*s”, “%*d”, “%*f” formats dynamically control width.

printf(“%-*.*s\n”, 10,4,”Hello world”); // left aligned width of 10 chars and prints 4 chars.

The format “%.Ns” prints up to N characters.

“%%”, “\\”, “\””, “\’” formats print “%”, “\”, “ “ “,” ‘ “.

Some size formats are: “%hd” for short int, “%zd” for sizeof() , “%td” for pointers differences

--

--

No responses yet