Writing Strings to Screen:


Using putchar ( );
Like getchar ( ), C supports another character handling function putchar ( ) to output the values of character variables. It takes the following form:
                        char ch = ‘A’;
                        putchar (ch);
The function putchar ( ) requires one parameter. This statement is equivalent to:
                        printf(“%c”,ch);
We can use putchar ( ) function repeatedly to output a string of characters stored in an array using a loop. For example:
                        char name[6] =”PARIS”;
                        for (i=0;i<5;i++)
                        {
                                    putchar (name[i]);
                                    putchar(‘\n’);

                        }

Using puts ( );
Another and more convenient way of printing string values is to use the function puts declared in the header file <stdio.h>. This is a one parameter function and invoked as under.
                        puts ( str );
Where str is a string variable containing a string value. This prints the value of the string variable str and then moves the cursor to the beginning of the next line on the screen. For example, the program segment
                        char name [20];
                        gets (name);
                        puts (name);
reads a line of text from keyboard and displays it on the screen. Note that the syntax is very simple compared to using scanf ( ) and printf ( ) functions.

Previous                                                                                    Next


Powered by Blogger.