0% found this document useful (0 votes)
16 views10 pages

Data Input Output Functions

The document explains input and output in C programming, detailing how data is received from external sources and displayed after processing. It covers the use of functions like scanf() for input and printf() for output, as well as the types of input/output functions available in C, including formatted and unformatted functions. Additionally, it discusses the importance of these functions for user interaction, data processing, and debugging.

Uploaded by

sathwikakantekar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Data Input Output Functions

The document explains input and output in C programming, detailing how data is received from external sources and displayed after processing. It covers the use of functions like scanf() for input and printf() for output, as well as the types of input/output functions available in C, including formatted and unformatted functions. Additionally, it discusses the importance of these functions for user interaction, data processing, and debugging.

Uploaded by

sathwikakantekar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is input in C?

In the basic structure of C program, input refers to providing data or information to a


program from an external source. Similar to how an ATM requires a user to input their
PIN to perform transactions, algorithms in programming often require external data to
operate effectively.
In C, input can be received from various sources, such as the command line or a file.
When data is provided to a C program from an external location, it is stored in the
computer's Random Access Memory (RAM), where the program can access and
process it. This external data, essential for the program's execution, is input.
By accepting input, C programs can interact with users, process external data, and
perform desired operations based on the provided information.

What is output in C?
In the basic structure of C program, input refers to providing data or information to a
program from an external source. Imagine using an ATM where you enter your PIN and
other required inputs. After following the instructions, the ATM provides cash, bank
details, or other desired information. Similarly, algorithms use the input in programming
to perform specific operations and generate the desired results.
After the algorithm processes the input, it produces output. The output can be
presented in various ways, such as displaying it on the screen, printing it on a printer, or
saving it to a disk. The purpose of output is to send the data from the program's
memory to a specified location.
It's important to note that while input is necessary for many operations, there are cases
where output can be generated without any specific input. For example, an algorithm
designed to generate random numbers will produce random numbers as output without
requiring specific input values.
How to take input and output of basic types in C?
To take input and output of basic types in C, you can use the scanf() function for input
and the printf() function for output. Here's how you can do it:
Input:
To take input, use the scanf() function with the appropriate format specifier for the
desired data type. The general syntax is:

scanf("%A", &variable);

Replace '%A' with the format specifier corresponding to the data type you want to input.
'&' is used to get the address of the variable, allowing the input to modify its value.
Output:
To display output, use the printf() function with the appropriate format specifier for the
desired data type. The general syntax is:

printf("%A", variable);

Replace '%A' with the format specifier corresponding to the data type you want to
output. This function will print the value of the variable specified.
For example, for different basic data types where scanf is used for input and printf is
used for output:

Integer -

scanf("%d", &intVariable);

printf("%d", intVariable);

Float -

scanf("%f", &floatVariable);

printf("%f", floatVariable);

Character -

scanf("%c", &charVariable);

printf("%c", charVariable);

Input and Output Functions in C: Why do we use it?


We use input and output functions in C to enable communication between the program
and the user or external devices. Here are some reasons why input and output functions
are essential in C programming:
● Interacting with the user: Input functions allow the program to receive data or
information from the user. This enables user interaction and makes programs
more dynamic and flexible. For example, a program may prompt the user for
input to perform specific calculations or make decisions based on user
preferences.
● Processing external data: Input functions enable the program to read data from
external sources, such as files or databases. This allows programs to work with
large datasets, configuration files, or other external resources. The program can
process this data, perform computations, or manipulate it as required.
● Providing feedback and results: Output functions allow the program to display
information or results to the user. This can include printing messages, displaying
calculated values, or generating reports. Output functions help in conveying
information and communicating the program's output effectively.
● Debugging and troubleshooting: Input and output functions are valuable tools for
debugging and troubleshooting programs. They allow developers to inspect the
program's behaviour by printing intermediate values, checking variable states, or
logging important information during program execution. This helps identify
issues, understand program flow, and diagnose errors.
● Integration with external devices: Input and output functions facilitate
communication with external devices, such as printers, displays, or network
interfaces. They provide a standardised way to send and receive data, enabling
programs to interact with various hardware components and peripherals.

Types of Input and Output Functions in C


In the C programming language, input refers to providing data or information to a
program, while output refers to displaying or writing the processed data. C provides a
standard library that includes various functions to facilitate input and output operations.
There are two main types of input and output functions in C:
Formatted Functions
These functions allow data to be presented or accepted in a specific format. The most
commonly used formatted input function is scanf(), which reads input from the user or
external sources based on specified format specifiers. The printf() function is used for
formatted output, allowing the program to display data in a desired format. These
functions provide flexibility in handling different data types, such as integers,
floating-point numbers, characters, etc.
Unformatted Functions
Unformatted functions are more basic and do not control data format during input or
output operations. They are categorised into character functions and string functions.
Character functions, such as getchar(), getche(), and getch(), are used for reading single
characters from the input device (e.g., keyboard). On the other hand, putchar() and
putch() are used for output, allowing the program to write single characters to the
output source (e.g., screen). String functions, like fgets() and puts(), are commonly used
for reading and writing strings of characters. These unformatted functions are simpler
but provide the necessary functionality for basic input and output operations.
The C Standard Files
In the C language, all devices, including the screen and keyboard, are treated as files
within a program. This means that when a program is executed, three files are
automatically opened to provide access to the screen, keyboard, and standard error:
● Standard output: Represented by the file pointer "stdout," this file allows the
program to display output on the screen or any output device.
● Standard input: Represented by the file pointer "stdin," this file enables the
program to read input from the keyboard or any input source.
● Standard error: Represented by the file pointer "stderr," this file is used for error
messages and displays error-related output on the screen or the appropriate error
output device.
The C language utilises file pointers to access and manipulate these files for reading
and writing operations within a program. This abstraction allows for consistent input
and output handling across different devices and facilitates efficient interaction with the
user and error reporting.
The Format Specifiers
In C programming, format specifiers are used in functions like printf() and scanf() to
specify the type of data being printed or read. These format specifiers inform the
program about the expected data type. Here are some commonly used format
specifiers and their corresponding data types:
Table showing data types and format specifiers

Taking Input of Multiple Values

#include <stdio.h>

int main() {
int num1, num2;

printf("Enter two numbers separated by a space: ");


scanf("%d %d", &num1, &num2);

printf("The entered numbers are: %d and %d", num1, num2);

return 0;
}

1. What is the purpose of fflush() function in C?


The function fflush() is C is utilised to clean the output buffer faced in a stream. At
times when you wish to get the output to be displayed on screen immediately, this
function helps enable that by immediately getting the buffered data cleared and written
on the output.

2. How to clear the input buffer?


In order to clear the input buffer, run the given code snippet -
int c;
while ((c = getchar()) != '\n' && c != EOF);

3. How to handle input errors when using scanf()?


In order to handle the input errors when using scanf(), make sure you assess its return
value, which is the number of conversions successfully done. If the value is lower than
the expected number of conversions, an error can be assumed. Clear the input buffer
and prompt the user to enter the input one more time. This will lead to successful
handling of an error when using scanf().

Reading input from the user and showing it on the console (output) are the
common tasks every C program needs. C language provides libraries
(header files) that contain various functions for input and output. In this
tutorial, we will learn different types of formatted and unformatted input
and output functions.

The Standard Files in C


The C language treats all the devices as files. So, devices such as the
"display" are addressed in the same way as "files".
The following three files are automatically opened when a program executes
to provide access to the keyboard and screen.

Standard File File Device

Standard input stdin Keyboard

Standard output stdout Screen

Standard error stderr Your screen

To access a file, C uses a predefined FILE struct type to refer to the file for
reading and writing purpose. Read this chapter to understand how to read
values from the screen and how to print the result on the screen.

Types of Input and Output Functions


We have the following categories of IO function in C −

​ Unformatted character IO functions: getchar() and putchar()


​ Unformatted string IO functions: gets() and puts()
​ Formatted IO functions: scanf() and printf()

The unformatted I/O functions read and write data as a stream of bytes
without any format, whereas formatted I/O functions use predefined formats
like "%d", "%c" or "%s" to read and write data from a stream.

Unformatted Character Input & Output Functions: getchar()


and putchar()

These two functions accept a single character as input from the keyboard,
and display a single character on the output terminal, respectively.

The getchar() function it reads a single key stroke without the Enter key.

int getchar(void)
There are no parameters required. The function returns an integer
corresponding to the ASCII value of the character key input by the user.

Example 1

The following program reads a single key into a char variable −

#include <stdio.h>

int main() {

char
int getchar(void)
There are no parameters required. The function returns an
integer corresponding to the ASCII value of the character key
input by the user.

Example 1
The following program reads a single key into a char variable −

ch;

printf("Enter a character: ");


ch = getchar();

puts("You entered: ");


putchar(ch);

return 0;
}

Output
Enter a character: W
You entered:
W
Example 2

The following program shows how you can read a series of characters till the
user presses the Enter key −

#include <stdio.h>

int main() {

char ch;
char word[10];
int i = 0;

printf("Enter characters. End with pressing enter: ");

while(1) {
ch = getchar();
word[i] = ch;
if (ch == '\n')
break;
i++;
}

printf("\nYou entered the word: %s", word);

return 0;
}

Output
Run the code and check its output −

Enter characters. End with pressing enter: Hello


You entered the word: Hello

You can also use the unformatted putchar() function to print a single
character. The C library function "int putchar(int char)" writes a character
(an unsigned char) specified by the argument char to stdout.

int putchar(int c)
A single parameter to this function is the character to be written. You can
also pass its ASCII equivalent integer. This function returns the character
written as an unsigned char cast to an int or EOF on error.

Example 3

The following example shows how you can use the putchar() function −

You might also like