Saturday 17 September 2016

C input and output functions

The c language provides two types of input and output functions namely formatted and unformatted  input and output functions. Here in this page you can find what are formatted input and output functions and how  they can be used in our programs


Input data can be entered into the computer from a standard input device by means of the C library function scanf. This function can be used to enter any combination of numerical values, single characters and strings. The function returns the number of data items that have been entered successfully.

In general terms the scanf function is written as

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

Where control string refers to a string containing certain required formatting information,and arg1,arg2,… .,argn are arguments that specific the address locations where the data is stored.

The prototype of the control string can be given as:

[=%[*][width][modifies]type=]

Here * is an optional argument that suppresses assignment of the input field,i.e.,it indicates that data should be read from the stream but ignored.
Width:it is an optional argument that specifies the maximum number of characters to be read.
Modifier: it is an optional argument that can be h,1, or the field L for the data pointed by the corresponding additional arguments.Modifier h is used for short int or unsigned short int, I is used for long int, unsigned long int, or double values.Finally ,L is used for long double data values.
Type:This specifies the type of data that has to be read. It indicates how this data is expected to be read from the user.

The scanf function ignores any blank spaces,tabs,and newlines entered by the user.The function simply returns the number of input fields successfully scanned and stored.

Example
:#include
Main()
{
Char item [20];
Int partno;
Float cost;
. . .
Scanf(“%s%d%f”, item,&partno,&cost);
. . .

}


Formatted output:Output data can be written from the computer onto a standard output device using the library function printf. This function can be used to output of any combination of numerical values, single characters and strings. Simply, the printf function moves data from the computers memory to the standard output device (monitor).

In general terms, the printf function is written as

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

Where control string refers to a string that contains formatting information, and arg1, arg2,…, argn are arguments that represent the individual output data items.

The prototype of the control string can be as given as follows.

%[flags][width][length] specifier

Each control string must begin with a % sing. The %character specifies how the next variable in the list of variables has be printed. After % sign follows the following parameters.
Flags: Specify output justifications such as decimal point , numerical sign, trailing zeros, or octal, decimal or hexadecimal prefixes.
 F-                    Left – justify with in the data given field width
+                      Displays the data with its numeric sign (either+or -)
#                      Used to provide additional specifiers like o,x,0,0x,or ox for octal and hexadecimal values, 
                         respectively, for values different than zero
0                           The number is left –padded with zeros (0)instead of spaces.

Width :Specifies the minimum number of characters to print after being padded with zeros or blank spaces, that is it specifies the minimum number of positions in the output.
Precision: Specifies the maximum number of characters to print

Specifier: It is used to define the type and the interpretation of the value of the corresponding argument.

Examples:printf (“/n Result:%d%c%f”, 12,’a’2.3);
Result : 12a 2.3
printf (“/n Result:%d%c%f”, 12,’a’2.3);
Result : 12a 2.3
printf (“/n Result :%5/t%x/t%#x”,234,234,234);
Result :234 EA ox EA

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top