0% found this document useful (0 votes)
8 views

1-Input Output functions

Uploaded by

akr 009u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

1-Input Output functions

Uploaded by

akr 009u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Input Output functions In C programming

What is input and output?


All the data entered from the keyboard must be stored into the memory location /variables of the program. This
activity is known as Input.

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.

Classification of I/O functions into two categories

1. Console I/O function


2. File I/O function

Console Input-Output functions

Console simply means screen and keyboard.


Console input/output operations can be done in two ways:
 Formatted
 Unformatted
Formatted Input /Output:
Formatted input/output statements enable the user to specify the type of the data and the way in which it
should be read or written

• Formatted input/output functions are scanf() and printfO.


• These two functions are present in the header file <stdio.h>
printf( ) function

printf( ) is the standard library function that is used for precise output formatting.

Syntax of printf( ) function:

int printf( format-control-string, arguments-list );

Explanation- Parameter of printf function is:

First parameter: Format control string

Return Value: int - printf() returns the no. of character to be printed.

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.

We can perform following formatting options with the help of printf( ):

 Right and left justification.


 Rounding floating-point values.
 Inserting literal characters.
 Displaying all types of data with appropriate size and precisions.

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.

Example: Use of precision for integer, floating-point, and strings

int main ()

int num1 = 123;


double num2 = 123.45678;

char s[] = "Hello World!!!";

puts("Use of precision for integer");

printf("\t%.4d\n\t%.8d\n\n", num1, num1);

puts("Use of precision for floating-point");

printf("\t%.2f\n\t%.2e\n\n", num2, num2);

puts("Use of precision for string");

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

Syntax of scanf( ) function:


int scanf( format-control-string, arguments-list );

Format –Control- String:

 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:

 Control string must be enclosed within the double quotes.


 For every I/O variable, there must be one character group
 Each character group should begin with % and followed by a conversion character.
 Multiple numbers of character groups can be allowed within the control string. Such case they may
be contiguous or separated by blank space or commas.
 Address_list contains address (memory location) of I/O variables proceeded by ampersand (&).
 All the address list variables must be separated by commas.
 The Values for address List should map in number, type and order for variables.
 There must be a comma to separate control string and the address lisl.

Eg : Scanf("%d", & a);

If the user enters 10 from the keyboard is stored in variable a;

Scanf(%d%F, &a, &b);

Reading character using scan set


We can only read certain characters from the input stream using scan set, set of characters enclosed in the
square bracket [ ] that is preceded by a percent % sign.

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.

Inverted scan set is opposite of scan set.

Example of scan set in scanf( )

int main ()
{
char s[ 9 ];

printf("Enter string: ");


scanf("%9[selz]", s);

printf("The input string is: \"%s\"\n", s);


return 0;
}
Output

Example of Inverted scan set in scanf( )

int main ()
{
char s[ 10 ];

printf("Enter string: ");


scanf("%9[^selz]", s);

printf("The input string is(Inverted scan set): \"%s\"\n", s);

return 0;
}
Output
C_Quiz-1.docx

C_Quiz-2.docx

C_Quiz-1_Answer.docx

C_Quiz-2_Answer.docx

You might also like