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

Unit 3 Input and Output

Uploaded by

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

Unit 3 Input and Output

Uploaded by

Manish Karki
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Unit 3: Input/Output

Functions (2 Hrs)
Contents of the unit
• Conversion Specifiers: IO functions: formatted and Unformatted
I/O(scanf, printf, getchar, putchar, getch, getche, gets, puts, putch,
getc , putc )
Introduction
• One of the essential operations performed in a C language programs is
to provide input values to the programs and output the data produced
by the program to a standard output device.

• One method to give value to variable through the use of assignment


operator as x = 10.
• Another method is to the use of scanf() function that reads data from
keyboard.
• For outputting the results we use printf() function that displays result
on monitor
Introduction
• There exists several functions in “C” language that can carry input and
output operations.
• These functions are collectively known as standard Input / Output
Library.
• Each program that uses standard input/ output function must contain
the statement
• #include<stdio.h>
• at the beginning
Input and Output Functions
• The input and output functions are classified as follows
1. Formatted Functions
2. Unformatted Functions
Formatted Functions
• The formatted functions allow the input from the keyboard or output
displayed on screen to be formatted according to our requirements.
• Input function: scanf ()
• Output function :printf()
Formatted Input
• The well-known function for formatted input is scanf. This is built-
function and is used to enter input data into the computer from a
standard input device.
• Its general form is as follows:
• scanf(“control string”, arg1, arg2,…argn);

• Here control string is the format in which data is to be entered.


• And agr1, agr2… argn are the location where the data is stored and
are preceded by ampersand sign(&)
Example
• #include<stdio.h>
• Int main()
•{
• int n;
• printf(“Enter any number”);
• scanf(“%d”,&n)
• ……………
Formatted Output
• The formatted output refers to the output of data that has been arranged in a
particular format.
• The function printf() is an example of formatted output that is used to output data
from program onto the screen
• General form:
• printf(“Control string”, agr1, arg2,……argn);
• The control string may of following types:
1. Character that will be printed on the screen as they appear
2. Format specification that define the output format for display of each item
3. Escape sequence characters such as \t or \n
4. Any combinations of above
Example
• #include<stdio.h>
• #include<conio.h>
• int main()
•{
• int a;
• float b;
• char ch;
• a = 10;
• b = 12.5;
• ch = 'A';
• printf("%d\t%0.3f\n%c",a,b,ch);
•}
Unformatted Functions
• Unformatted functions do not allows user to read or display data in
desired format.
• These library functions basically deal with a single character or a
string of characters.
• The functions getchar(), putchar(), gets(), puts(), getch(), getche(),
putche() are considered as unformatted functions.
Example
• // gets() and puts()
• #include<stdio.h>
• #include<conio.h>
• int main()
•{
• char name[50];
• puts("What is your name:");
• gets(name);
• puts("Your name is ");
• puts(name);
•}
getchar()
• The getchar() function reads a character from a standard input device.
• It takes the form:
• char ch = getchar();

• This function reads only character at a time.


putchar() function
• This function displays only character at a time on the monitor

• Its general form is:


• putchar(character_variable);
Example
• #include<stdio.h>
• #include<conio.h>
• int main()
•{
• char ch;
• printf("Enter a character\n");
• ch = getchar();
• printf("You input\n");
• putchar(ch);
• return 0;
•}
Output
Program to read string
• #include<stdio.h>
• int main()
•{
• char name[30];
• printf("Enter your name\n");
• gets(name);
• printf("You typed”);
• puts(name);
• return 0;
•}
Output
gets() and puts() function
• The gets() function reads string from keyboard. The puts() function is
used to print string on the monitor.

You might also like