Scanf Function

    1100621 VIEW 1 0

Scanf function in C which allows the programmer to accept input from the standard input device (keyboard) and stores them in variables. The scanf is similar to printf function.  Instead of printing data on the output device it reads data entered from the input device.

  • The scanf function reads input from the standard input device into one or more variables
    scanf(“%lf”, &miles);
    

    Reads a real number that (by default) the user has typed in into the variable miles

  • Each variable must be preceded by the ampersand (&) address-of operator
  • We can read values into multiple variables with a single scanf as long as we have corresponding format strings for each variable
    scanf(“%c%c%c”, &letter_1, &letter_2, &letter_3);
    
  • The user must press the return or enter key to cause the value(s) to be read
  • We can read values into multiple variables with a single scanf as long as we have correpsonding format strings for each variable
    scanf(“%c%c%c”, &letter_1, &letter_2, &letter_3);
    
  • The user must press the return or enter key to cause the value(s) to be read
  • For numeric values (associated with a %lf or %d placeholder) blank spaces are skipped
  • For character values (associated with a %c placeholder) blank spaces are not skipped (since a space is a character!)