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
New line
 Backspace
f Form feed
Single quote
\ Backslash
Null
Horizontal tab
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. : It is used for displaying a tab.
2. : It is used for displaying a new line.
3. : 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
II-70 Programming Concepts
6. What is the difference between getch() and getche()?
Ans: These functions are used to read the character from the standard input device. The getch()
function gets the character from the console with no echo. Whereas getche() function gets the char-
acter from the console with echo.
7. Which are the character-oriented functions?
Ans:
1. putchar();
2. getchar();
8. Which header file is used for using i/o library functions?
Ans: #include <stdio.h> header file is supporting to any i/o library function. Hence, this
header file is needed.
9. What is the use of & in the scanf() function?
Ans: The operator & is used to obtain an address of a variable. It is associated with the argument in
the scanf() function. It is to be preceded to a variable.
10. Give the syntax of getchar().
Ans:
Variable = getchar();
Example:
x=getchar();
Note: Value entered through the keyboard is stored in the variable x.
11. Give the syntax of putchar().
Ans:
putchar(variable);
Example:
x=getchar();
putchar(x);
Note: Value entered through the keyboard is stored in the variable x. The stored value in x is displayed
on the screen with the help of putchar().
12. Write a program to read any one character or digit integer through keyboard by using 
getchar() and display it by using putchar().
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
M04_ITL-ESL4791_02_SE_C04.indd 70 12/22/2012 5:00:39 PM
Input and Output in C II-71
printf("Enter any alphanumeric: ");
x=getchar();
/* read char or number through keyboard */
putchar(x);
/* print on the screen */
getche();
}
OUTPUT:
Enter any alphanumeric: k
k
13. How cgets() is different from gets()?
Ans:
gets(): This function is used for accepting any string through stdin (keyboard) until enter key is pressed.
cgets():  This function reads string from the console. The syntax is as follows:
char *cgets(char *st);
It requires character pointer as an argument. The string begins from st[2].
14. How will you execute a DOS command through C program?
Ans: The system() function is helpful in executing different DOS commands. It returns 0 on suc-
cess and –1 on failure.
The syntax is as follows:
Syntax:
system ("COMMAND");
The command should be enclosed within double quotation marks.
15. What is the use of the exit() function?
Ans: This function terminates the program. It is defined in process.h header file.
16. What is a stream?
Ans: Stream is a sequence of characters, e.g. “ABCDE”, “as 9” etc.
17. What are the tips to design the output?
Ans:
1. Give space between numbers.
2. Provide suitable and problem-related variable names and headings.
3. Provide user prompt, so that the user can understand what to do.
4. Provide a gap between two lines, so that the text should be readable.
5. Alert the user about what to do and what not to do.
6. Use formatted inputs and outputs for precisely inputting the data and outputting results.
7. It is recommended to use escape sequence characters such as ,  and .
M04_ITL-ESL4791_02_SE_C04.indd 71 12/22/2012 5:00:39 PM
II-72 Programming Concepts
Multiple-choice Questions
1. Which of the following is not character constant?
(a) ‘a’ (b) ‘’
(c) ‘ ’ (d) ‘”’
2. Which of the following is character constant?
(a) ‘k (b) ’
(c) ‘/’ (d) ‘’
3. Which of the following is a valid string constant?
(a) “pearson,” “ ”Education” (b) “pearson” , “education”
(c) “pearson” ”Education” (d) “person””
4. Which of the following is not a string constant?
(a) “Pearson” (b) “**&&^”
(c) “pearson,” “”Education” (d) “pearson” ”Education”
5. What will be the output of the following program?
void main()
{
printf(" %d%d%d%d",'A','B','C','D');
}
(a) 65666768 (b) ABCD
(c) 91929394 (d) None of the above
6. What will be the values ofaandbafter execution of the following program?
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
a=65*66;
b='A' *'B';
clrscr();
printf("a=%d b=%d",a,b);
}
(a) a = 4290 b = 4290 (b) a = 4290 b = AB
(c) a = 4290 b = 0 (d) None of the above
7. What function is appropriate for accepting a string?
(a) gets() (b) getch()
(c) getche() (d) scanf()
8. What is the ASCII range for 0 to 9 digits?
(a) 48 to 57 (b) 65 to 90
(c) 97 to 122 (d) None of the above
M04_ITL-ESL4791_02_SE_C04.indd 72 12/22/2012 5:00:40 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset