Input :
In any programming language input means to feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement.
Output :
In any programming language output means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output required data.
Here we will discuss only one input function and one putput function just to understand the meaning of input and output.
The basic input function:-
* printf( ) function
This is one of the most frequently used functions in C for output. It can be used for writing chars, ints, floats, as well as string. The general form of printf () is as follows:
printf ("……. % ", );
Example:
#include
main()
{
int dec = 5;
char str[] = "abc";
char ch = 's';
float pi = 3.14;
printf("%d %s %f %c\n", dec, str, pi, ch);
}
The output of the above would be:
5 abc 3.140000 c
Here %d is being used to print an integer, %s is being usedto print a string, %f is being used to print a float and %c is being used to print a character.
* Other input function:-
Are
Input
Data type Function Purpose
Char
String getch()
getche()
Fgethar()
getchar()
gets() Gets a character from the keyboard as soon as it typed, no need to hit Enter
Similar to getch(),except echoes the character on the screen.
Gets the character & echoes it on the screen, requires Enter Key to be hit
A macro, works similar to fgetchar.
Accept a string until Enter Key hit.
The basic output function:-
* scanf( ) function
This is the function which can be used to to read an input from the command line.
Try following program to understand scanf() function. It can be used for reading chars, ints, floats, as well as string. The general form of scanf () is as follows:
scanf ("% ",& );
Example:
#include
main()
{
int x;
int args;
printf("Enter an integer: ");
if (( args = scanf("%d", &x)) == 0) {
printf("Error: not an integer\n");
} else {
printf("Read in %d\n", x);
}
}
Here %d is being used to read an integer value and we are passing &x to store the vale read input. Here &indicates the address of variavle x.
This program will prompt you to enter a value. Whatever value you will enter at command prompt that will be output at the screen using printf() function. If you eneter a non-integer value then it will display an error message.
Enter an integer: 20
Read in 20
* other output function:-
Are
Output
Purpose Function Data type
Prints the character on the screen.
Prints the character on the screen.
A macro, works similar to putch( ) & function.
Outputs a string to the screen. Putch( )
Fputchar( )
Putchar( )
Puts( )
Char
String