1-Input Output functions
1-Input Output functions
After the calculations, result stored in memory location /variables must be transferred onto the output device for
user viewing. This activity is known as Output.
printf( ) is the standard library function that is used for precise output formatting.
Describes the output format which consists of conversion/format specifiers, precisions, flags, field widths and
literal characters.
Each conversion/format specifier starts with % sign and ends with a conversion specifier.
A format control string can have special meaning if it follow special syntax rule
Type 1:
Note: [ ] indicate optional term.
Width:
With the help of field width, we can change the position of data being displayed on the screen.
It is +ve integer .It defines minimum field width. It specifies the total number of characters used to
display.
If the field width is greater than data being displayed, then the data is right justified, put first number of
blank space then prints data.
int main ()
{
printf("%4d\n", 1);
printf("%4d\n", 12);
printf("%4d\n", 123);
printf("%4d\n", 1234);
printf("%4d\n", 123456);
Output-
1 2 3 4 5 6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 6
Precision:
Precision has a different meaning when used with different data types
It indicates the number of characters used after the decimal point.
Case 1: if data type of data is string: precision is +ve integer which indicate how many character in the
string will be printed. If precision is larger than length of data whole string will be printed.
Case 2: If data type of data is fractional (float, double, long double): It indicates how many digits will
be printed after decimal. If precision is less than number of digits after decimal point in data then
rounded value will be printed.
Case 3: If data type of data is integer: It will that number of zero before the data. (Total no. of
zero=precision-length of data)
Case 4: if data type of data is character: In case of char data type char has no meaning.
int main ()
printf("\t%.8s\n\t%.12s\n", s, s);
return 0;
Output
Type 2:
Syntax: \x
Where x must be one of the following
a: to produce a sound(bell),ascii value=7
b: to shift one space back(back space) ,ascii value=8
t: to shift zero to seven space forward (horizontal tab) ,ascii value=9
n: to print data in new line(new line) ,ascii value=10
v: use in printer for vertical tab(vertical tab) ,ascii value=11
f: use in printer to come first position(from feed) ,ascii value=12
r: shift the cursor to first position(carriage return) ,ascii value=13
Formatted input function:
scanf ( ) :
To read values into the variables of the program accepted from the keyboard.
This is used to accept numeric, character and string type of data
It is a sequence of one or more character groups. Each character is a combination of % symbol and one
of the conversion characters.
The Control string specifies the type of the values and number of values, which are to be supplied to the
variables.
Argument-List: Specifies the list of variables into which data must be stored.
Return Value: int - scanf() return the no. of arguments reads successfully.
Rules:
The characters from the input stream that matched the character in scan set are stored in the array, otherwise,
stops inputting characters when a character that is not stored in scan set is encountered.
On the other hand, if we want to omit certain characters from input stream we should place a care (^) sign
before the scan characters. This is called an inverted scan set.
int main ()
{
char s[ 9 ];
int main ()
{
char s[ 10 ];
return 0;
}
Output
C_Quiz-1.docx
C_Quiz-2.docx
C_Quiz-1_Answer.docx
C_Quiz-2_Answer.docx