Kamis, 21 November 2013

Formatted input/output function in Turbo C Language

It is very easy to use unformatted input/output functions as discussed. We can read and display only characters not numbers using unformatted functions. Using formatted input/output functions, we can read integers, floating point number and characters. The formatted input function is scanf() and formatted output functions is printf().

Scanf()

Syntax
int scanf(“format string”, &var1, &var2, &var3,……..&varn);

This function scanf() is used to read any type of data such as int, char, float, double and even string from the keyboard. It accepts the following two parameters:
format string consists of one or more format specifires starts with % symbol.
&var1, &var2, &var3, …., &varn represents the list of variables. Here, symbol ‘&’ refers to the address of the variable. In the memory locations identified by these addresses, the respective values are stored.
Return Type int: The function returns an integer value. This integer value is the number of item that have been read successfully using the keyboard. But, we rarely use this return value in our programs.
The various format specifiers along with the associated meaning are shown in the following table:

Data Type
Format Specifier
Meaning
int
%d
%o
%x
%i
%u
%h
Read a decimal integer value
Read an octal value
Read a hexadecimal value
Read a decimal, octal or hexadecimal value
Read an unsigned integer value
Read a short integer value
float
%e
%f
%g
Read a floating point number
Read a floating point number
Read a floating point number
char
%c
%s
Read a character
Read a string (series of character)
double
%f
Read a long floating point number or double
long int
%ld
Read a long integer value

The following table shows the way the user has to enter the values from the keyboard:

Input statement
Entering value
Input way
scanf(“%d%d%d”,&a,&b,&c);
3 integer value
10 20 30
Where, a=10      b=20       c=30
scanf(“%d,%d,%d”,&a,&b,&c);
3 integer value
10,20,30
Where, a=10      b=20       c=30
scanf(“%f”,&a);
1 floating point value
787.35
Where, a=787.3.5
scanf(“%c”,&a);
1 character value
P
Where, a=p
scanf(“%f,%d,%c”,&a,&b,&c);
1 floating point value
1 integer value
1 character value
787.35 10 P
Where, a=787.35, b=10, c=P
scanf(“%d-%d-%d”,&a,&b,&c);
3 integer values separated by ‘-‘ (hyphen) sign
10-20-30
Where, a=10, b=20, c=30
scanf(“%d%d%d”,&a,&b,&c);
3 integer values separated by ‘/’ (slash) symbol
10/20/30
Where, a=10, b=20, c=30

Note: Do not use any escape sequences such as \t, \n etc. in scanf().

For example: Consider the following statement:
scanf(“%d\n”,&a);
In the above statement, remove ‘\n’, otherwise it may give wrong output.
Now, let us see, some more ways of data reading using scanf() function are given below:
Input statement
Entering value
Input way
scanf(“%2d%2d%2d”,&a,&b,&c);
3 integer values
1 2 3
Where, a=1  b=2  c=3
11 22 33
Where, a=11  b=22  c=33
102030
Where, a=10  b=20  c=30
102 030 4
Where, a=10  b=2  c=3
The remaining two digits 0 and 4 are ignored or can be read by a subsequent scanf()
scanf(“%12s”,&str);
1 string
.Programming
Where, str=.Programming
scanf(“%[^\n]s”,str);
Input the string till the end of line
Wlecome! To the dotprogramming
Where, str= Wlecome! To the dotprogramming
Note:
To read a string with space gets() function will used in place of scanf().
The “scanf(“%[^\n]s”,str);” function read and stored all the character untill user will not press enter key.

Formatted Output Function

Tidak ada komentar:

Posting Komentar