Computer Concept & Programming In C - Unit II - 1




Q.1   Give a brief introduction of standard I/O in C.
Ans.  Standard I/O in C: -
A file contains related characters and numbers. Because all characters and numbers are represented in bits on computers, the C language treats a file as a series of bytes (8 bits make up 1 byte). A series of bytes is also called a stream. In fact the C language treats all file streams equally, Some of the file streams may come from a disk or tape drive from a terminal or even from a printer.
  Additionally there are three file streams.
1. stdin – The standard input for reading.
2. stdout – The standard output for writing.
3. stderr – The standard error for writing error massage.
(i) Standard input (stdin) file stream links to your keyboard
(ii) The standard output (stdout) and standard error (stderr) file streams point to your terminal screen. Also many operating systems allow you to redirect these files streams.
* When one executes the printf () function, one infact sends the output to the default file streams, stdout which points to your screen. 
Note: - The C language provides many functions to manipulate file reading and writing (I/O). The header file stdio.h contains the declarations for those functions. Therefore always include the header file stdio.h in your C program before doing anything with the file I/O.

Q.2 How can you get the input from the user in C.
Ans. Getting the input from the User: -
Typing from keyboard is still the most common and standard way to input information into computers. The C language has several functions to direct the computer to read the input from the user (typically through the keyboard). The two most fundamental input functions are the getc( ) and getchar( ) functions.
Using the getc ( ) function: -
The getc( ) function reads the next character from a file stream and returns the character as integer.
The syntax for the getc( ) function is 
# include<stdio.h> 
    int getc(FILE * stream);
here, FILE * stream declares a file stream (that is a variable). The function returns the numeric value of the character read. If an end of file (EOF) or an error occurs the function returns EOF. [At this moment, do not worry about the FILE structure.]
EOF: -
Defined in the header file <stdio.h>, EOF is a constant. EOF stands for (end of file) usually the value of EOF is –1. But keep using EOF instead of –1, if you need an end of file indicator, in case a compiler uses a different value.
The example below reads a character typed in by the user from the keyboard and then displays the character on the screen.
# include <stdio.h>
# include <conio.h>
void main ( )
  {
     int ch;
     clrscr ( ); /* for clear screen*/
     printf (“\n Enter a character\”);
     ch = getc (stdin);
     printf (“\n you entered:%c”, ch);
     getch ( );
 }
The output of the above example will be 
enter a character 
 I
You entered: I
Using the getchar ( ) function: -       
The C language provides another function, getchar ( ) to perform a similar operation to getc ( ). More precisely, the getchar ( ) function is equivalent to getc (stdin).
The syntax for the getchar ( ) function is.
  # include <stdio.h>
  int getchar (void);
Here, void indicates that no argument is needed for calling the function. The function returns the numeric value of the charcter read. If an end of file or error occurs, the function returns EOF.
Reading in a character by calling getchar ( ): -
The following program demonstrates how to use the getchar ( ) function to read the input from the user.
# include <stdio.h>
# include <conio.h> 
void main ( )            
{
int ch1, ch2;
clrscr ( );
printf (“\ n please type in two characters together\n”);
ch1 = getc (stdin);
ch2 = getchar ( );
printf (“\n The first character you just entered is: %c\n”, ch1);
printf (“\n The second character you just entered is: %c\n”, ch2);
getch ( );
}
The output of the following program is;
Please type in two characters together
Hi
The first character you just entered is: H
The second character you just entered is: i
Note: - Nothing is passed to the getchar ( ) function. This is because the getchar ( ) has its default file stream – stdin. You can replace the getchar ( ) function with getc (stdin), because getc (stdin) is equivalent to getchar ( ).