Tutorial Playlist
Input and output (I/O) in C programming handle data entering and leaving a program. The stdio.h header file provides essential functions like scanf() for input and printf() for output. Streams in C make I/O programming device-independent, enabling programmers to concentrate on data rather than specific sources or destinations. By using streams, programmers can seamlessly manage input streams (data entering the program) and output streams (data leaving the program). This abstraction simplifies handling I/O operations in C programs, ensuring flexibility and ease of use for different devices and data sources.
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.
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.
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 -Â |
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:
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:
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 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.
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:
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.
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
The printf() function is commonly used in C programming to display output on the console. It is defined in the stdio.h header file. Programmers use printf() to print the values of variables or text sentences. Variables of different data types such as float, char, int, etc., can be printed using printf().
#include <stdio.h> |
Output:
Hello, world! Welcome to the world of programming. |
#include <stdio.h> |
Output:
The number is: 42 |
#include <stdio.h> |
Output:
The character is: A |
#include <stdio.h> |
Output:
The value of num1 is: 3.141590 |
#include <stdio.h> |
Output:
Today's date is 1996-8-16 |
The scanf() function in C receives inputs from the user. It allows the program to store the input values into variables of the appropriate data type.
In summary, the scanf() function receives inputs of various data types from the user. It is important to ensure that the variables used to store the input values match data types.
While efforts have been made to provide accurate and original responses, there may still be similarities with existing content. It is always recommended to verify and cross-reference the information provided.
#include <stdio.h> |
Output:
Enter an integer: 25 |
#include <stdio.h> |
Output:
Enter a float value: 3.14 |
#include <stdio.h> |
Output:
Enter a character: A |
#include <stdio.h> |
Output:
Enter two numbers separated by a space: 10 20 |
The scanf() function in C returns the total number of characters it reads, while the printf() function returns the number of characters that would have been printed if there were no errors during the output operation.
Here's an example illustrating the usage:
#include <stdio.h> |
Output:
chocolate Value of x is: 9 |
In the given program, the printf("chocolate") statement prints the word "chocolate" and returns the return value of printf(), which is 9.
Here are some practice problems on input and output functions in C -
Can you find the output obtained from the given program?
#include <stdio.h> |
Answer: The sum of 10 and 5 is: 15
What would be the resulting output of this program?
#include <stdio.h> |
Answer: Hello, World!
What would be the output obtained from the mentioned program?
#include <stdio.h> |
Answer: The area of the circle with radius 3.50 is: 38.48
To sum up, input and output in C programming play a crucial role, enabling accurate communication and collaboration between the program, its users and external devices being used. Their standardised approach to handling data entering and leaving the program is what fuels programmers to create powerful, dynamic and versatile applications in the long run.
While learning all about input and output functions in C through this tutorial can benefit you greatly, upskilling with upGrad courses like Full Stack Software Development Bootcamp can further help you develop and nurture in-demand development skills. With in-demand skills taught under the guidance of industry professionals, this course is made to transform learners into seasoned developers.
Don’t forget to grab this amazing opportunity, enroll now!
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; |
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().
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...