Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

KASULA UMAMAHESWARA RAO II B.

Com Semester - III


INPUT-OUTPUT STATEMENTS

In 'C' language , the input and output functions are used to receive data from the input
device or for sending data to the output device. These functions are standard built-in functions
which are available in “stdio.h” and “conio.h” header files.
The input-output (I/O) functions are classified into 2 categories as shown below:

I/O Functions

Formatted Unformatted

getc( ) putc( )
scanf( ) getch( ) putchar( )
printf( ) getche( ) puts( )
gets( )

FORMATTED I/O FUNCTIONS

The functions that accept numbers, characters and strings as arguments to read and
display using format characters (like %d, %f etc.) are called formatted input-output functions.
Using these functions the values can be formatted (arranged) according to the user requirements.
The ‘scanf( )’ and ‘printf( )’ functions are used as formatted I/O functions.

Scanf( ) Function:
It is a formatted input function which is used to accept data from the user (keyboard) at
run time. The given data is transferred to the memory and is stored in the specified variables.
Syntax:

scanf(format-string , &var1, &var2, …… ) ;

In the above syntax, the „format-string‟ refers to a string constant that contains format
characters according to the type and number of variables. Other characters are not allowed.
Var1, var2, …… are the variables to hold the given values. All the variables except string
variables must be preceded by the address (&) operator.
The values that are supplied through keyboard must be separated by either space(s),
tab(s), or newline(s).
Example: Let a and b are integer type, c is float type, x and y are character type variables. Then
this can be used as follows:

1. scanf(“%d”, &a);
2. scanf(“%d %f”,&a, &c);

Visvodaya Govt. Degree College, Venkatagiri


KASULA UMAMAHESWARA RAO II B.Com Semester - III
3. scanf(“%d %f %c”, &a, &c, &y);

Printf( ) Function:
The printf( ) function is a formatted output function that is used to display the given
information on the standard output device. It is used to print numbers, characters and strings or
combination of the above along with appropriate message.
The syntax of this function can be any one of the following:

Syntax-1: printf(string) ;
Syntax-2: printf(string, values-list) ;

In the above syntax, the „values-list‟ can be variables, constants, or expressions separated
by commas.
The „string‟ is a string constant that contains the following:
i. Any character in the „C‟ character set.
ii. Format characters that begin with „%‟ symbol
iii. Escape sequence characters that begin with „\‟ symbol

Examples:
1. printf(“Hello. What is your name?”);
2. printf(“%d”, 70*45);
3. printf(“Sum of 2 numbers = %d”, sum);
4. printf(“Total = %d \n Average = %f”, total, avg);

Visvodaya Govt. Degree College, Venkatagiri

You might also like