Home   MosaicPresentation format
<http://cnfolio.com/IntroToComputingTopic05Print>
Introduction to Computing – B142L

Input and output

Academic year 2011/12



Objectives:
  1. Standard data streams
  2. Common output functions
  3. Common input functions






Default input and output streams










Types of input and output data










Common output functions










Character output







 


References:






Text string output







 


References:






Integer output







 


References:






Floating point output







 


References:






General output format



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

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


 


References:






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


 


References:






Common input functions










Character input







 


References:






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


 


References:






Text string input







 


References:






Integer input







 


References:






Floating point input







 


References:






General input format and return value



%[*][width]type


 


References:






Review questions




Formatted input and output


Which is not a valid input and output format for C programs?

Float
Character
Line
Binary






Review questions




Formatted input and output


puts( "Hello world" );

Which function below is equivalent to the C statement above?

printf() with %s
scanf() with %d
scanf() with %f
printf() with %t






Review questions




Formatted input and output


Use the string "Jack and Jill" as the input to the program below. What is the value of the variable key at the end of the program?

#include <stdio.h>

int main( void )
{
   char text[ 64 ] = { '\0' };
   char key;

   scanf( "%7s", text );
   scanf( "%c", &key );
}

space
n
a
k