Input and Output


Input and Output

When we talk about input, we're feeding data into a program. An input can be in the form of a file or a command-line argument. The built-in functions in C programming allow you to read the provided input and feed it to the program as needed.

When we talk about output, we refer to data shown on a screen, printed, or saved in a file. C programming has built-in functions for displaying data on a computer screen and saving it to text or binary files.

The Standard Files

All devices are treated as files in C programming. So, much like files, devices like the display are addressed, and the following three files are automatically opened when a program runs to enable access to the keyboard and screen.

The file pointers are the mechanism through which you may read and write to a file. This section demonstrates how to read values from the screen and print the results.

The getchar() and putchar() functions are used to get and put characters in a string () Functions

The int getchar(void) method returns an integer representing the next accessible character from the screen. Only a single character is read at a time by this function. If you wish to read more than one character from the screen, you may use this technique in the loop.

The int putchar(int c) method displays the character provided in and returns the same character. Only a single character is placed at a time using this function. If you wish to display more than one character on the screen, you may use this technique in the loop. Take a look at the following example.

Take a look at the following example.

#include <stdio.h> int main( ) { int c; printf( "Enter a value :"); c = getchar( ); printf( "\nYou entered: "); putchar( c ); return 0; }

When the code above is built and run, it will prompt you to enter some text. When you type something and hit enter, the software scans only one character and displays it as follows:

$./a.out
Enter a value : this is test
You entered: t

The gets() and puts() Functions

Until a terminating newline or EOF, the char *gets(char *s) function reads a line from stdin into the buffer referred to by s. (End of File).

The string's' and a trailing newline are sent to stdout using the int puts(const char *s) function.

NOTE: Despite the fact that the gets() method has been deprecated, you should use fgets instead ().

#include <stdio.h> int main( ) { char str[100]; printf( "Enter a value :"); gets( str ); printf( "\nYou entered: "); puts( str ); return 0; }

When the code above is built and run, it will prompt you to enter some text. When you type a text and click enter, the software scans the entire line till it reaches the end and displays it as follows:

$./a.out
Enter a value : this is test
You entered: this is test

The scanf() and printf() Functions

The int scanf(const char *format,...) function takes data from the standard input stream stdin and scans it according to the specified format.

The int printf(const char *format,...) function publishes the output to the standard output stream stdout and conforms it to the specified format.

The format can be a basic constant string, but you can also use percent s, percent d, percent c, percent f, and so on to print or read strings that are integer, character, or float. There are a variety of different formatting choices that may be utilised depending on the situation. Let us now move on to a basic example to better grasp the concepts:

#include <stdio.h> int main( ) { char str[100]; int i; printf( "Enter a value :"); scanf("%s %d", str, &i); printf( "\nYou entered: %s %d ", str, i); return 0; }

When the code above is built and run, it will prompt you to enter some text. When you type something and click enter, the software scans it and displays it as follows:

$./a.out
Enter a value : seven 7
You entered: seven 7

It's worth noting that scanf() requires input in the same style as the percent s and percent d parameters, which means you'll need to use legitimate inputs like "string integer." If you enter "string string" or "integer integer," it will be considered incorrect. Second, scanf() stops reading a string when it comes across a space, thus "this is test" is three strings for scanf() ().