Write a program to demonstrate the use of width specifier.

#include<stdio.h>
#include<conio.h>
int main()
{
	int x=55,y=33;
	printf("\n %3d",x-y);
	printf("\n %6d",x-y);
	getch();
}

/*Width is given 3 and 6.Hence the results are displayed at diffrent 
positions on the screen.*/

In this program, we are using the width specifier in printf() function to format the output.

The width specifier is used to specify the minimum number of characters to be used for the output of a value. If the value has fewer characters than the width specifier, the output is padded with blank spaces to fill the width.

In the program, we have two integer variables x and y, initialized to 55 and 33 respectively. We are then using printf() function to display the difference between x and y using the subtraction operator (-).

The first printf statement uses a width specifier of 3 (“%3d”). This means that the output will be at least 3 characters wide, and if the result has fewer than 3 characters, it will be padded with blank spaces to the left. Since the result of x-y is 22, which has only two characters, the output will be padded with one blank space to the left, and the output will be ” 22″.

The second printf statement uses a width specifier of 6 (“%6d”). This means that the output will be at least 6 characters wide, and if the result has fewer than 6 characters, it will be padded with blank spaces to the left. Since the result of x-y is 22, which has only two characters, the output will be padded with four blank spaces to the left, and the output will be ” 22″.

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