MAP

DATA INPUT AND OUTPUT


As we know that any c program is made up of 1 or more then 1 function. Likewise it use some functions for input output process. The most common function are printf() and scanf().

 

Printf() function :

 Printf() function is use to display something on the console or to display the value of  some variable on the console The general syntax for printf() function is as follows
printf(<”format string”>,<list of variables>);

To print some message on the screen
Printf(“God is great”);
Text Box: More Examples:
printf(“\nIndia is the best”);
printf(“\nVande Matram”);
printf(“\nJay Hind”); Text Box: Output
India is the best
Vande Matram
Jay Hind

This will print message “God is great” on the screen or console.

 

To print the value of some variable on the screen
Integer Variable :

int a=10;
printf(“%d”,a);
Here %d is format string to print some integer value and a is the integer variable whose value will be printed by printf() function. This will print value of a “10” on the screen. You can make this output interactive by writing them
Int a=10;
Printf(“a=%d”,a);
This will print “a=10” on the screen

To print multiple variable’s value one can use printf() function in following way.
Int p=1000,r=10,n=5;
Printf(“amount=%d rate=%d yeat=%d”,p,r,n);
This will print “amount=1000 rate=10 year=5” on the screen

Float Variable :

Float per=70.20;
printf(“Percentage=%f”,per);
Here %f is format string to print some float(real) value and per is the float variable whose value will be printed by printf() function. This will print value of a “Percentage=70.20” on the screen.

Character Variable:
char ans=’Y’;
printf(“Answer=%c”,ans);
Here %c is format string to print single character value and ans is the character variable whose value will be printed by printf() function. This will print value of a “Answer=Y” on the screen.


Suppose we want to print “Amar” on the screen with character variable
Char c1=’A’,c2=’m’,c3=’a’,c4=’r’ ;
Printf(“Name = %c %c %c %c”, c1,c2,c3,c4);

This will print “Name=A m a r” on the screen.

You can use single printf() function to print different data type variable’s value also.
Example
Int rno=10;
Char res=’P’;
Float per=75.70;
Printf(“Rollno=%d Result=%c Percentage=%f”,rno,res,per);

This will print message “Rollno=10 Result=P Percentage=75.70” on the screen.

Scanf() Function :

Scanf() function is use to read data from keyboard and to store that data in the variables.
The general syntax for scanf () function is as follows.

Scanf(“Format String”,&variable);

Here format string is used to define which type of data it is taking as input this format string can be %c for character, %d for integer variable and %f for float variable. Where as variable the name of memory location or name of the variable and & sign is an operator that tells the compiler the address of the variable where we want to store the value. One can take multiple input of variable with single scanf() function but it is recommended that there should be one variable input with one scanf() function.

Scanf(“Format string1,format string2”,&variable1,&variable2);



For Integer Variable :
Int rollno;
Scanf (“%d”,&rollno);
Printf(“Enter rollno=”);
Here in scanf() function %d is a format string for integer variable and &rollno will give the address of variable rollno to store the value at variable rollno location..

For Float Variable :
Float per;
Printf(“Enter Percentage=”);
Scanf(“%f”,&per);
Here in scanf() function %f is a format string for float variable and &per will give the address of variable per to store the value at variable per location.


For Character Variable :
Char ans;
Printf(“Enter answer=”);
Scanf(“%c”,&ans);

 
 

                                                                                   




Here in scanf() function %c is a format string for character variable and &ans will give the address of variable ans to store the value at variable ans location.

 

Single character input – the getchar function :

Single characters can be entered into the computer using the “C” library function getchar. The getchar function is a part of the standard “C”  language I/O library. It returns a single character
from a standard input device (typically a keyboard). The function does not required any arguments through a pair of empty parentheses must follow the word getchar.
In general terms, a reference to the getchar function is written as.
Character variable=getchar();

Where character variable refers to some previously declared character variable.
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.
If an end-of file condition is encountered when reading character with the getchar function, the value of the symbolic constant EOF will automatically be returned. The detection of EOF in this manner offers a convenient way to detect an end of file, whenever and wherever it may occur.  Appropriate corrective action can be taken. The getchar function can also be used to read multicharacter strings, by reading one character at a time  a multiples loop.

Single character output – The putchar function:

Single character can be displayed (i.e. written out of the computer) using the C library function putchar.The putchar function, like getchar is a part of the standard “C” language I/O library. It transmits a single character to a standard output device.  The character being transmitted will normally be represented as a character-type variable.  It must be expressed as an argument to the function, enclosed in parentheses following the word putchar. In general a reference to the putchar function is written as

Putchar (character variable)

Where character variable refers to some previously declared character variable.
A “C” program contains the following statements.:
      char c=’a’;
      putchar(c);

The first statement declares that c is character type variable. The second statement causes the current value of c to be transmitted to the standard output device where it will be displayed.
The putchar function can be used to output a one-dimensional, character type array.
Each character can then be written separately within a loop.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More