C has many input output functions in order to read data from input devices and display the results on the screen. Input and output functions are classified into 2 tyopes.

1. Formatted Functions : Input - Scanaf , Output - Printaf
2. Un Foramatted Functions : Input - Getch(),getche(),getchar() , Output - putch(),puts ().


Formatted Functions :

These functions read and write all types of data values. They require a conversion symbol to indents the data type using these functions the O/P can be presented in an aligned manner.


a) scanf ()

function is used to read values using key board. It is used for runtime assignment of variables. The general form of scanf( ) is
scanf("format String "" , list_of_addresses_of_Variables );

The format string contains - Conversion specifications that begin with % sign
Example:
Scan f(" %d %f %c ", &a &b, &c)
"&" is called the "address" operator. In scanf( ) the "&" operator indicates the memory location of the variable. So that the Value read would be placed at that location.


printf( ) :

function is used to Print/display values of variables using monitor: The general form of printf() is
printf("control String", list_of_ Variables );

Characters that are simply printed as they are
Conversion specifications that begin with a % sign
Escape sequences that begin with a "\" sign.

Example:
 
Program main () 
{ 
int avg = 346;
float per = 69.2; 
printf("" Average = %d \n percentage = %f", avg, per); 
}

printf( ) examines the format string from left to right and prints all the characters until it encounter a "%" or "\" on the screen. When it finds % (Conversion Specifier) it picks up the first value. when it finds "\" (escape sequence) it takes appropriate action (\n-new line). This process continues till the end of format string is reached.

Example:
 
Program ()
main ( )
{ 
float per; printf("Enter values for avg & per");
scanf("" %d %f", & avg, & per);
printf("" Average = %d \n Percentage = %f", avg. per);
}

Unformatted functions character I/O functions

getchar ( )

function is used to read one character at a time from the key board

Syntax ch = getchar ( ); where ch is a char Var.main ()
{
char ch;
printf("Enter a char"); 
ch = getchar ( );
printf("ch =%c", ch);
}		

putchar ( )
function is used to display one character at a time on the monitor.
Syntax: putchar (ch);
Ex char ch = "M" putchar (ch);
The Computer display the value char of variable "ch" i.e M on the Screen.

getch ( )
function is used to read a char from a key board and does not expect the “enter” key press.
Syntax: ch = getch ( );
When this function is executed ,computer waits for a key to be pressed from the dey board. As soon as a key is pressed, the control is transferred to the nextline of the program and the value is assigned to the char variable. It is noted that the char pressed will not be display on the screen.

getche ( )
function is used to read a char from the key board without expecting the enter key to be pressed. The char read will be displayed on the monitor.
Syntax: ch = getche ( );


Strng I/O functions

gets ( ) function is used to read a string of characters including white spaces. Note that wite spaces in a strng cannot be read using scanf( ) with %s format specifier.
Syntax: gets (S);
where "S" is a char string variable
Ex: char S[ 20 ]; gets (S);

When this function is executed the computer waits for the string to be entered

News Walls


Copyrights © 2016. All Rights Reserved to Learn Computers Home, India. //Site Designed.