L-30 MCS 275 Mon 26 Mar 2001

Below are the programs we discussed in class.

Reading integers :

/* L-30 MCS 275 Monday 26 March 2001 reading integers with scanf() */

/* A conversion specification begins with a % and ends with a conversion
   character, such as d, i, o, x, as illustrated below. */

#include <stdio.h>

int main()
{
   int i;

   printf("\nReading integer numbers...\n\n");

   printf("Give a decimal integer : ");
   scanf("%d", &i);
   printf("-> Your decimal integer : %d\n", i);
   printf("Give an octal number (preceded by 0) : ");
   scanf("%i", &i);
   printf("-> Your octal number : %o = %d (decimal)\n", i, i);
   printf("Give a hexadecimal number (preceded by 0x) : ");
   scanf("%i", &i);
   printf("-> Your hexadecimal number : %x = %d (decimal)\n", i, i);
   printf("Give an octal number (without leading 0): ");
   scanf("%o", &i);
   printf("-> Your octal number : %o = %d (decimal)\n", i, i);
   printf("Give a hexadecimal number (without leading 0x) : ");
   scanf("%x", &i);
   printf("-> Your hexadecimal number : %x = %d (decimal)\n", i, i);

   return 0;
}
Reading floating-point numbers :
/* L-30 MCS 275 Monday 26 March 2001 reading floats with scanf() */

/* The conversion specification for characters is usually %f or %lf.
   In scientific computing, the distinction between ordinary floats
   and long floats (of type double) is very important.
   Note that the same l conversion character applies to integers,
   without l, we convert to short int, with l, to long int. */

#include <stdio.h>

int main()
{
   float f;
   long double x;

   printf("\nReading floats...\n\n");

   printf("Give a long floating number (>15 digits) : ");
   scanf("%f", &f);
   printf("-> Your number as float : %.16f\n", f);

   printf("Give a long floating number (>15 digits) : ");
   scanf("%lf", &x);
   printf("-> Your number as double : %.16f\n", x);

   return 0;
}
Reading characters :

/* L-30 MCS 275 Monday 26 March 2001 reading characters with scanf() */

/* The conversion specification for characters is usually %c.
   In addition, we can skip spaces and add ordinary characters.
   To test the correctness of the input, check the return value
   of scanf(), which is the number of successful conversions. */

#include <stdio.h>

int main()
{
   char c;
   float amount;
   int okay;

   printf("\nReading characters...\n\n");

   printf("Give a character (without leading spaces) : ");
   scanf("%c", &c);
   printf("-> Your character : %c\n", c);
   printf("Give a character (eventually preceded by spaces) : ");
   scanf(" %c", &c);
   printf("-> Your character : %c\n", c);

   c = getchar();  /* must skip enter symbol from previous read */

   printf("Give dollar amount, preceded by $ : ");
   okay = scanf("$%f", &amount);
   if (okay == 1)
      printf("-> Your dollar amount : $%.2f\n", amount);
   else
      printf("Failed to read dollar amount, $ forgotten?\n");
 
   return 0;
}

On the use of scanset :

/* L-30 MCS 275 Monday 26 March 2001 using scan set with scanf() */

/* A conversion specification of the form %[string] is for special strings.
   For instance, the format %[abc] will scan abc from input and then stop.
   If the format is preceded by a circumflex ^, then any character in the
   input may precede the string.  An illustration of this is below. */

#include <stdio.h>

int main()
{
   char sentence[30];

   printf("\nUsing scan set...\n\n");

   printf("Give a sentence of no more than 30 characters,\n");
   printf("terminated by a point :\n\n");

   scanf("%29[^.]", sentence);

   printf("\nYour sentence : %s\n", sentence); 

   return 0;
}