Writing strings

we have already used printf () function for writing with %s format to print strings to the screen. For example

char name [11];
printf (“%s”, name);

The above printf () function will display the entire contents of the array name. similar to scanf () function, the printf () function too has its own limitations, and so we have two functions putchar () and puts () to write char name [11];

printf (“%s”, name);

characters. These are similar to getchar () and gets ().

Program use of putchar () function

#include <stdio.h>
    void main ()
    {
        char ch; 
        printf (“\nEnter characters:”);
        ch = getchar ();
        printf (“nEntered characters is: “);
        putchar (ch);
    }

Output

Enter a character: a
Entered character is: a

The function putchar () will display the characters a on the screen i.e., only one characters. The function puts () when used will display the entire string. For example:

puts (“welcome”);

The functions puts () is an extension of printf () function i.e., it is a combination of printf () with a new line character. In printf () we use new line characters ‘\n’ to skip to the next line. Whereas in puts () it will automatically skip to the next line after printing the message on the screen.

Program use of puts () function

#include <stdio.h>
    void main () 
    {
        char name [80];
        puts (“Enter a string”);
        gets (name);
        puts (“inputted string is”);
        puts (name);
    }

Output

Enter a string
Bhubaneswar
Inputted string is
Bhubaneswar



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext