Input and Output functions UNIT-I Input/ Output functions and other library functions All the input/output operations are carried out through function calls. There exists several functions that become standard for input and output operations in 'C'. These functions are collectively known as the standard I/O library. 1. #include<stdio.h> - standard input output header file. Each program that uses a standard input/output function must contain this statement. The instruction # include<stdio.h> tells the compiler 'to search for a file named stdio.h and place its contents at this point in the program'. Three types of I/O functions: a) Character I/O functions i. getchar() ii. putchar() b) Formatted I/O functions i. scanf() ii. printf() c) string I/O functions i. gets() ii. puts() getchar(): The function getchar() is used to read a single character from the keyboard. It is an unformatted input function. Form: variablename=getchar(); variablename is a valid 'C' name that has been declared as char type. char ch; ch=getchar(); putchar(): The function putchar() is used to write character to the standard output function. It is an unformatted output function. Form: putchar(variablename); Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 1
scanf(): char ch='a'; putchar(ch) or putchar('a'); Example program to read a character and print that character. #include<stdio.h> main() { char ch; printf("enter a character\n"); ch=getchar(); printf("the character is:"); putchar(ch); } Output: Enter a character b The character is:b This is a formatted input function. General form: scanf("control string", arg1,arg2,...,argn); The "control string" specifies the field format (format specifiers) in which the data is to be entered and The arguments arg1, arg2,, argn specify the address of locations where the data is stored. Control string contains i. Field (or format) specifications, ii. Consisting of the conversion character %, iii. A data type character (or type specifier), iv. And an optional number, specifying the field width. The data type character indicates the type of data that is to be assigned to the variable associated with the corresponding argument. Integer input: %wd '%' indicates that a conversion specification follows. W is an integer number that specifies the field width of the number to be read. Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 2
'd' known as data type character. Examples: int n1, n2; scanf("%d%d",&n1,&n2); scanf("%2d%5d",&n1,&n2); An input may be skipped by specifying * in the place of field width. Int a,b; scanf("%d%*d%d",&a,&b); will assign the data 123 456 789 as follows 123 to a 456 is skipped 789 to b Inputting Real numbers: The field width of real numbers is not to specified and scanf reads real numbers using the simple specification %f for both the notations, namely decimal point notation and exponential notation. scanf("%f%f%f",&x,&y,&z); 475.89 43.21E-1 678 x=475.89 y=4.321 z=678 double: The field specification is %lf Inputting a character using scanf: %c is the specifier. Inputting a character strings: scanf function can input string containing more than one character. Specification: %ws Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 3
Example program: #include<stdio.h> main() { char s[10]; printf("enter a string\n"); scanf("%4s",s); printf("string is:"); printf("%s\n",s); } Output: Enter a string Anuradha String is:anur %s terminates reading at the encounter of a blank space. (%s adds a null character at the end ('\0')) #include<stdio.h> main() { char s[10]; printf("enter a string\n"); scanf("%s",s); printf("string is:"); printf("%s\n",s); } Enter a string Anu Radha String is:anu Some versions of scanf for strings: %[characters] and %[^characters] The specification %[characters] means that only the characters specified within the brackets are permissible in the input string. If any other character encountered, then the string will be terminated. The specification %[^characters] allows the characters specified after the (^) circumflex are not permitted in the input string. Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 4
gets(): The gets() function is used to read a sequence of characters including blank spaces. (where scanf reads only uo to blank space) up to new line('\0'); at the end it adds null character ('\0'). gets(stringname); where string name is of type character array. scanf Format codes code Meaning %c read a single character %d read a decimal integer %e, %f, %g read a floating point value %h read a short integer %i read a decimal, hexadecimal, or octal integer %o read an octal integer %s read a string (character array) %u read an unsigned decimal integer %x read a hexadecimal integer %[..] read a string of word(s) Formatted ouput: 1. printf() General form: scanf("control string", arg1,arg2,...,argn); i. Control string consists of Characters that will be printed on the screen as they appear. Format specifications. Escape sequence characters. It specifies many arguments and their types. ii. The arguments arg1, arg2,, argn are variables whose values are formatted and printed according to the specifications of the control string. %wp type specifier Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 5
where 'w' is an integer number that specifies the total number of columns for the output value. 'p' specifies the number of digits after decimal point (real) or number of characters to be printed from a string. Output of Integer numbers: %wd printf("%6d",9876); 9 8 7 6 printf("%-6d",9876); printf("%06d",9876); 9 8 7 6 0 0 9 8 7 6 Output of Real numbers: 1. Decimal notation %w.pf 2. Exponential notation %w.pe float y=98.7654; printf("%7.4f",y); printf("%7.2f",y); 9 8. 7 6 5 4 9 8. 7 7 printf("%10.2e",y); 9. 8 8 e + 0 0 1 Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 6
printf("%e",y); 9. 8 7 6 5 4 0 e + 0 0 1 printf("%-10.2e",y); 9. 8 8 e + 0 0 1 Printing a single character: %wc The character will be displayed right justified in the field of w columns. For left justification place a minus sign (-) before the integer w. Default value of 'w' is 1. Printing of strings: %w.ps Where 'w' specifies the field width for display. 'p' instructs that only the first p characters of the string are to be displayed. #include<stdio.h> main() { printf("%s\n","new DELHI 110001"); printf("%20s\n","new DELHI 110001"); printf("%20.10s\n","new DELHI 110001"); printf("%.5s\n","new DELHI 110001"); } Output: NEW DELHI 110001 NEW DELHI 110001 NEW DELHI NEW D printf("%s\n","new DELHI 110001"); NEW DELHI 110001 Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 7
printf("%20s\n","new DELHI 110001"); N E W D E L H I 1 1 0 0 0 1 printf("%20.10s\n","new DELHI 110001"); printf("%.5s\n","new DELHI 110001"); puts(): N E W D N E W D E L H I The function is used to print string of values or group of characters. General Form: puts(stringname); where string name is of type character array. Here, there is no need to specify '\n', explicitly, after printing string, the cursor is at the initial position of next line. printf Format codes code Meaning %c print a single character %d print a decimal integer %e print a floating point value in exponential form %f print a floating point value %g print a floating point value either e-type or f- type depending on value %h print a short integer %i print a signed decimal integer %o print an octal integer, without leading zero %s print a string (character array) %u print an unsigned decimal integer %x print a hexadecimal integer, without leading 0x Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 8
Library functions: 1. <stdio.h> - Standard I/O library functions Function Data type Task returned printf( ) int Send data items to the standard output device scanf( ) int Enter data items from the standard input device gets() char* Enter a string from standard input device puts(s) int Send string s to the standard output device getchar(void) int Enter a single character from the standard input device putchar( c) int Send a single character to the standard output device 2. <math.h> - Mathematical functions Function Data type Task returned pow(d1,d2) double Return d1 raised to the d2 power sqrt(d) double Return the square root of d ceil(d) double Return a value rounded up to the next higher integer floor(d) double Return a value rounded down to the next lower integer fabs(d) double Return the absolute value of d 3. <ctype.h> - Character testing and conversion functions Function Data type Task returned isalpha( c) int Determine if argument is alphabetic. Return nonzero value if true; 0 otherwise. isascii( c) int Determine if argument is ASCII character. Return nonzero value if true; 0 otherwise. isdigit( c) int Determine if argument is decimal digit. Return nonzero value if true; 0 otherwise islower( c) int Determine if argument is lowercase. Return nonzero value if true; 0 otherwise isupper( c) int Determine if argument is uppercase. Return Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 9
nonzero value if true; 0 otherwise toascii( c) int Convert value of argument to ASCII. tolower( c) int Convert letter to lowercase. toupper( c) int Convert letter to uppercase. 4. <stdib.h> Function Data type Task returned abs(i) int Return the absolute value of i exit(u) void Close all files and buffers, and terminate the program (Value of the 'u' is assigned by the function to indicate the status. free(p) void Free a block of allocated memory whose beginning is indicated by p. malloc(u) void* Allocate u bytes of memory. Return a pointer to the beginning of the allocated space. rand(void) int Return a random positive integer. 5. <string.h> - String manipulation functions Function Data type Task returned strcmp(s1,s2) int Compare two strings lexicographically. Return a negative value if s1<s2; 0 if s1 and s2 are identical; and a positive value if s1>s2. strcpy(s1,s2) char* copy string s2 to string s1. strlen(s) int Return the number of characters in string s. strcat(s1,s2) char* Concatenation s2 is appended at the end of s1. Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 10