Input and Output Functions in C
Input and output functions are foundational to C programming, facilitating data exchange via standard libraries like stdio.h. With functions like scanf() and printf(), these operations utilize streams to manage data flow efficiently, ensuring seamless interaction with various devices. This mechanism simplifies the complex process of receiving data (input) and presenting results (output), making programming device-independent.
What is input in C?
Have you visited ATMs? If yes, you know ATM is an Electronic Banking outlet that provides transactions to the users based on some set of instructions. Still, for every user, it requires some details like a PIN, which means most algorithms have some set of instructions but need some external data/information to work over it. Input refers to the process of feeding data into the program. Data can be in a command line or from a file. Random Access Memory is kept by the C program while executing. When data come from an external location to the program, it is moved to RAM where the program can access it and that external data is known as input.
What is output in C?
Let's continue our ATM story so that when users provide PINs and other required inputs, the ATM, after performing all the instructions, provides cash, bank details, or other desired stuff, which means the algorithm, after performing on the input, provides the desired results. Still, it may be in different ways, like the output on screen or a print through a printer or in another way. Output refers to sending data out of the program or simply sending data to the location out of the program memory. The destination of the data may be a screen, printer, or Disk. For getting output, it is not always compulsory to have an input, like an algorithm for generating random numbers will simply return random numbers without any input.
How to take input and output of basic types in C?
We have some input and output functions in C, let's look over them: Input
For taking input in C, we use the built-in function of the C scanf(). scanf() method reads the input from standard input stream stdin and scans that input as per the type specified.
Syntax of taking input in C
The above syntax is for taking input from the user. Where %A defines format specifier in C, it helps the compiler identify the data type of variable for which we will take input, and '&' is the address operator in C. It helps the compiler change the actual value of this variable stored at this address in the memory.
Output
For display output to the user in C, we are using the built-in function of C printf(). printf() method writes the output to the standard output stream stdout and prints the value passed as the parameter to it.
Syntax of display output in C
The above syntax for displays output to the user. Where %A defines format specifier in C, it helps the compiler identify the data type of variable we are going to output.
The basic type of input and output in C includes data types of variables like int, float, char, etc. The A in the above syntax is replaced with the appropriate format specifier of that type.
Format specifier for different data types
Data type | value of A |
---|---|
int | %d |
float | %f |
char | %c |
long | %l or %ld |
double | %lf |
Syntax of input and output of basic data types in C
Example
Output
The above code takes the inputs for variables of a type character, integer, and float using scanf and then outputs them using printf() with the help of proper format specifiers.
How to take input and output of advanced types in C?
There are various types of Advanced or user-defined data types in C, like:
- String
- Structure
To take the input or provide the output, we will use the same input and output functions of C that we have used for primary data types, let's discuss how we will do that.
1. String
Strings are simply a one-dimensional character array with an end line character ‘\0’ at the end. In string, all the characters are present in the contiguous memory block, and the maximum size of the string is pre-defined.
Syntax to define a string
Here, size is a pre-defined integer that refers to the maximum size of the string. There are four ways to take input of a string:
- gets()
- This function will take the complete line as input from stdin and store it in the string given.
- gets() function is removed in C11, so using this function may cause some errors.
Syntax:
- fgets()
- This function takes input from a specific stream of characters. We send the maximum number of characters to take as input and based on this there are three cases possible:
- If the number of characters in the stream is equal to or greater than the given maximum size, it will take the first n characters (where n is maximum size).
- If the number of characters in the stream is less than the given size, it will take the complete line as input.
- It stops when the newline character hits.
- As the gets() function is removed, this function is one of the alternatives. Syntax:
- scanf() by using %[^\n]%*c as access specifier
- This function will take the complete line as input, including the whitespaces.
- %[^\n]%*c breaks into sub-parts:
- In the scanf() function, we can use a regular expression to restrict the input here %[^\n] is used to take input only until no new line appears.
- %c is used to take input of characters,
- "*" is used to inform the scanf() function not to assign input to the variable until the whole input is taken.
Syntax:
- scanf() by using %s as an access specifier
- This function will take input only up to the first space character.
Syntax:
Note: In scanf() for string, we don't have to use "&" as the name of the string is a pointer to its first position and all of the functions mentioned above are present in stdio.h header file.
We use the printf() function to print the string by taking %s as a format specifier. Syntax:
Example
For input:
Output:
In the above code we take the input for strings using different methods as discussed above and then print the strings using printf().
2. Structure
Structure is the user-defined data type, usually used to combine various data types together. A structure is consists of various data members and when we access its data members using dot operator they behave like normal data-types variables. So, the process of input/output for structure variables is similar to other data types variables using above-defined input and output functions of C. Let’s take an example for better understanding:
Example: We have created a structure, an object of it, then with the help of the dot operator access structure data members to take input with the help of scanf() function. At last, printed every variable by accessing it with dot operator and printing with printf() function. Code for this is mentioned below:
Let us provides input as: Person_name 21 34 5.7
Output:
Built-in functions
In this section we are going to see some built-in input and output functions in C in detail.
scanf() and printf()
As we discussed above, scanf() and printf() are used for input and output methods of the programming language of c.
scanf() function helps to read the file or input which we provide and in scanf() function we use format specifiers like %c, %d, etc to detect the data type of variable which we give as input. Return type of scanf() is integer. scanf() functions return the total numbers of variables which are scanned successfully means total number of inputs. It has three types of return value they are -
- Greater than zero if the variables are passed successfully.
- Equal to zero if no variable provided.
- Less than zero if error occurs or EOF (end-of-file).
Syntax
Example1
In the below example we are scanning two variables at a time. In which %d is used for first one val1 and %c is used for another one val2. So here the return value of it will be 2 because here it scanned 2 variables. If you observe, you can identify the data type of both variables are different, the first one is integer and the second one is character. We can easily identify it with the help of the Format specifier.
Output
printf() function is used for displaying output to the screen and in printf() function we use format specifiers like %c, %d, etc to detect the data type of variable which we give as input. Return type of printf function is integer. It returns the total no of characters given as output by printf().
Syntax
For example
In the below example, we print two variables val1 and val2 but you can see data type of val1 is integer and val2 is character and in integer you can give input -2e31 to 2e31-1 and in the character you can only give one character. So the printf() function returns the total number of characters in int plus one character. And here we do not providing any space or line gap between the val1 and val2 otherwise it was also counted.
Output
getchar() and putchar()
putchar() function is the function of the standard output console. It displays only a single character at a time and character is a type of unsigned char (means char uses all 8 bits and there is no sign bit). And the character which is passed to this function is passed as a parameter. Return type of this function is int and it return the ASCII value of the character which is passed to it. If the function is executed successfully it returns the ASCII value of the character which passed as parameter to this function and otherwise return EOF if error occurs.
Syntax
Example 1
Output
Example 2
Output
getchar() function is the function of the standard input console. It takes a single character as input at a time and it does not take any parameter. Return type of its character type of unsigned char which it reads as input and return EOF(end-of-file) if error occurs.
Syntax
Example
Output
gets() and puts()
Puts functions are the part of output and gets functions are the part of input. They are declared in stdio.h header file.
puts() function used for print/displaying the output on the screen but here passes variables only in the form of line or string. It prints the passed string in the new line, and its return type is integer. It gives only two type of return value, they are-
- Number of character which been printed to console if successful execution.
- Any error or EOF (end of file).
Syntax
Example
Output
gets() function is used for the input method. It reads the line and stores it in the character array. Due to this function, we can get a input of string followed by enter or space as input.
The return type of this function is string. It returns a string (which passes as parameter in the gets() function) on successful execution and returns EOD (end of file) if any error occurs. Syntax
Example
fprintf()
fprintf() function is used for printing output in the file instead of the standard output screen.
Syntax
In the above syntax, stream represents the file where we have to print the output. format is a string, which is basically our output and it may be embedded by some format tags which are replaced by values passed as arguments.
Let's see an example for more understanding:
Output
example.txt
In the above code, we created a file and taken a string as input. Then we write the taken input to the file using fprintf() function.
putch() & getche()
putch() function is declared in the conio.h header file. It is used to print a single character on the screen and the character is a type of alphanumeric char ( value can be alphabet or number).
getche() function is also declared in the conio.h header file. It is used to take characters from standard input keyboards and after taking input it immediately prints the output on output screen, we don't need to hit enter to give command for output.
Syntax
Format specifiers for input/output
Format specifier is used to define the data type of variable in input and output function in C. It helps compiler to identify the data type of variable. It is use inside the scanf() function for taking input and inside the prinf() function for displaying output. Total we have eight format specifier in c.
Data type | Format specifiers | Description |
---|---|---|
int | %d | use for decimal integer value |
%u | use for unsigned integer value | |
%o | use for unsigned octal value | |
%x | use for unsigned hexadecimal value | |
long int | %ld | use for long int value |
double | %f | use for double value |
%lf | use for long double value | |
float | %f | use for floating point value |
%e | use for floating point value in decimal or exponential form. | |
char | %c | use for single character value |
%s | use for string of value of character |
Conclusion
- Input refers to the process of feeding data into the program and Output refers to the process of sending data out of the program.
- Standard Input/Output library included in program using stdio.h header file which consist of main input and output functions in C scanf() and printf().
- Streams in C programming are used to take input or give output to put away worries of initial location or final destination of the data.