Input Output Functions in C Language
Input Output Functions in C Language
Language
Input and Output
In programming input means reading data from the input devices or from a file.
Output means displaying results on the screen.
C provides number of input and output functions. These input and output
functions are predefined in their respective header files. Header file for standard
input and output functions is stdio.h. These are included into program prefixed
with #include statement.
Input and Output functions are classified into TWO categories.
/* Program Example */
#include <stdio.h> //header file section
void main()
{
int a, b, c;
printf("Enter the value of a = ");
scanf("%d",&a) ; //read value a through keyboard
printf("\nEnter the value of b = ");
scanf("%d",&b); //read value b through keyboard
c = a + b;
printf("\na + b = %d ",c); //Print the sum of two value a and b
}//End of Program
by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col
lege(Autonomous) Bengaluru
Formatted Output Function in C
Example: printf()
The printf() is a library function to send formatted output to
the screen.
// Program Example
#include <stdio.h>
Void main()
{
float number1 = 13.5;
double number2 = 12.4;
• getchar()
• putchar()
• getch()
• putch()
• gets()
• puts()
Example:
char c;
c = getchar();
printf(“enter a character”);
c=getchar();
printf(“c = %c ”,c);
}//End of Program
by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col
lege(Autonomous) Bengaluru
putchar()
This function prints one character on the screen at a time which is read
by standard input.
Syntax:
putchar( variable name);
Example:
char ch= ‘c’;
putchar(ch);
by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col
lege(Autonomous) Bengaluru
/* putchar() program example */
#include <stdio.h>
void main()
{
char ch;
printf(“enter a character: ”);
scanf(“%c”,&ch);
putchar(ch);
}//End of Program
by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col
lege(Autonomous) Bengaluru
getch() & getche()
These functions read any alphanumeric character from the standard input
device
• The getche() accepts and displays or echoes the character.
• The getch() accepts but does not display the character.
Syntax:
getche();
getch();
getch();
}//End of Program
by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col
lege(Autonomous) Bengaluru
putch()
This function prints any alphanumeric character taken by the standard input
device
#include <stdio.h>
void main()
{
char ch;
printf(“Press any key to continue”);
ch = getch();
printf(“ you pressed:”);
putch(ch);
}//End of Program
by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col
lege(Autonomous) Bengaluru
gets()
String I/O
• This function is used for accepting any string until enter key is pressed
Syntax:
char str[length of string in number];
gets(str);
puts(str);