Input/Output Functions

scanf

                The usually used input statement is scanf () function.
Syntax of scanf function is
                  scanf (“format string”, argument list);
                The format string must be a text enclosed in double quotes. It contains the information for interpreting the entire data for connecting it into internal representation in memory.
Example: integer (%d) , float (%f) , character (%c) or string (%s).
                The argument list contains a list of variables each preceded by the address list and separated by comma. The number of argument is not fixed; however corresponding to each argument there should be a format specifier. Inside the format string the number of argument should tally with the number of format specifier.
               Example: if i is an integer and j is a floating point number, to input these two numbers we may use
scanf (“%d%f”, &i, &j);
Program:
scanf

getchar

               getchar function will accept a character from the console or from a file, displays immediately while typing and we need to press Enter key for proceeding.
Syntax of getchar() is
                                        int getchar(void);
                 It returns the unsigned char that they read.If end-of-file or an error is encountered getchar() functions return EOF.
For example:
char a;
a= getchar();
                   The function "getchar()" reads a single character from the standard input device, the keyboard being assumed because that is the standard input device, and assigns it to the variable "a".
The following program illustrates the use of getchar function.
getchar function

gets

                 It is used to scan a  line of text from a standard input device. The gets() function will be terminated by a newline character. The newline character won't be included as part of the string. The string may include white space characters.
Syntax :      char *gets(char *s);
                gets() function is declared in the header file stdio.h. It takes a single argument. The argument must be a data item representing a string. On successful completion, gets() shall return a pointer to string s.
               The usage of gets() is illustrated in the following program.
gets function

printf

               The usually used output statement is printf (). It is one of the library functions.
Syntax : printf (“format string”, argument list);
              Format string may be a collection of escape sequence or/and conversion specification or/and string constant. The format string directs the printf function to display the entire text enclosed within the double quotes without any change.
Escape sequence:
                 Escape sequence is a pair of character. The first letter is a slash followed by a character. Escape sequence help us to represent within the format string invisible and non-printed character although there are physically two characters in any escape sequence. It actually represents only one. The various escape sequences are
  Escape sequence     Meaning  
  \n     New line  
  \t     Tab  
  \b     Back space  
  \a     Bell  
  \o     Null character  
  \?     To print question mark  
  \\     To print slash  
  \'     To print single quote  
  \"     To print double quote  
Conversion specification:
                Conversion specification is also a pair of character. it is preceded by  % and followed by a quote which may be a character. The Conversion specification inscribes the printf() function that it could print some value at that location in the text. The Conversion characters supported by C are
  Conversion character     Meaning  
  %d     Data item is displayed as a signed decimal integer.  
  %i     Data item is displayed as a single decimal integer.  
  %f     Data item is displayed as a floating-point value without an exponent.  
  %c     Data item is displayed as a single character.  
  %e     Data item is displayed as a floating-point value with an exponent.  
  %g     Data item is displayed as a floating-point value using either e-type or f-type conversion depending on value.  
  %o     Data item is displayed as an octal integer, without a leading zero.  
  %s     Data item is displayed as string.  
  %u     Data item is displayed as an unsigned decimal integer.  
  %x     Data item is displayed as a hexadecimal integer, without a leading 0x.  
The following program illustrates the use of puts function.
printf

putchar

putchar function displays a single character on the screen.
prototype:     int  putchar(int c);
The following program illustrates the use of putchar function.
putchar

puts

                           It is used to display a string on a standard output device. The puts() function automatically inserts a newline character at the end of each string it displays, so each subsequent string displayed with puts() is on its own line. puts() returns non-negative on success, or EOF on failure.
The following program illustrates the use of puts function.
puts function

No comments: