Writing strings in using %s format

Program 34) Write a program to store the string "United Kingdom" in the array country and display the string under format specifications.

This program output illustrates the following features of the %s specifications.
1. When the field width is less than the length of the 
    string, the entire string is printed.
2. The integer value on the right side of the decimal 
    point specifies the numbers of characters to be 
    printed.
3. When the number of characters to be printed is 
    specified as zero, nothing is printed.
4. The minus sign in the specification causes the string
    to be printed left-justified.
5. The specification % .ns prints the first n characters of 
    the string.


                                     PROGRAM

main ( )
 {
       char country [15] = "United Kingdom" ;
       printf ("\n\n") ;
       printf ("*123456789012345*\n") ;
       printf (" --------- \n") ;
       printf ("%15s\n", country) ;
       printf ("%5s\n", country) ;
       printf ("%15.6s\n", country) ;
       printf ("%-15.6s\n", country) ;
       printf ("%15.0s\n", country) ;
       printf ("%.3s\n", country) ;
       printf("%s\n", country) ;
       printf ("---------- \n") ;
 }


Output:       *123456789012345
                   ---------
                   United Kingdom
                   United Kingdom
                              United
                   United


                   Uni
                   United Kingdom
                  ----------

No comments:

Post a Comment