Managing Input and Output operations in C | C Input and Output

All input and output operations are carried out through function calls such as printf() and scanf(). These functions are collectively known as the standard I/O functionsFor the compiler, I/O operations are not part of ‘C’. Hence, there is a need for a statement that instructs the compiler, that the standard I/O header file containing standard input and output functions is in usage. This is achieved by means of a preprocessor directive.

#include<stdio.h>

The filename stdio.h stands for standard input/output header file. The instruction #include<stdio.h> tells the compiler ‘to search the file named stdio.h and place its content at this point in program’. The content of the header file become the part of the code when it is compiled.


Reading a Character:

Reading a single character can be done by using the function getchar(). This can also be done with the help of the scanf() function.

The getchar() takes the following form

variable_name=getchar();


getchar() takes no argument. variable_name is a valid variable name that has been declared as char type. When this statement is encountered, the computer waits until a key is pressed and then assigns this character as a value to getchar() function. Since getchar() is used on the right hand side of an assignment statement, the character value of getchar() is in turn assigned to the variable name on the left. 


Example:


char ch;

ch=getchar();


will assign the character ‘A’ to the variable ch when we press the key A on the keyboard.



Writing a Character: 

Like getchar(), there is a function putchar() for writing a character one at a time to the screen.

It takes the form as shown below: 

putchar (variable_name);

where variable_name is a type char variable containing a character. This statement display the character contained in the variable_name at the screen. putchar() takes one argument.


For example,

ans= ‘y’;

putchar(ans);


will display the character y on the screen.

 

The statement putchar(‘\n’);

 

Would cause the cursor on the screen to move to the beginning of the next line.



Formatted Input:

Formatted input refers to an input data that has been arranged in a particular format. For example, consider the following data: 

21  15.6  John

This line contains three pieces of data, arranged in a particular form. Such data has to be read conforming to the format of its appearance. For example, the first part of the data should be read into a variable int, the second into float, and the third part into char. This is possible in C using the scanf() function.


The general form of scanf()

scanf(“control string”, & arg1, & arg2,…….,& argn ) ;

The control string specifies the field format in which the data is to be entered and the argument arg1, arg2,… argn specify the address of locations where the data is stored. Control string and arguments are separated by commas. Control string contains field specifications, which direct the interpretation of input data. It may include:


  • Field (or format) specifications, consisting of the conversion character % a data type character (or type specifier), and an optional number, specifying the field width.
  • Blanks, tabs, or newlines.
  • Blanks, tabs and newlines are ignored. The data type character indicates the type of data is to be assigned to the variable associated with the corresponding argument. The field width specifier is optional.


A scanf() function can input strings containing one or more characters. Following are the specifications:


Syntax

Example

Description

%ws

%5s

To read a string of 4 characters

%wc

%3c

To read 3 characters

%wd

%2d

To read an integer 0f 2 digit



Example:

int main()

{

int  age;

printf(\nHow old are you ?”);

scanf(“%2d”,&age);

printf(“\n I am %2d years old”,age);

return 0;

}


Output: 

How old are you ?  21

I am  21 years old



Formatted Output:

We use the printf() function for generating the formatted outputs or standard outputs in accordance with a format specification. The output data and the format specification string act as the parameters of the function printf().


Syntax:  

printf(“control string”,arg1, arg2…argn);


Control string consists of three types of items:

· Characters that will be printed on the screen as they appear.

· Format specifications that define the output format for display of each item.

·  Escape sequence characters such as \n, \t and \b


The control string indicates how many argument follows and what their types are. The arguments arg1, arg2, …, argn are the variables whose values are formatted and printed according to the specification of the control string. The arguments should match in number, order and type with the format specifications.

Similar to the scanf() usage for integer, printf() function uses %wd to output an integer, where w specifies the minimum field width for the output. If the width specified is more than the number of digits to be printed, the number is right justified by appending the leading spaces as necessary. To left align the number with width, include symbol i.e. “%-wd”.To append 0 instead of spaces to the left side of numbers include 0 with width i.e. “%0wd”

The output of real numbers may be displayed in decimal notation using the following format specification:

%w.p f

The integer w indicates the minimum number of positions that are to be used for the display of the value and the integer p indicates the number of digits to be displayed after the decimal point.

We can also display real numbers in exponential notation by using the specification

%w.p e

Readability of the output can be increased by using many of these format specifiers in a suitable combination. This is accomplished in the following program.


Example:

int main()

{

int no1=150, no2=45;

printf(“\n Number = %d%d”,no1,no2 );

printf(“\n Number = %10d%10d”,no1,no2);

printf(“\n Number = %-10d%-10d”,no1,no2);

printf“\n Number =  %010d%010d”,no1,no2);

return 0;

}


Output:

Number=15045

Number=              150               45       

Number=150            45

Number=00000001500000000045




Post a Comment

0 Comments