Saturday 17 May 2014

printf() and scanf()


The functions printf() and scanf() enable you to communicate with a program. They are called input/output functions, or I/O functions for short.  Historically, these functions, like all other functions in the C library, were not part of the definition of C.The C90 and C99 standards describe standard versions of these functions, and we'll follow that standard.
 
Although printf() is an output function and scanf() is an input function, both work much the same, each using a control string and a list of arguments. We will show you how these work, first with printf() and then with scanf().


The printf() Function

The instructions you give printf() when you ask it to print a variable depend on the variable type. For example,
                         printf("The value of  mango is %d\n", num);
                         printf("The first letter of English alphabet is %c\n", var);

we have used the %d notation which is used for printing an integer and the %c notation when printing a character. These notations are called Conversion specifications or Format specifiers because they specify how the data is to be converted into displayable form. We'll list the conversion specifications that the ANSI C standard provides for printf() and then show how to use the more common ones table presents the conversion specifiers and the type of output they cause to be printed.


Control-string is the phrase enclosed in double quotes.To know more about Format Specifier goto here...
Let see one Example:
#include <stdio.h>
main()
{
   int age = 25;
   printf ("The age of student = %d", age);

}
The above program does the following:
(1) Declares a variable age of type int.
(2) Initializes age to 25, an integer value.
(3) Specifies %d, a format specifier indicating that an integer is to be displayed by printf() and the data is stored in the variable called age.

The printf() function also has a return value; it returns the number of characters it printed. If there is an output error, printf() returns a negative value. 
Ex:
                a=printf ("The age of student = %d", age); 
then a =23

The scanf() Function

The C library contains several input functions, and scanf() is the most general of them, because it can read a variety of formats. Of course, input from the keyboard is text because the keys generate text characters: letters, digits, and punctuation. When you want to enter, say, the integer 2004, you type the characters 2 0 0 and 4. If you want to store that as a numerical value rather than as a string, your program has to convert the string character-by-character to a numerical value; that is what scanf() does! It converts string input into various forms: integers, floating-point numbers, characters, and C strings. It is the inverse of printf(), which converts integers, floating-point numbers, characters, and C strings to text that is to be displayed onscreen.

Like printf(), scanf() uses a control string followed by a list of arguments.For Example,
#include <stdio.h>
main()
{
   int a,sub1, sub2, sub3, sub4;
   printf ("Enter the marks for four subjects:");
   a=scanf ("%d %d %d %d",&sub1, &sub2, &sub3, &sub4);
   printf("The no. of item reads %d",a);
   printf ("The marks of four subjects are: %d %d %d %d",sub1, sub2, sub3, sub4);
 }
The scanf() function returns the number of items that it successfully reads.
For input data 45 56 76 90, it prints  : The no. of item reads 4 and The marks of four subjects are:45 56 76 90

0 comments :

Post a Comment