Sprintf( ) and sscanf( ) Functions

The sprintf( ) function works similar to the printf( ) function except for one small difference. Instead of sending the output to the screen as printf( ) does, this function writes the output to an array of characters. The following program illustrates this.

Example:1
1. Program to Simple Function.

    #include<stdio.h>
    void main()
    {
        int i = 10 ;
        char ch = 'A' ;
        float a = 3.14 ;
        char str[20] ;
        printf ( "\n%d %c %f", i, ch, a ) ;
        sprintf ( str, "%d %c %f", i, ch, a ) ;
        printf ( "\n%s", str ) ;
    }
       
        
Output:
            10 A 3.140000
            10 A 3.140000
            --------------------------------
            Process exited after 0.05335 seconds with return value 14
            Press any key to continue . . .
        
Example:2
Formated String

#include<stdio.h>
void main( )
{
int weight = 63 ;
printf ( "\nweight is %d kg", weight ) ;
printf ( "\nweight is %2d kg", weight ) ;
printf ( "\nweight is %4d kg", weight ) ;
printf ( "\nweight is %6d kg", weight ) ;
printf ( "\nweight is %-6d kg", weight ) ;
}
  
        
Output:
        weight is 63 kg
        weight is 63 kg
        weight is   63 kg
        weight is     63 kg
        weight is 63     kg
        --------------------------------
        Process exited after 0.04775 seconds with return value 20
        Press any key to continue . . .
        
Example:3

#include<stdio.h>
void main( )
{
    printf ( "\n%f %f %f", 5.0, 13.5, 133.9 ) ;
    printf ( "\n%f %f %f", 305.0, 1200.9, 3005.3 ) ;

    /*

    Even though the numbers have been printed, the numbers have not
    been lined up properly and hence are hard to read. A better way
    would be something like this...

    */

    printf ( "\n%10.1f %10.1f %10.1f", 5.0, 13.5, 133.9 ) ;
    printf ( "\n%10.1f %10.1f %10.1f", 305.0, 1200.9, 3005.3 );
}
           
        
Output:
                5.000000 13.500000 133.900000
                305.000000 1200.900000 3005.300000
                       5.0       13.5      133.9
                     305.0     1200.9     3005.3
                --------------------------------
                Process exited after 0.04943 seconds with return value 33
                Press any key to continue . . .
        
Example:4
4.Formatting strings with printf( )

#include<stdio.h>
void main( )
{
char firstname1[ ] = "Sandy" ;
char surname1[ ] = "Malya" ;
char firstname2[ ] = "AjayKumar" ;
char surname2[ ] = "Gurubaxani" ;

/* without formated */
printf ( "\n%s%s", firstname1, surname1 ) ;
printf ( "\n%s%s", firstname2, surname2 ) ;

/* With Formated */
printf ( "\n%20s%20s", firstname1, surname1 ) ;
printf ( "\n%20s%20s", firstname2, surname2 ) ;
}
       
        
Output:
            SandyMalya
            AjayKumarGurubaxani
                           Sandy               Malya
                       AjayKumar          Gurubaxani
            --------------------------------
            Process exited after 0.05533 seconds with return value 41
            Press any key to continue . . .
        
Example:5
5.The following program shows a few of these conversions, some sensible, some weird.

#include<stdio.h>
main( )
{
char ch = 'z' ;
int i = 125 ;
float a = 12.55 ;
char s[ ] = "hello there !" ;
printf ( "\n%c %d %f", ch, ch, ch ) ;
printf ( "\n%s %d %f", s, s, s ) ;
printf ( "\n%c %d %f",i ,i, i ) ;
printf ( "\n%f %d\n", a, a ) ;
}
     
        
Output:
                z 122 0.000000
                hello there ! 6487552 0.000000
                } 125 0.000000
                12.550000 -1610612736

                --------------------------------
                Process exited after 0.05862 seconds with return value 23
                Press any key to continue . . .
        
Example:6
6.Unformatted Console I/O Functions

#include<stdio.h>
void
    main( )
    {
    char ch ;
    printf ( "\nPress any key to continue" ) ;
    getch( ) ; 
    /* will not echo the character */
    printf ( "\nType any character" ) ;
    ch = getche( ) ; 
    /* will echo the character typed */
    printf ( "\nType any character" ) ;
    getchar( ) ; 
    /* will echo character, must be followed by enter key */
    printf ( "\nContinue Y/N" ) ;
    fgetchar( ) ; 
    /* will echo character, must be followed by enter key */ // ERROR Check it.
    }
     
                
        
Output:
            Press any key to continue
            Type any characterS
            Type any characterSachin

            Continue Y/N
            --------------------------------
            Process exited after 9.1 seconds with return value 97
            Press any key to continue . . .
            
        
Example:7
7. ERROR let's check first

#include<stdio.h>
void main( )
{
char ch = 'A' ;
putch ( ch ) ;
putchar ( ch ) ;
fputchar ( ch ) ;
putch ( 'Z' ) ;
putchar ( 'Z' ) ;
fputchar ( 'Z' ) ;
}
     
                
        
Output:
            AAAZZZ
            --------------------------------
            Process exited after 0.05162 seconds with return value 90
            Press any key to continue . . .
        
Example:8

#includ<stdio.h>
void main( )
{
char name[50] ;
char footballer[40] ;
printf ( "\nEnter name " ) ;
scanf ( "%s", name ) ;
printf ( "%s", name ) ;

puts ( "Enter name" ) ;
//gets ( footballer ) ; 
/* sends base address of array */ 
// gets not working check it
puts ( "Happy footballing!" ) ;
puts ( footballer ) ;
}
     
        
Output:
        Enter name Sachin
        SachinEnter name
        Happy footballing!


        --------------------------------
        Process exited after 3.361 seconds with return value 0
        Press any key to continue . . .