Showing posts with label 3. DATA INPUT AND OUTPUT. Show all posts
Showing posts with label 3. DATA INPUT AND OUTPUT. Show all posts

Friday, December 11, 2009

3.5 THE gets AND puts FUNCTIONS

C contains a number of other library functions that permit some form of data transfer into or out of the computer.

The gets and puts functions offer simple alternatives to the use of scanf and printf for reading and displaying strings,as illustrated in the following example.

#include

main()     /*read and write a line or text*/

{

char line[80];
gets(line);
puts(line);

}

This program utilizes gets and puts,rather than scanf and printf,to transfer the line of text into and out of the computer.

3.4 WRITING OUTPUT DATA - THE printf FUNCTION

Output data can be written from the computer onto a standard output device using the library function printf.It is similar to the input function scanf,except that its purpose is to display data rather than to enter it into the computer.

In general terms, the printf function is written as

printf(control string,arg1,arg2,arg3,.....,argn)

where control string refers to a string containing certain required formatting information,and arg1,arg2,arg3,....,argn are arguments that represent the the individual output data items.

Here is a simple program that makes use of the printf function



#include

main() /*print several floating point numbers*/
{

float i=2.0,j=3.0;
printf("%f %f",i,j);

}

The output of the program will be as follows:

2.000000 3.000000

Thursday, December 10, 2009

3.3 ENTERING INPUT DATA- THE scanf FUNCTION

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.

In general terms, the scanf funstion is written as

scanf (control string,arg1,arg2,arg3,....,argn)

where control string refers to a string containing certain required formatting information,and arg1,arg2,arg3,....,argn are arguments that represent the the individual input data items(actually the arguments represent pointers that indicate the addresses of the data items within the computer's memory).

Some commonly used conversion characters for data input are

CONVERSION CHARACTER----------------------------------------------MEANING
c--------------------------------------------------------------data item is a single character
d--------------------------------------------------------------data item is a decimal character
e--------------------------------------------------------------data item is a floating point value
f--------------------------------------------------------------data item is a floating point value
g--------------------------------------------------------------data item is a floating point value
h--------------------------------------------------------------data item is a short integer
i---------------------------------------------data item is a decimal,hexadecimal or octal integer
o--------------------------------------------------------------data item is an octal integer
u------------------------------------------------------data item is an unsigned decimal integer
x-----------------------------------------------------------data item is a hexadecimal integer


Consider a C program that contains the following statements

#include

main()
{
int i;
float x;
char c;
.............
scanf("%d %f %c",&i,&x,&c);
or if
scanf("%3d %5f %c",&i,&x,&c);
.............
}

If the data items are entered as 10 256.875 T

when the program is executed ,then 10 will be assigned to i,256.875 will be assigned to x,T will be assigned to c

or if

field with is specified as in lin6
10 will be assigned to i,256.8 will be assigned to x and 7 will be assigned to c.The remaining two input characters 5 and T will be ignored.

%3d--->the first three digits will be assigned to i(1,0 and blank space)
%5f-->the next 5 digits will be assigned to x (2,5,6,.,and 8)
%c-->the next digit will be assigned to c(7)





3.2 SINGLE CHARACTER OUTPUT- THE putchar FUNCTION

Single characters can be displayed (i.e. written out of the computer) using the c library function putchar.The function is complementary to the character input function getchar.

In general a function reference would be written as

putchar(character variable)

where character variable refers to some previously mentioned character variable.

Below is a simple example which takes a character as input from the user and displays the equivalent uppercase character.

/*lowercase to uppercase conversion*/
#include
#include

main()

{

char ch;
printf("Enter any character value");
ch=getchar();
printf("The equivalent uppercase character is ");
putchar(toupper(ch));

}

3.1 SINGLE CHARACTER INPUT- THE getchar FUNCTION

Single characters can be entered into the computer using the C library function getchar.

In general terms, a function reference would be written as

character variable =getchar();

A C program contains the following statements.

char c;
..........
c=getchar();

The first statement declares that c is a character type variable.The second statement causes a single character to be entered from the standard input device and then assigned to c.