Write a program to demonstrate the use of width specifier.

#include<stdio.h>
#include<conio.h>
int main()
{
	printf("\n %.2s","saloni");
	printf("\n %.3s","saloni");
	printf("\n %.4s","saloni");
	printf("\n %.5s","saloni");
	printf("\n %.6s","saloni");
	getch();
}

This program demonstrates the use of the width specifier in C. The width specifier allows us to specify the minimum width of a field when printing output using the printf() function.

In the above program, we have used the %s format specifier to print a string. The .2, .3, .4, .5, and .6 in front of the s specifier specify the minimum width of the field. The result of the program will be:

sa
sal
salo
salon
saloni

In the first line, the width specifier is set to 2, so only the first two characters of the string “saloni” are printed. In the second line, the width specifier is set to 3, so the first three characters of the string “saloni” are printed. Similarly, in the third, fourth, and fifth lines, the width specifier is set to 4, 5, and 6, respectively.

Thanks

Write a program to demonstrate the use of width specifier.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top