INPUT & OUTPUT STATEMENTS

Input/output statements defines how we can read input from different input devices or write into different output devices. By input operation, we mean user providing values for the program through an external device (like keyboard) and by output operation, we mean writing results of computation to an external device, like monitor, printer etc. An input/output statement is nothing but standard function provided by C and each of them have a list of arguments enclosed in parentheses.

Standard Input/ Standard Output/ Standard Error
When you run a C program, operating system opens 3 files and provides it to the program; they are standard input, standard output and standard error.
  1. Standard input file is used to take inputs to the program provided by user. Standard input device is your keyboard.
  2. Standard output file is used to write the output that your program creates. Standard output device is your monitor.
  3. Standard error file is used to write the errors encounter during program execution. Your monitor serves as the standard error file.
You might think that why keyboard, monitor etc. are referred as file here. This is because the operating system treats these devices as files and manipulate them using standard file operations. For example, when you want to write something onto a monitor, you write them to the file referring to the monitor. Operating system will handle this through the abstraction layer provided by your hardware and write them onto the screen.
C programming language provides many of the built-in functions to read given input and write data on screen, printer or in any file.

scanf() and printf() functions

#include<stdio.h>
#include<conio.h>
int i;
void main() {
scanf("%d",&i);
printf("Enter a value");
getch();
printf( "\nYou entered: %d",i);
}
When you will compile the above code, it will ask you to enter a value. When you will enter the value, it will display the value you have entered.
NOTE : printf() function returns the number of characters printed by it, and scanf() returns the number of characters read by it.
int i = printf("studytonight");
In this program i will get 12 as value, because studytonight has 12 characters.

getchar() & putchar() functions

The getchar() function reads a character from the terminal and returns it as an integer. This function reads only single character at a time. You can use this method in the loop in case you want to read more than one characters. The putchar() function prints the character passed to it on the screen and returns the same character. This function puts only single character at a time. In case you want to display more than one characters, use putchar() method in the loop.
#include <stdio.h>
#include <conio.h>
int c;
void main( ) {
c=getchar();
printf("Enter a character"); putchar(c);
}
getch();
When you will compile the above code,it will ask you to enter a value. When you will enter the value, it will display the value you have entered.

gets() & puts() functions

The gets() function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF (end of file). The puts() function writes the string s and a trailing newline to stdout.
#include<stdio.h>
#include<conio.h>
char str[100];
void main() {
gets( str );
printf("Enter a string"); puts( str );
}
getch();
When you will compile the above code,it will ask you to enter a string. When you will enter the string, it will display the value you have entered.

No comments:

Post a Comment