Chp 11 Console Input/Output Flashcards
As a result of Dennis Ritchie wanting C to remain compact:
He deliberately omitted everything related with Input/Output.
Well, then how do we get the program to communicate with us?
THe C developers of C compilers write several standard I/O functions and put them in library.
I/O facilities with different operating systems would be different.
Types of I/O
and explain them:
Console I/O: Functions to receive input from the keyboard and write output to VDU.
File I/O functions: functions to perform I/O operations on a floppy disk or hard disk.
What is called console:
The screen and keyboard together are called console.
Types of Console I/O functions:
and whats their difference:
Unformatted Functions:
Example: gets() , puts() for string.
getch(),putch(), getche(),putchar(),getchar() for char.
Formatted Functions:
scanf(), printf() for all data types.
The main difference being formatted functions give us control of where to put it, how many spaces, how many digits after decimal.. etc etc. Allows the i O to be formatted as per our requirements.
function printf()
what it contains and how it works..
printf( ‘Format string’, list of variables );
The format string can contain :
- Char to print as it is.
- Conversion specifications, start with %.
- Escape Sequences, start with /.
How does it interpret the contents of format string?
It examines the string left to right, dumps char on the screen as long as it doesn’t encounter % or /. When % encountered it picks up the variable and print its value in the specified format. and on meeting /, it takes appropriate action.
Format specifications:
%d - asks to print var in integer form.
short signed int. Signed int %d or %I
long signed int %ld
unsigned or signed char: %c
string %s
float %f
double %lf
long double %Lf
short unigned int, unsigned int: %u
long unsigned int: %lu
unsigned hexadecimal %x
unsigned octal %o
Optional specifiers
to edit field width and justify
Example: %10d will print the variable as a decimal integer in a field of 10 columns.
when value is less than 10 columns then by default it gets padded with blanks on left and is right justified.
%-10d for left justifying.
%10.1f decimal point separating field width from precision.
It helps to create tables of values, readable output.
Escape Sequence:
Whats \?
And mention 9 of them:
\ backslash is Escape Operator.
\t : tab.
Screen with 80 columns, has 10 zones of 8 columns each. tab takes control to the beginning of the next zone.
\n newline, \b backspace
\f Form Feed: while printing, takes to top of next page.
' Single quote, \ Backslash, \t tab,
\r Carriage return : Takes to beginning of line in which it is currently placed.
\a Alert : alerts the user by sounding the speaker.
" Double quote.
Delimiters, how to print them:
a blank space, comma, or other character or symbol that indicates the beginning or end of a character string, word, or data item
using \ before them , we can print them.
printf, using another format specifier for a variable:
printf() uses whatever specification we have given to it and it attempts to do the specified conversion.
scanf()
scanf( “format string”, list of addresses of variables);
& :address of operator.
Dont include escape sequences in format string.
Value entered must be separated by blanks, tab(s) or newlines.
sprintf()
It does not send output as printf. works like printf but sprintf() stores value in a char array.
sprintf( str, “%d%c”,i,ch);
sscanf()
Allows us to read characters from string and to convert and store in C variables according to specified formats.
Useful for in memory conversion of characters to values.
same as scanf but first argument is the string from which reading takes place.
Unformatted console I/O functions:
when is it preferred?
Can deal with single char or strings.
Scanf needs an enter key hit for func to digest input.
Sometimes to read a single char the instant its typed wout waiting for enter key hit.