EnggPedia - The Engineering Encyclopedia

Wed03292017

Last update06:04:15 AM

Font Size

Profile

Menu Style

Cpanel
Back Computer Programming Languages Input & Output statements in C language

Input & Output statements in C language

Input Function:

Input functions or streams are used are to read different type of data (Such as numeric, string or character) from the keyboard. You can use more than one types of input functions in the program.

Scanf ( ):

Scanf () is the input function of standard library. In C language, scanf ( ) function can be used without giving reference to standard library but  you need to include the stdio.h header file. Scanf ( ) function is used to read data from the keyboard. The syntax of scanf ( ) statement is:

Scanf (“List of format specifier”, & list of variables);

The first part of the general syntax of scanf () function is the list of format specifiers which depends on the data types of variables to be read. This part should be enclosed in double quotes. Moreover, the list of format specifier can be used with prithf () function. The % is used to tell or instruct compiler that next coming character is a format specifier.

The second part of the general syntax of scanf () function is the list of variables. Each variable name is start with a symbol & (called ampersand) is known as an address operator which refers to the address of that variable which is going to be read.

Example 1: Write a scanf() statement which read two variables a and b of int data type.

scanf(“%d%d”, &a, &b) ;

Example 2: Write a scanf() statement which read variable a of float data type and variable b of double data type.

Scanf(“%f  %lf”, &a, &b) ;

Note: You can use spaces between format specifiers but good practice is to write in sequence and together as in example 1.

Output Function:

Output functions or streams are used are to display different type of data (Such as numeric, string or character) and results on the screen. You can use more than one types of output functions in the program.

Printf ( ):

Printf( ) is the output function of the standard library, In C Language, printf( ) function can be used without giving reference of any standard library, but you need to include the studio.h header file. The syntax of the printf( ) statement is given below:

Printf(“List of format specifier and escape sequences”, list of variables) ;

Like scanf() function, the general syntax of printf() function has two parts. First part of printf() syntax refer to the list of format specifiers which help to display data of variables according to their data types and list of escape sequences to print formatted data. In C / C++ Language, the character \ (Backslash) is considered as an escape character because it causes an escape from the normal interpretation of string. The use of \ instruct compiler that next coming character has special meaning. The list of escapre sequences are given below.

Escape Seq

Purpose

Example

Output of example

\n

New line (can be use for vertical spacing)

Printf(“Sheeba\nAltaf”) ;

Sheeba

Altaf

\t

Tab (More than one space and can be used for horizontal spacing)

Printf(“Sheeba\tAltaf”) ;

Sheeba          Altaf

\b

Backspace

Printf(“Sheeba\b\bAltaf”) ;

SheeAltaf

\r

Carriage Ruturn ( Used in printing text)

Printf(“Sheeba\rAltaf”) ;

Altaf

\’

Single Quote

Printf(“Don\’t”) ;

Don’t

\”

Doule Quote

Printf(“\”Sheeba\””) ;

“Sheeba”

\v

Vertical tab (used in printing text like \n)

Printf(“Sheeba\vAltaf”) ;

Sheeba

Altaf

In printf() statement, you can set the width of text which is to be displayed. Such as

If you have an integer variable X with value 5, that is X= 5, then

Printf(“%6d”, X) ;

It means the width of displayed data is 6 or you can say six places will be reserved for displaying 5. Similarly, let you have a real value 9.8 in a variable X. Then

Printf( “%3.2f”, X) ;

It means the width of displayed data is 3 and precision is 2.

Related Articles to this Article
While loop is a conditional loop which execute set of instructions repeatedly until specific condit
The most common advantages of C language are
In C/ C++ language, loop is an iterative constructs which execute set of instructions according to
Input functions or streams are used are to read different type of data (Such as numeric, string or
Read more similar Articles