Managing Input and Output Operations

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Managing Input and Output operations

The I/O functions are classified into two types:


 Formatted Functions
 Unformatted functions

Input and Output functions

Formatted Functions Unformatted Functions

printf() getch()
scanf() getche()
getchar()
gets()
putch()
putchar()
puts()
Unformatted Functions:
They are used when I/P & O/P is not required in a specific format.
C has 3 types I/O functions.
a) Character I/O
b) String I/O
c) File I/O
a) Character I/O:
1. getchar() This function reads a single character data from the standard input.
(E.g. Keyboard)
Syntax :
variable_name=getchar();

eg:
char c;
c=getchar();

2. putchar() This function prints one character on the screen at a time.


Syntax :
putchar(variable name);
eg
char c=‘C’;
putchar(c);
Example Program
#include <stdio.h>
main()
{
int i;
char ch;
ch = getchar();
putchar(ch);
}
Output:
A
A
3. getch() and getche() : getch() accepts only a single character from keyboard. The
character entered through getch() is not displayed in the screen (monitor).
Syntax
variable_name = getch();

Like getch(), getche() also accepts only single character, but getche() displays the
entered character in the screen.
Syntax
variable_name = getche();
4 putch(): putch displays any alphanumeric characters to the standard output device.
It displays only one character at a time.
Syntax
putch(variable_name);
b) String I/O
1.gets() – This function is used for accepting any string through stdin (keyboard)
until enter key is pressed.
Syntax
gets(variable_name);

2. puts() – This function prints the string or character array.


Syntax
puts(variable_name);
3. cgets()- This function reads string from the console.
Syntax
cgets(char *st);

It requires character pointer as an argument.


4. cputs()- This function displays string on the console.
Syntax
cputs(char *st);

Example Program
void main()
{
char ch[30];
clrscr();
printf(“Enter the String : “);
gets(ch);
puts(“\n Entered String : %s”,ch);
puts(ch);
}
Output:
Enter the String : WELCOME
Entered String : WELCOME

Formatted Input & Output Functions


When I/P & O/P is required in a specified format then the standard library functions
scanf( ) & printf( ) are used.
O/P function printf( )
The printf( ) function is used to print data of different data types on the console in a
specified format.
General Form

printf(“Control String”, var1, var2, …);

Control String may contain


1. Ordinary characters
2. Conversion Specifier Field (or) Format Specifiers
They denoted by %, contains conversion codes like %c,
%d etc.
and the optional modifiers width, flag, precision, size.
Conversion Codes
Data type Conversion Symbol
char %c
int %d
float %f
Unsigned octal %o
Pointer %p

Width Modifier: It specifies the total number of characters used to display the value.
Precision: It specifies the number of characters used after the decimal point.
E.g: printf(“ Number=%7.2\n”,5.4321);
Width=7
Precesion=2
Output: Number = 5.43(3 spaces added in front of 5)
Flag: It is used to specify one or more print modifications.
Flag Meaning
- Left justify the display
+ Display +Ve or –Ve sign of value
Space Display space if there is no sign
0 Pad with leading 0s
E.g: printf(“Number=%07.2\n”,5.4321)
Output: Number=0005.43

Size: Size modifiers used in printf are,


Size modifier Converts To
l Long int
h Short int
L Long double
%ld means long int variable
%hd means short int variable
3. Control Codes
They are also known as Escape Sequences.
E.g:
Control Code Meaning
\n New line
\t Horizontal Tab
\b Back space
Input Function scanf( )
It is used to get data in a specified format. It can accept data of different data types.
Syntax
scanf(“Control String”, var1address, var2address, …);

Format String or Control String is enclosed in quotation marks. It may contain


1. White Space
2. Ordinary characters
3. Conversion Specifier Field
It is denoted by % followed by conversion code and other optional modifiers
E.g: width, size.
Format Specifiers for scanf( )

Data type Conversion Symbol


char %c
int %d
float %f
string %s
E.g Program:

#include <stdio.h>
void main( )
{
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
sum=num1+num2;
printf("Sum: %d",sum);
}
Output
Enter two integers: 12 11
Sum: 23

You might also like