Chp 11 Console Input/Output Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

As a result of Dennis Ritchie wanting C to remain compact:

A

He deliberately omitted everything related with Input/Output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Well, then how do we get the program to communicate with us?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Types of I/O

and explain them:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is called console:

A

The screen and keyboard together are called console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Types of Console I/O functions:

and whats their difference:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

function printf()

what it contains and how it works..

A

printf( ‘Format string’, list of variables );

The format string can contain :

  1. Char to print as it is.
  2. Conversion specifications, start with %.
  3. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Format specifications:

A

%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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Optional specifiers

to edit field width and justify

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Escape Sequence:
Whats \?

And mention 9 of them:

A

\ 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Delimiters, how to print them:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

printf, using another format specifier for a variable:

A

printf() uses whatever specification we have given to it and it attempts to do the specified conversion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

scanf()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

sprintf()

A

It does not send output as printf. works like printf but sprintf() stores value in a char array.

sprintf( str, “%d%c”,i,ch);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

sscanf()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Unformatted console I/O functions:

when is it preferred?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

getch()

A

Return most recently typed character.

17
Q

getche()

A

Return most recently typed character.

e in getche means it echoes( displays) the char u type, on screen.

18
Q

getchar()

A

Return most recently typed character.
it echoes( displays) the char u type, on screen.
But needs a enter key hit.

19
Q

getchar() and fgetchar()

A

former is MACRO

and latter is a function.

20
Q

putch(), putchar(), fputchar():

A

They can print only one char at a time.

they work exactly same.

21
Q

gets()

why use it?

A

Used cuz sacnf can’t receive multi-word strings.
as blank, enter, tab will tell scanf that string has been entered.
gets() terminated only when enter key is hit, so space and tabs are acceptable.
gets() gets only one string from keyborad.

gets() get a newlne (\n) terminated string from keyboard and replaces \n with \0.

gets( base address of string);

22
Q

puts()

A

It outputs only one string to the screen.