43
C Programming
141
printf( "\nYou entered: ");
puts( str );
return 0;
}
When the above code is compiled and executed, it waits for you to input some
text. When you enter a text and press enter, then the program proceeds and
reads the complete line till end, and displays it as follows:
$./a.out
Enter a value : this is test
You entered: This is test
The scanf() and printf() Functions
The
int scanf(const char *format, ...)
function reads the input from the
standard input stream stdin
and scans that input according to
the
format
provided.
The
int printf(const char *format, ...)
function writes the output to the
standard output stream
stdout
and produces the output according to the format
provided.
The
format
can be a simple constant string, but you can specify %s, %d, %c,
%f, etc., to print or read strings, integer, character, or float, respectively. There
are many other formatting options available which can be used based on
requirements. Let us now proceed with a simple example to understand the
concepts better:
#include <stdio.h>
int main( )
{
char str[100];
int i;
printf( "Enter a value :");
scanf("%s %d", str, &i);
printf( "\nYou entered: %s %d ", str, i);