Display the string with different formats :
The printf ( ) function is used for displaying the various datatypes.The printf( ) function with is format is to be used for displaying the string on the screen.Various formats are shown in the below table..|
Sr.no
|
Statement
|
Output
|
|
1.
|
Printf(“%s\n”,text);
|
PRABHAKAR
|
|
2.
|
Printf(“%.5s\n”,text);
|
PRABH
|
|
3.
|
Printf(“%.8s\n”,text);
|
PRABHAKA
|
|
4.
|
Printf(“%.15s\n”,text);
|
PRABHAKAR
|
|
5.
|
Printf(“%-10.4s\n”,text);
|
PRAB
|
|
6.
|
Printf(“%11s\n”,text);
|
PRABHAKAR
|
Where the text is 'PRABHAKAR'
- The first statement displays the output 'PRABHAKAR'.The entire string can be displayed with the first statement.
- We can also specify the precision with character string,Which is to be displayed.The precision is provided after decimal point.For instance in the 2nd statement in the above table the first five characters are displayed. Here the integer value 5 on the right side of the decimal point,which specify the five characters to be displayed.
- In the 3rd statement the first eight characters are displayed.
- While statement number four-display the entire strings.
- the fifth statement with -sign (Ex.%-10.4s),display the string with left justified.
- When the field length is less than the length of the string the entire string is printed.when it is greater tan the length of the string,blank spaces are initially printed followed by the string.this effect can be seen in the 6th statement.
- When number of character to be displayed is specified as zero after decimal point nothing will be displayed.
#include <stdio.h>
#include <conio.h>
void main( )
{
char text[ ]="HAVE A NICE DAY";
int i=0;
while(i<=15)
{
printf("%c",text[i]);
i++;
}
}
OUTPUT:
HAVE A NICE DAY
Explanation :
In this program while loop is used for printing the character up to length of 15.There after while loop terminates when variable 'i' reaches to 15.