O'Reilly logo

Express Learning - Computer Fundamentals and Programming by ITL ESL, Ashok Kamthane

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

1. What are the formatted and unformatted functions?
Ans: The formatted input and output functions supported by C are scanf() and printf()
respectively. The scanf() reads the data into a variable from the keyboard and printf() prints/
writes the formatted data on the monitor screen. The letter f in the printf and scanf stands for
formatted. As per requirement, input and output data provided can be arranged in meticulous formatted.
It is essential for a programmer to provide proper conversion symbol/control string in the input and out-
put functions to identify the data type. The control string in the input and output functions create formatted
results. The return values are equal to the number of variables/arguments specified in the function and
it must match with the fields of format specified in the control string of input/output functions.
The syntax of formatted input function scanf() is as follows:
scanf(control_string, argument1, argument2,---------, argument n);
Example: scanf("%d",&x);
Where %d is the control string that specifies the field format in which the data is to be entered. The
other part is the variable x and a sign & (ampersand) must precede to x. The &x is the address location
of the variable x.
Similarly, the syntax of formatted output function printf() is as follows:
printf(control_string, argument1, argument2,---------, argument n);
Example: printf("%d %f %c",x,y,z);
Here the control string is %d %f %c (type of values) and arguments are x y z are the identifiers
whose data is to be printed.
Unformattedfunctions:The unformatted input/output functions work only with character data type.
They do not require format conversion symbol for the formatting of data types, because they work only
with character data type. There is no need to convert data.
In case values of other data types are passed to these functions, they are treated as character data.
A few examples of unformatted functions supported by C are as follows:
getche(), getch(), gets(), getchar(), puts() etc.
getch() accepts a character from a keyboard and can be stored in a variable.
Input and Output in C
4
M04_ITL-ESL4791_02_SE_C04.indd 68 12/22/2012 5:00:39 PM
Input and Output in C II-69
getche() is similar to getch(). It also accepts a character from a keyboard and can be stored in
a variable. However, unlike getch(), getche() displays the character on the screen.
2. What is the difference between character I/O and string I/O?
Ans: Character  I/O: These functions work with single character at a time, e.g., getc(),
putc(), etc.
String  I/O: These functions work with set of characters (i.e., strings) at a time, e.g., gets(),
puts(), etc.
3. What is the escape sequence? List and indicate the functions of escape sequences.
Ans: The printf() and scanf() statements follow the combination of characters called escape
sequences. In order to come out, computers from routine sequence escape sequences are used. These
are nothing but special characters starting with ‘\’. It is a combination of back slash(\) followed by a
letter or combination of digits. Escape sequence in ‘C’ is viewed as single character and, therefore, it is
a legitimate character constant. Following table gives escape sequence and their representations.
Escape Sequence Meaning
\n New line
\b Backspace
\f Form feed
\’ Single quote
\\ Backslash
\0 Null
\t Horizontal tab
\r Carriage return
\a Alert
\” Double quote
\v Vertical tab
\? Question mark
\ooo Octal notation
\X hh Hexadecimal notation
4. List any three escape sequences with their uses.
Ans:
1. \t: It is used for displaying a tab.
2. \n: It is used for displaying a new line.
3. \b: It is used for a backspace.
5. What is the difference between puts() and putch()?
Ans:
puts(): This function prints the string or character array.
putch(): This function prints any alphanumeric character (single character) taken by the standard
input device.
M04_ITL-ESL4791_02_SE_C04.indd 69 12/22/2012 5:00:39 PM

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required