home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tyc / list1410.c < prev    next >
C/C++ Source or Header  |  1993-10-16  |  1KB  |  54 lines

  1.  /* Demonstrates some uses of scanf(). */
  2.  
  3.  #include <stdio.h>
  4.  
  5.  void clear_kb(void);
  6.  
  7.  main()
  8.  {
  9.      int i1, i2;
  10.      long l1;
  11.      float f1;
  12.      double d1;
  13.      char buf1[80], buf2[80];
  14.  
  15.      /* Using the l modifier to enter long integers and doubles. */
  16.  
  17.      puts("Enter an integer and a floating point number.");
  18.      scanf("%ld %lf", &l1, &d1);
  19.      printf("You entered %ld and %lf.\n",l1, d1);
  20.      puts("The scanf() format string used the l modifier to store");
  21.      puts("your input in a type long and a type double.\n");
  22.  
  23.      clear_kb();
  24.  
  25.      /* Use field width to split input. */
  26.  
  27.      puts("Enter a 5 digit integer (for example, 54321).");
  28.      scanf("%2d%3d", &i1, &i2);
  29.  
  30.      printf("You entered %d and %d.\n", i1, i2);
  31.      puts("Note how the field width specifier in the scanf() format");
  32.      puts("string split your input into two values.\n");
  33.  
  34.      clear_kb();
  35.  
  36.      /* Using an excluded space to split a line of input into */
  37.      /* two strings at the space. */
  38.  
  39.      puts("Enter your first and last names separated by a space.");
  40.      scanf("%[^ ]%s", buf1, buf2);
  41.      printf("Your first name is %s\n", buf1);
  42.      printf("Your last name is %s\n", buf2);
  43.      puts("Note how [^ ] in the scanf() format string, by excluding");
  44.      puts("the space character, caused the input to be split.");
  45.  }
  46.  
  47.  void clear_kb(void)
  48.  
  49.  /* Clears stdin of any waiting characters. */
  50.  {
  51.      char junk[80];
  52.      gets(junk);
  53.  }
  54.