Tuesday, January 29, 2019

Input/output Function.


Each set of programming language has some sort of input output functions. These functions are standard I/O function and put them in library. A set of rules or routines defined for input and output of data is called I/O functions.

Functions printf() and scanf() are the most commonly used to display out and  take input respectively
1.     printf ( ) :-This function is used to display text, constant or value of variable on screen in specified format. It offers %d, %f, % c, %s etc.

The required header for the printf function
 is: #include <stdio.h>
 
Syntax:
printf ("format string", argument list);

Example:
#include<stdio.h>//This is needed to run printf( ) function.
int main( )
{
printf("C Programming");//displays the content inside quotation
return0;
}
Output :
C Programming

2.  scanf ( ):-The function is used to get input from the user (Keyboard) during execution of program and stored in a variable of specified form is called scanf( ) function. Data can be int, float, char, string etc.



Syntax: scanf(“format string”,& variable name);
 
When we used scanf,first declaed what the variable type was "%d" for int, "%f" for float and second part we hve to use &  to point to the variable instead of getting its value 

Example:
#include<stdio.h>
int main( )
{
int c;
printf("Enter a number\n");
scanf("%d",&c);
printf("Number=%d",c);
return0;
}


Output
Enter a number
4
Number=4
 
3.     getch ( ) :-This function is used to get a single character input from the user during execution of program. It also force to wait the output to stay on screen until any key pressed from keyboard.

Syntax:
  •      For value input: variable name= getch( );
  •      For screen holding at the end of program: getch( );

Example:
#include<stdio.h>
#include<conio.h>
void main ( )
{
clrscr( ); // clear screen
char m;
printf("Enter character:");
m=getch ();
printf("The character you Entered=%c",m );
getch( );
}

Output:
Enter character:
The character you Entered=a


4.     getche ( ) :-It is also used to get a single character from the keyboard during the program execution. When this function is executed, the character entered by the user is displayed on the screen.

Syntax: variable name=getche( );

Example
#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
char p;
printf("enter any character:");
p=getche();
printf("the character u entered=%c",p );
getch();
}

Output
enter any character:x
the character u entered=x

5.     gets function :-This special function is used to input string values from the user during execution of the program. As the user press enter key when typing after the string. A null character (\0) is automatically entered at the end of the string.

Syntax:
gets(variable name);

Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
char str[];
clrscr( );
printf("Enter a string:");
gets(str);
getch( );
}
Output:
Enter a string:hello

6.     puts function :-The function is used to display the string value on the screen.

Syntax: puts(parameter/value/variable);

Example:
#include<stdio.h>
#include<conio.h>
void main ( )
{
char name[];
printf("enter your name:");
gets(name);
printf("your name is:");
puts(name);
getch( );
}


Output:
Enter your name:somebody
your name is:somebody

7.     getchar ( ) :- The function "getchar( )" accept a single character from the standard input device and assigns it to the variable "a".
For example:
char a;
a= getchar( );

Example:
#include <stdio.h>
#include<conio.>
int main ( )
{
char a;
clrscr;
printf(“Enter a character:\n”);
a=getchar( );
printf(“Given character is: %c\n”,a);
getch( );
return 0;
}

Output:
Enter a character:
c
Given character is:c

8.     putchar ( ) :- This function displays one character at a time in monitor.

Syntax:
putchar(variable_name);

9.     getc ( )  :-This function is used to read a character from file.

1.  putc() : -This function is used to print a character on a file.

No comments:

Post a Comment