Google News
logo
String I/O functions In C Language
The following are the input and output functions of strings in c

         Input functions: scanf(), gets()
Output functions: printf(), puts()

The scanf() and printf() are generic i/o functions that they support all built-in data types such as int, float, long, double, strings,..etc. But gets() and puts() are specialized to scan and print only string data. There is a little difference between scanf() and gets(), while reading string from keyboard, the scanf() accepts character by character from keyboard until either a new line (‘\n’) or blank space is found, which ever comes earlier. Whereas “gets()” accepts until a newline is found. That is it accepts white spaces & tab also, these input functions append a null character at end of string, the formatted string %s is used in printf() and scanf().for example :
Strings

The scanf() function consider the jack and Jill as 3 strings, whereas gets() considers as single string. In case to scan total string using scanf(), then it should be scanf(“%s%s%s”, a,b,c); here a,b,c are three arrays.

The printf() and puts() is work in similar way. All I/O functions take first byte address (base address of array) as argument and prints the given string using pointer.

  Program : The following program is an example of string I/O functions.
#include<stdio.h>
#include<string.h>
int main()
{
    char name[30];
    printf(“Enter name: “);
    gets(name);     //Function to read string from user.
    printf(“Name: “);
    puts(name);    //Function to display string.
    return 0;
}
Output :

Enter name: Free Time Learning
Name: Free Time Learning