<http://lib.cnfolio.com/ENG421StandardInputOutput>
Introduction to Algorithms and Programming

Standard input and output



Default input and output streams






Types of input and output data






Common input functions






Reference documentation about standard input functions and options:




Character input





#include <stdio.h>

int main( void )
{
   char code;

   code = getchar();
   printf( "Found %c \n", code );

   code = getc( stdin );
   printf( "Found %c \n", code );

   scanf( "%c", &code );
   printf( "Found %c \n", code );
}




Classification of characters


The ctype.h header file contains some functions to identify different character types:
isalpha() returns 1 if the character is a letter
isdigit() returns 1 if the character is a number
isspace() returns 1 if the character is a space or newline
tolower() returns the input character in lowercase format
toupper() returns the input character in uppercase format


#include <stdio.h>
#include <ctype.h>

int main( void )
{
   char code;

   code = getchar();
   printf( "Found %c \n", code );

   if ( isalpha( code ) )
      printf( "Alphabetical\n" );

   if ( isdigit( code ) )
      printf( "Numerical\n" );
}




Text string input





#include <stdio.h>

int main( void )
{
   char text[ 64 ] = { '\0' }; /* clear the string */

   scanf( "%10s", text );
   printf( "Found %s \n", text );

   scanf( "%10c", text );
   printf( "Found %s \n", text );
}




Integer input





#include <stdio.h>

int main( void )
{
   int number = 0;

   while ( scanf( "%d", &number ) == 1 )
   {
      printf( "Found %d \n", number );
   }
}




Floating point input





#include <stdio.h>

int main( void )
{
   float number = 0;

   while ( scanf( "%f", &number ) == 1 )
   {
      printf( "Found %f \n", number );
   }
}




General input format


%[*][width]type


#include <stdio.h>

int main( void )
{
   int dd, mm, yy;

   if ( scanf( "%d%*c%d%*c%d", &dd, &mm, &yy ) == 3 )
      printf( "Found %d-%d-%d", dd, mm, yy );
   else
      printf( "Error with input" );
}




Common output functions






Reference documentation about standard output functions and options:




Character output





#include <stdio.h>

int main( void )
{
   char code = 'a';

   putchar( code );
   putchar( '\n' );

   code = 'b';
   putc( code, stdout );

   code = 'c';
   printf( "%c\n", code );
}




Text string output





#include <stdio.h>

int main( void )
{
   puts( "Step 1" );
   printf( "%s", "Step 2" );
}




Integer output





#include <stdio.h>

int main( void )
{
   printf( "%d \n", 15 );
   printf( "%i \n", 15 );
   printf( "%u \n", 15 );
   printf( "%u \n", -15 );
   printf( "%x \n", 15 );
   printf( "%X \n", 15 );
}




Floating point output





#include <stdio.h>

int main( void )
{
   float code1 = 0.245722;
   double code2 = 0.00451278;

   printf( "%.9f \n", code1 );
   printf( "%.9lf \n", code2 );
   printf( "%e \n", code1 );
   printf( "%E \n", code1 );
   printf( "%g \n", code1 );
   printf( "%G \n", code1 );
}




General output format


%[flags][width][.precision]type

Some useful flags:
Left justify in field
+ Display positive or negative sign
0 Pad output with zeroes


#include <stdio.h>

int main( void )
{
   float code1 = 0.245722;
   double code2 = 0.00451278;

   printf( "%+7.4f \n", code1 * -1 );
   printf( "%+7.4lf \n", code2 );
   printf( "%10e \n", code1 );
   printf( "%10E \n", code1 );
   printf( "%10g \n", code1 );
   printf( "%10G \n", code1 );
}




Return value and escape characters


The return value of printf() is the number of characters printed.

Escape characters permit special characters to be printed:
\" double quotes
\' single quote
\? question mark
\n newline
\\ backslash
%% percentage sign


#include <stdio.h>

int main( void )
{
   float code1 = 0.245722;
   double code2 = 0.00451278;
   int wordCount;

   wordCount = printf( "%+7.4f \n", code1 * -1 );
   printf( "Printed %d characters \n", wordCount );
   printf( "%+7.4lf \n", code2 );
   printf( "%10e \n", code1 );
   printf( "%10E \n", code1 );
   printf( "%10g \n", code1 );
   printf( "%10G \n", code1 );
}