Input and output Functions
When your C program begins to execute, three input/ output devices are opened automatically . C has no built- in for input or output. A library of functions is supplied to perform these operations. The I/O library functions are listed the "header" file <stdio.h>. There are numerous library functions available in C for input-Output (I/O). They are broadly classified into three categories.
- Console I/O functions: Functions that receive input from the keyboard and display it on the monitor.
- Disk I/O functions: Functions to make an input-output on a floppy or hard disk.
- Port I/O functions: Functions to perform operation on various ports.
Streams - All input and output is performed with streams. A “stream” is a sequence of characters organized into lines. Each line consists of zero or more characters and ends with the “newline” character. ANSI C standards specify that the system must support lines that are at least 254 characters in length (including the newline character).
Types of Streams in C
Standard input stream is called “stdin” and is normally connected to the keyboard. Standard output stream is called “stdout” and is normally connected to the screen. Console I/O functions are broadly classified into two groups.
I/O functions - I/O functions are gropes into two categories:
1. Unformatted I/O functions 2. Formatted I/O functions
The formatted I/O functions allow programmers to specify the type of data and the way in which it should be read in or written out. On the other hand. Unformatted I/O functions do not specify the type of data and the way is should be read or written. Amongst the above specified I/O functions scanf() and printf() are formatted I/O functions. The following table list the formatted and unformatted I/O functions.
Most of the input/output functions are provided as a part of the C standard library to which we can get access through the header file called stdio. h. The stdio.h is an abbreviation for standard input-output header file. We can include this header file in our program using pre-processor directive, #include<stdio.h>.
It tells complier to place contents of header file in the program. After compilation, the contents of the header file become a part of the source code.
Formatted Output -
1. printf Function : C provides the printf function to display the data on the monitor. This function can be used to display any combination of numerical values, single characters and strings. The following figure shows the general from of printf statement.
Printf() – This function provides for formatted output the screen.
The syntax is : printf (“message and format Specifier”, var 1, 2,…);
The “format Specifier” includes a listing of the data types of the variables to be output and, optionally, some text (message) and control characters (Escape sequences)
Example:
Code
float a ; int b ; scanf (“%f %d”, &a, &b); printf (“You entered %f and d\n”, a, b);
The following shows the output of a printf() statement:
Code
int sum = 20,count = 3; printf(“%d divided by %d equals % f/n”, sum, count, (float) sum/count);
Output: 20 divided by 3 equals 6,666667
Important points
• Control string must be enclosed within the double quotes.
• For every data item to be displayed, there must be a placeholder corresponding to its data type.
• Multiple placeholders are allowed in the control string. In such a case they may be contagious or separated by blank spaces or commas.
• The date items (the variables whose values to be displayed) must be included in the print list and they must be separated by commas.
• Print list is not enclosed within double quotes.
• The comma must be used to separate the format string and the print list.
Formatting Integer Output – We have seen that how integer numbers can be displayed on the monitor using %d placeholder. In case of integer number, the placeholder can accept modifiers to specify the minimum field width and left justification. By default, all output is right justified. We can force the information to be left justified by putting a minus sign directly after the %.
For example ,
As shown in the above examples, if the number width in digits is less than the minimum field width the empty place are filled with spaces. However, if the number width is greater than the minimum field width, the number is printed in full, overriding the minimum field width specification. This is illustrated in the following list of examples. It is also possible to pad the empty places with zeroes. If we want to pad with zeroes, we have to place a zero before the minimum field width specifier.
Demonstrates formatted printf statement for integer numbers
Code
#include<stdio.h> void main() { int a = 12345; printf (“with no modifier : %d\n”, a); printf (“MFW=8 with right justified : %8d\n”, a);/*MFN: Minimum Field Width*/ printf (“MFW=8 with left justified : %8d\n”, a); printf (“MFW=8 with left justified : %8d\n”, a); printf (“Minimum Field Width 3 : %3d\n”, a); printf (“Padding With Zeros : %08d\n”, a); }
Output
With no modifier : 12345
MFW = 8 with right justified : 12345
MFW = 8 with left justified : 12345
MFW = 8 with left justified : -12345
Minimum Field Width 3 : 12345
Padding With Zeros : 000 12345
Formatting Floating Point Output – In case of floating numbers placeholder can accept modifiers that specify the minimum field width, precision and left justification. To add a modifier we have to place a decimal point followed by the precision after the field width specifier. For e and f formats, the precision modifiers determines the number of decimal place to be displayed. For example, % 8.4f will display a number at least 8 characters wide including decimal point with four decimal places.
* When minimum field width is specified without negative sign, display is right justified.
* When only minimum field width is specified and it is less than the actual string length, the minimum field width is overridden and complete string is displayed.
* We can insert negative sign before minimum field width to get left justified display.
Enhancing the Readability of Output- We commonly use two specifiers to enhance the readability of the output.
\t: It is a ‘tab’ character. It inserts four/eight blank spaces. So we can use tab character to provide four/eight blank spaces between the two fields
\n: It is newline character. It display the message or variable values following it on the next line. The backslash symbol(\) is known as a escape character. Since the newline. The backslash symbol (\) is known as escape character. Since the newline character and the tab character begin with the backslash they are called escape sequences.
There are few more escape sequences. These are listed in following table.
The program below shows how escape sequences may be used in strings.
Code
void main() { printf(“John said,\”Hello.\”\n”); Escape sequences in string*/ printf(“\t This symbol \\ is a backslash.\n”); }
Output:
John said, "Hello."
This symbol\ is a backslash
The example below shows the effect of field width on integer arguments.
/* Printing integers right-justified*/
Code
void main() { printf(“%4d\n”,1); printf(“%4d\n”,12); printf(“%4d\n”,123); printf(“%4d\n”,1234); printf(“%4d\n\n”,12345); printf(“%4d\n”,-1); printf(“%4d\n”,-12); printf(“%4d\n”,-123); printf(“%4d\n”,-1234); printf(“%4d\n”,-12345); }
The precision value appears after the dot (.) in the conversion specification, and it takes on different meaning for different specifiers.
For Specifier d, it indicates the minimum number of digits (the default value is 1).
For Specifier, E and f, it refers to the number of digits after the decimal point.
For Specifier g and G, it refers to the maximum number of significant digits.
For Specifier s, it means the maximum number of characters to be displayed.
Formatting Input –
2. scanf ( ) : function is a general function to read data from the standard input device. The format of the function is:
This function provides for formatted input from the keyboard. The syntax is:
scanf (“format Specifier”, &varl, &var2….);
The “format Specifier” is a listing of the data types of the variables to be input and the & in front of each variable name tells the system WHERE to store the value that is input. It provides the address for the variable.
The format Specifier is matched with the expression & inch, causing the scanf() function to interpret the characters entered as a floating-point number, and store the value into the address of inch.
The scanf() function returns an integer value that is the number of successful conversions done, or the system defined end-if-value.
The list below shows the conversion specifiers used in scanf().
3. getchar ( ) : This function provides for getting exactly one character from the keyboard.
Code
char ch; ch = getchar ( );
4. putchar (char) : This function provides for printing exactly one character to the screen.
Code
char ch; ch = getchar (); /* input a character from kbd*/ putchar (ch); /* display it on the screen */
5. gets(arrayname): It receives a string from the keyword in this function blank space also allow it takes string from kbd. & terminated after enter key is hit so we can enter any string including blank space but this not happened in scanf() function so we use gets() function.
Code
gets (arrayname); // Array must be the character array
6. puts() : It used to outputting a string on the screen opposite to gets in print statement we can display only continuous string but not including blank pace so we use puts() function for display a string which include blank space.
Code
puts(arrayname); // Array must be the character array puts(“ ”);
Ex.
Code
void main(){ char a [20]; gets (a); puts (a); }
Explanation: In the above program two numbers are entered. Using conditional operator are tested. If the first number is greater than second multiplication of numbers is calculated division is performed.
Post a Comment