i2tutorials

Formatted Input and Output Functions

Formatted Input and Output Functions:

 

C provides standard functions scanf() and printf(), to perform formatted inputs and outputs. These functions accept a format specification string and a variable list as the parameters. The format specification string is a character string that specifies the data type of each variable to be input or output and the size or width of the I/O.

 

scanf()

 

The scanf() function is used for inputs formatted from standard inputs and provides numerous conversion options for the printf() function.

 

Syntax:

 

scanf(format_specifiers, &data1, &data2,……); // & is address operator

 

The scanf() function reads and converts the characters from the standard input according to the format specification string and stores the input in the memory slots represented by the other arguments.

 

Example:

 

scanf(“%d %c”,&data1,&data2);

 

In the case of string data names, the data name is not prefixed with the &.

 

printf()

 

The printf() function is used for output formatted as the standard output according to a format specification. The format specification string and the output data are the parameters of the printf() function.

 

Syntax:

 

printf(format_specifiers, data1, data2,…..... );

 

Examples:

 

printf(“%d %c”, data1, data2);

 

The character specified after % is referred to as a conversion character because it allows a data type to be converted to another type and printed.

 

Formatted Input and Output

The format specification string can contain text as well.

 

printf(“Number: %d\n”, data1);
printf(“%d\n%c”, data1, data2);

 

Example program:

 

#include <stdio.h> 
void main()
{
int a;
printf(“Enter a value: ”); 
scanf(“%d”,&a); 
printf(“Entered Value= %d”,a);
}

 

 

 

Exit mobile version