Class 8: I/O Flashcards
Character Based I/O
Int getchar() - input
Int putchar() - output
Formatted I/O
Zero or more characters with a requirement to specify the format of the input or output
Int scanf()
Int printf()
Int getchar(void)
Int putchar(Int)
These function use the integer (4 byte) values which correpond to a given 1 byte ascii character. The integer must be cast before being assigned to char variables or must be printed with %c format code to be output as a char with printf()
ASCII
A character encoding standard that uses 8 bit binary code to represent characters. The ascii table organizes characters and their corresponding binary, decimal, and hexadecimal representations
IMPORTANT: scanf ignores ______ but not ________ white space characters when it reads NUMERIC values from input
Leading, following
Int int1. Int2;
Scanf(“%2i”, &int1);
Scanf(“%2, &int2);
INputs: 2345
Int1 has value 23
Int 2 has value 45
Printf(“%4.3f\n”, x);
Field With: 4
Precision: 3
Char s1[64]
Scanf(“%[^\n]”, s1);
Reads the whole line (until new line is encountered)
Hello World
Scanf(“%[^\t]%[^\t]”, s1, s2)
Field1<tab>field2</tab>
What is the return type of printf()
It returns the NUMBER of characters printed
If you use scanf() to read in the value of a character array, should you use &
No because the name of the array is a const pointer to the first character of the string
If you use scanf() to read and integer (%d), how does it work
Scanf will skip white space characters (space, tab, new line) until it encounters first non whitespace character. Because it expects to read integer, it expects to find digits character or + or -. Scanf() continues reading until it encounters a non digit. It then conclude that is has reached the end of the integer. Scanf() places the non digit back into the input. This means that the next time program goes to read the input, it starts at the previously rejected non digit characters.
Using scanf to read string %s
If you use %s, any other character other than whitespace is acceptable, so scanf skips whitespace to the first non-whitespace character and saves up non-whitespace characters until hitting whitespace again.
Scanf using %c
If you use scanf() to read a character, all input characters are fair game. If the next input character is a space or new line, a space or new line is assigned to the indicated variable; whitespace is not skipped
What is the return value of scanf()
The function returns the number of items that it successfully read. If it reads not items, scanf() returns 0. It returns EOF when it detects a condition known as “end of file”
Int getchar()
Getchar() returns an integer value which correpond to a give 1-byte ascii character, the integer must be CAST before assigning it to a char variable or must b printed with %c format code to be output as a char with printf()
Int putchar(int)
/the int value returned can be ignored/