Reading Strings from Terminal Using scanf ( ):


The familiar input function scanf ( ) can be used with %s format specification to read in a string of characters. For example:
                                    char address [10];
                                    scanf(“%s”, address);
The problem with the scanf ( ) function is that it terminates its input on the first white space it finds. A white space includes blanks, tabs, carriage returns, form feeds, and new lines. Therefore, if the following line of text is typed in at terminal,
                                    NEW YORK
then only the string “NEW” will be read into the array address, since the blank space after the word “NEW” will terminate the reading of string.

To solve this problem gets ( ) function is used discussed in next topic. 

Previous                                                                                    Next


Powered by Blogger.