Sunday, 21 January 2018

Reading Strings From the Terminal


Reading Strings From the Terminal


Reading Strings From the Terminal
The familiar input function scanf can be used with %s format specification to read in a string of characters.
        char address[15];
        scanf("%s", address);
   The problem with the scanf function is that it terminates its input on the first white space it finds.Therefore if the following line of text is typed in at the 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 string.

Note: Unlike previous scanf calls, in the case of character arrays , the ampersand(&) is not required
before the variable name. The scanf function automatically terminates the string that is read with a null character and therefore the character array should be large enough to hold the input string plus the null character.'C' does not provide operators that work on strings directly. For instance we cannot assign one string to another directly.
      string = "ABC";
      string1 = string2;   are not valid.

Writing strings to screen:
 The %s format can be used to display an array of characters that is terminated by the null character.% 10.4s indicates that the first four characters are to be printed in a field width of 10 columns.%-10.4s indicates that the string will be printed left justified.

Note :
1.When the field width is less than the length of the string, the entire string is printed.
2.The integer value on the right side of the decimal print  specifies the number of characters to be printed.
3.When the number of characters to be printed is specified as  'zero' nothing is printed.
4.The minus sign in the specification causes the string to be   printed left justified

Related Posts:

  • Introduction To Functions Introduction To Functions Functions are building blocks of C. The general form of C (User-defined) function is:       Return-type-function-name(parameter list)       parameter declaration &… Read More
  • Parameter Passing Technique Parameter Passing Technique The parameter passing mechanism refers to the actions taken regarding the parameters when a function is invoked. There are two parameter passing techniques available in C. They are: 1) Call B… Read More
  • Case Control Structure(SWITCH) Case Control Structure(SWITCH) Introduction:           In real life we are often faced with situations where we are required to make a choice between a number of  alternatives rather than on… Read More
  • GO TO Statement GO TO Statement This Keyword is used when we want to transfer the control to some point in the program which is not in the normal, step by step sequence of a program.  It takes control from one place to another un… Read More
  • The NOT(!) Operators The NOT(!) Operators So far we have used only the logical operators && and ||.Third operator is the Not operator, written as !. This operator reverses the result of the expression it operates on.For example,if th… Read More

0 comments:

Post a Comment