0% found this document useful (0 votes)
20 views

Input Output Functions in C Language

Uploaded by

22csel34
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Input Output Functions in C Language

Uploaded by

22csel34
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Input/Output Functions in C

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.

by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col


lege(Autonomous) Bengaluru
Formatted Input/Output
Functions in C
Properties
• Input data or output results are formatted as per requirement.
• Formatted function improves the readability of the input and output.
• Formatted functions can be used to read and write data of all data type(char, int, float, double).
• Formatted input and output functions require format specifiers(%c, %d, %f, %lf) to identify the type of data.

Formatted Input Function in C


scanf()

Formatted Output Function in C


printf()

by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col


lege(Autonomous) Bengaluru
Formatted Input Function in C
Example: scanf()
• scanf function reads all types of data value from input devices (or) from a file.
• The address operator '&' is to indicate the memory location of the variable.
• This memory location is used to store the data which is read through keyboard.

/* 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;

printf("number1 = %f\n", number1);


printf("number2 = %lf", number2);
} //End of Program

by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col


lege(Autonomous) Bengaluru
Unformatted Functions in C
C Language has three types of Unformatted I/O Functions
i. Character I/O
ii. String I/O
iii. File I/O

by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col


lege(Autonomous) Bengaluru
Unformatted Input/Output functions
in C
Examples:

• getchar()
• putchar()
• getch()
• putch()
• gets()
• puts()

by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col


lege(Autonomous) Bengaluru
getchar()
• This function reads a character-type data from standard input.
• It reads one character at a time till the user presses the enter key.\
Syntax:
Variable_name = getchar();

Example:
char c;
c = getchar();

by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col


lege(Autonomous) Bengaluru
/* getchar( ) example */
#include <stdio.h>
void main()
{
char c;

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();

by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col


lege(Autonomous) Bengaluru
/* program examples for getch() and
getche() functions */
#include <stdio.h>
void main()
{
char ch1,ch2;
printf(“Enter two alphabets:”);
ch1=getche();
ch2=getch();

printf(“\n Input characters are ch1=%c and ch2=%c”,ch1,ch2);

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

char str[length of string in number];


gets(str);

by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col


lege(Autonomous) Bengaluru
/* program example for gets()
function */
#include<stdio.h>
void main()
{
char ch[30];

printf(“Enter the string:”);


gets(ch);

printf(“Entered string: %s”, ch);


}//End of Program
by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col
lege(Autonomous) Bengaluru
puts()
This function prints the string or character array. It is opposite to gets()

Syntax:
char str[length of string in number];
gets(str);
puts(str);

by Prof. Jeo Joy A, Dept. of Computer Science, Kristu Jayanti Col


lege(Autonomous) Bengaluru

You might also like