When we talking about the Input, it means value taken in the program from outside world.So in C language when we say input taken from outside means value entered on the keyboard will transfer to our program.
The same concept for output, when we say output it means our program is going to display some result on some screen.
In C language programming everything is a file whether it is some text file or peripheral like screen, keyboard etc. In C language, Pointers are defined in the standard library for console device.
See the below table for the pointers to Console input, output and error device.
Standard File | File Pointer | Console device | ||
---|---|---|---|---|
Standard input | stdin | Keyboard | ||
Standard output | stdout | Screen | ||
Standard error | stderr | Screen | ||
int getchar(void);
This function picks next available character from stdin pointer(keyboard) and return an integer.
int putchar(int);
This functiontakes an integer as an input which is to be print and give that value to stdout pointer to print at the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<stdio.h> int main( ) { int ch_var; printf( "Enter a value: \n"); ch_var = getchar( ); printf( "Your entered value is: "); putchar( ch_var ); return 0; } |
1 2 3 4 5 6 7 8 |
Output 1: Enter a value :a Your entered value is: a Output 2: (run again this program for different input) Enter a value :98 Your entered value is: 9 (because only single character is acceptable) |
Note: You can use these function in the loop for multi-character input.
char* gets(char *string);
This function takes input into buffer string from console pointer stdin until a newline or EOF not met.
int puts(char *string);
This function takes a string(array of character terminates with NULL) as an input which is to be print and pass to stdout pointer to print at the screen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<stdio.h> int main( ) { char string[100]; printf( "Enter a value: \n"); gets(string); printf( "Your entered string is: "); puts( string); return 0; } |
1 2 3 4 |
Output : Enter a value :Hello Reader Your entered string is: Hello Reader |
int scanf(const char *format,…);
This function reads input from stdin buffer according to format specifier described in its first argument.it returns “total count of input values”.
Input data Type | Data Format Specifier | |||
---|---|---|---|---|
character | %c | |||
integer | %d,%i,%u,%lu | |||
string | %s | |||
Decimal or Real values | %f,%lf,%Lf | |||
Example:
1 2 3 4 5 6 7 8 9 10 11 |
/* let i_var1,i_var2 is integer variable which will store input data*/ /*f_var2 is float variable for real value store*/ /*1. To get single integer Input from keyboard*/ scanf("%d",&i_var1); /*2. To get two integer Input from keyboard*/ scanf("%d%d",&i_var1,&i_var2); /*3. To get one integer and 1 float Input from keyboard*/ scanf("%d%f",&i_var1,&f_var2); |
int printf(const char *format, …);
The printf() writes output to standard output stream stdout in the required format specifier.
1 2 3 4 5 6 7 8 9 10 11 |
/* let i_var1,i_var2 is integer variable which stores output data*/ /*f_var2 is float variable for real value store*/ /*1. To print single integer output on screen*/ printf("%d",i_var1); /*2. To print two integer output on screen*/ printf("%d\n%d",i_var1,i_var2); /* \n for new line*/ /*3. To print one integer and 1 float output on screen*/ printf("%d\n%f",i_var1,f_var2); |
Write a program for two integer number Addition and integers values shall input from the keyboard:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include<stdio.h> #include<limits.h> int main( ) { unsigned long int ui_var1; unsigned long int ui_var2; unsigned long int ui_result; printf("You can Input value in between %lu to %lu \n",(unsigned long int)0, (unsigned long int)ULONG_MAX); /*typcasted to avoid warning*/ printf( "Enter first Integer input: "); scanf("%lu",&ui_var1); printf( "\nEnter Second Integer input: "); scanf("%lu",&ui_var2); ui_result=ui_var1+ui_var1; printf( "\nYour addition is : %lu",ui_result); return 0; } |
1 2 3 4 5 6 |
Output: You can Input value in between 0 to 18446744073709551615 Enter first Integer input: 2526 Enter Second Integer input: 3537 Your addition is : 6063 |