Input and Output
Input and Output
In C programming, for input and output, operations are supplied as functions in the
standard library. So, we can take the data through input functions and sends results
Standard Files in C
C language treats all its inputs and outputs as files. A file is a place where information
comes from or can be sent. These files can either be read or written only. These files
are called I/O streams. C language has 3 I/O files (also called streams). Streams are
always open and ready to use. Streams in C are as shown in the table below.
Standard input stdin used to take the input from the device such as Keybo
Standard output stdout used to send output to a device such as monitor or scr
Standard error stderr used to route the error message to the screen
Therefore, the keyboard and screen are referred to as the standard input and output
files. In C, all library functions are covered by the stdio.h header file. For using I/O
functionality, the programmer must include the stdio.h header file into the program.
scanf() in C
scanf is the input function that gets the formatted input from the file stdin that is
the keyboard. As mentioned earlier, C treats the keyboard input as a file. scanf
is a built-in function that comes with all C compilers. Its header file is stdio.h.
The scanf() reads all types of data values given by the user and these values
execution.
You need to pass the address of the variable to the scanf function along with
other arguments so that the read values can be assigned to the correct
destination.
scanf() Syntax
The first argument is the format string (conversion specification) and followed by
a list of variable arguments of pointer types ie., the address of variables that
need to be passed.
scanf() Example
int a;
double b;
scanf("%d\n", &a);
scanf("%lf\n", &b);
scanf() reads the characters from standard input and interprets them according
printf() in C programming
The printf() prints all types of data value to the standard output(typically the
screen). It requires a conversion symbol and variable name to print the data.
printf() Syntax
The first argument is the format string which is composed of ordinary characters
arguments.
printf() Example
printf("welcome");
printf("The result is %d ",a);
Usually the format string is enclosed with the double quotation marks in
both scanf() and printf().
#include<stdio.h>
int main()
{
int a;
printf("Enter the value of a = ");
scanf("%d",&a);
printf("\nThe value of a = %d",a);
return(0);
}O u t p u t
when you compile and execute the above program, you will get the following
output.
getchar() in C Programming
The getchar() reads character type data from input file stdin. It reads one character
at a time till the user pressses the enter key. The getchar() returns a character type.
getchar() - Syntax
variable-name = getchar();
getchar() - Example
char ch;
ch = getchar();
This places the next character into the variable ch. It is a low level function.
putchar() in C Programming
The putchar() writes the character type data to the output file stdout(screen). It is
putchar() - Syntax
putchar( variable-name);
putchar() - Example
char ch = 'C';
putgachar(ch);
int main()
{
char a;
a = getchar();
putchar(a);
return(0);
The most important point is that the both getchar() and putchar() could be
implemented as macros, rather than function. This means that it might not be possible
putchar( function() );
gets() in C Programming
The gets() is used to read string from standard input file stdin and places it into some
buffer which the programmer must provide. The gets() stops reading when an enter
gets - Syntax
puts() in C Programming
The puts() is used to send the string to the output file stdout, until it finds a NULL
end of string marker. The NULL is not written, instead the newline character is written
to the output stdout.
puts - Syntax
puts() returns an integer value, whose value is only guaranteed if there is an error.
int main()
{
char ch[20];
printf("Enter the text: ");
gets(ch);
puts(ch);
return(0);
When you compile and execute the above program, you will get the following output.
Output