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

Input and Output PDF

The document discusses input and output functions in C programming. It covers unformatted input/output functions like getch(), putch(), getchar(), putchar() and formatted input/output functions like scanf(), printf(). It provides examples of using these functions to take input of different data types like integer, float, character and outputting the same.

Uploaded by

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

Input and Output PDF

The document discusses input and output functions in C programming. It covers unformatted input/output functions like getch(), putch(), getchar(), putchar() and formatted input/output functions like scanf(), printf(). It provides examples of using these functions to take input of different data types like integer, float, character and outputting the same.

Uploaded by

vumisunar4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Input and Output

Presentation by:
Bhumika Sunar
Deepanjali Shrestha
Ragini Yadav
What is Input and Output?
Input:
When we say Input, it means to feed some data into a program.
An input can be given in the form of a file or from the command line.
C programming provides a set of built-in functions (i.e. scanf, printf, strcpy) to read the
given input and feed it to the program as per requirement.

Output:
When we say Output, it means to display some data on screen, printer, or in any file.
C programming provides a set of built-in functions to output the data on the computer
screen as well as to save it in text or binary files.
Types of I/O functions in C
1. Unformatted I/O Functions.
2. Formatted I/O Functions.

Unformatted I/O function:


Unformatted I/O functions are used only for character data type or
character array/string and cannot be used for any other datatype.

• Why they are called unformatted I/O?


It is because we cannot use format specifiers in these functions and
hence, cannot format these functions according to our needs.
The following unformatted I/O functions will be discussed in this section which has two categories:

getch() putch()
getchar() putchar() Unformatted output functions

getche() Unformatted input functions


puts()
gets()
getch() putch()
 It reads single character.  Display a single character which is given by the
user.
 It doesn't echoes on the screen.
 It is not waiting to press enter to show output.
 We can type only one character at a time.
 It will accept a Keystroke and never displays it.
 It is suitable for passwords.
getch() putch()
#include<stdio.h>
#include<conio.h> #include<stdio.h>
main() #include<conio.h>
{ main()
char ch; {
char ch;
printf(“Enter the character:");
ch= getch();
printf(“Enter the character:");
printf("\n Entered character is:%c", ch);
ch= getch();
}
putch(ch);
}
Output:
Enter the character: Output:
Entered character is: A Enter the character: A
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
getche()
It reads only single character.
It echoes on the screen.
printf(“Enter the character:");
We can type only one character at a ch= getche();
time. printf("\n Entered character is:%c", ch);
It will immediately display on the
screen without the ‘enter’ key.
}
Output:
Enter the character: A
Entered character is: A
getchar() putchar()

Reads only first single character at a Displays a single character at a time by


time(even if we type multiple passing that character directly to it or by
characters). passing a variable that has already
It echoes on the screen. stored a character.
It is waiting to press enter to show the
output.
getchar() putchar()
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<conio.h>
main()
main()
{
{
char ch;
char ch;

printf(“Enter the character:");


printf(“Enter the character:");
ch= getchar();
ch= getchar();
printf("\n Entered character is:%c", ch);
putchar(ch)
}
}

Output:
Output:
Enter the character: abcd
Enter the character: abcd
a
Entered character is: a
gets() puts()

Reads a group of characters or strings Displays a group of characters or


from the keyboard by the user and these strings which is already stored in a
characters get stored in a character array. character array.
gets() and puts()
#include<stdio.h>
#include<conio.h>
int main()
{
char st[50];

printf("enter the character:");


gets(st);
printf("\n entered character is:");
puts(st);
}

Output:
Enter the character : this is presentation
Entered character is: this is presentation
Formatted I/O Functions:
Formatted I/O functions are used to take various inputs from the user
and display multiple outputs to the user.
These functions can help to display the output to the user in different
formats using the format specifiers.

Format specifiers:
In C, the basic data types like int, float, and char each has specific
format specifiers that should be used with functions like scanf() and
printf() for input and output.
These format specifiers ensure that the data is interpreted and
displayed correctly, and they are an essential part of input and output
operations in C.
Format Specifier Datatype Used for

%d %i int Read/print signed integer values


%u unsigned int Read/print unsigned integer values
%li long int Read/print long integer values
%lu unsigned long int Read/print unsigned long integer values

%hd short int Read/print short integer values


%c char Read/print a character
%c signed char Read/print signed character
%c unsigned char Read/print unsigned character
%f float Read/print floating point number
%lf double Read/print the double values
%Lf long double Read/print long double values
%s char Read/print the string
The following formatted I/O functions will be discussed:
1. printf()
2. scanf()

printf():
printf() function is used to display any value like float,
integer, character, string, etc. on the console screen.
It is a pre-defined function that is already declared in the
stdio.h(header file).
Syntax:
To display any variable value:
printf(“Format Specifier”, var1, var2, …., varn);
(%d, %f, %s, %c, etc.)
scanf():
scanf() function is used for reading or taking any value from the keyboard by the
user, these values can be of any data type like integer, float, character, string, and
many more.
This function is declared in stdio.h(header file), that’s why it is also a pre-defined
function.
In scanf() function, we use &(address-of operator) which is used to store the
variable value on the memory location of that variable.
Syntax:
scanf(“Format Specifier”, &var1, &var2, …., &varn);
(%d, %f, %s, %c, etc.)
For example,
• Integer
Input: scanf(“ %d ", &intVariable);
Output: printf(“ %d ", intVariable);

• Float
Input: scanf(“ %f ", &floatVariable);
Output: printf(“ %f ", floatVariable);

• Character
Input: scanf(“ %c ", &charVariable);
Output: printf(“ %c ", charVariable);
// C program to show input and output
#include <stdio.h> while((getchar())!='\n');

int main() //Character


{ printf("\n\nEnter the Character: ");
scanf("%c", &ch);
int num;
char ch; declaring the variables
float f; printf("\nEntered character is: %c", ch);

// Integer return 0;
printf("Enter the integer: "); }
scanf( "%d", &num);
format specifier address-of operator

printf("\nEntered integer is: %d", num); Output of above source code:


Enter the integer: 10
//Float Entered integer is: 10
printf("\n\nEnter the float: ");
scanf("%f", &f); Enter the float: 2.5
Entered float is: 2.500000

printf("\nEntered float is: %f", f); Enter the Character: A


Entered Character is: A
 How to take input and output of advanced type in C?
In C, strings are typically represented as arrays of characters, and when working with
strings, you indeed use the %s format specifier for input and output.

Input: scanf("%s", stringVariable);


Output: printf("%s", stringVariable);
// To read/input a Sentence
Example: printf("\n\nEnter the Sentence: ");
scanf("%s", str);
#include <stdio.h>
int main() // Output the Sentence
{ printf("\nEntered Sentence is: %s", str);
// Declare string variable as character array return 0;
char str[50]; }
// To read/input a word
printf("Enter the Word: "); Output:
scanf("%s\n", str); Enter the Word: Skill Vertex
Entered Word is: Skill Vertex
// Output the Word Enter the Sentence: This is our presentation
printf("\nEntered Word is: %s", str); Entered Sentence is: This is our presentation
Conclusion:

Basic Input and Output (I/O) in the C programming language provides


the means to receive data from the user, display information to the
user, and read from or write to files. It is fundamental to interacting
with the user and the external world.
THANK YOU!

You might also like