C input & output


When we refer to input, it means filling the program with some data. Input can be in the form of a file or from the command line. The C language provides a series of built-in functions to read the given input and fill it into the program as needed.

When we refer to output, it means to display some data on the screen, on the printer, or in any file. The C language provides a series of built-in functions for outputting data to the computer screen and saving data to text files or binary files.

Standard files

The C language treats all devices as files. So devices (such as monitors) are treated the same way as files. The following three files are automatically opened when the program is executed to provide access to the keyboard and screen.

Standard fileFile pointerDevice
Standard inputstdinKeyboard
StdoutstdoutScreen
Standard ErrorstderrYour screen

The file pointer is the way to access files. This section will explain how to read values ​​from the screen and how to output the results to the screen.

getchar() & putchar() Function

int getchar(void) The function reads the next available character from the screen and returns it as an integer. This function will only read a single character at a time. You can use this method inside a loop to read multiple characters from the screen.

int putchar(int c) The function outputs characters to the screen and returns the same characters. This function will only output a single character at a time. You can use this method inside a loop to output multiple characters on the screen.

Please see the example below:

#include <stdio.h>int main( ){   int c;

   printf( "Enter a value :");
   c = getchar( );

   printf( "\nYou entered: ");
   putchar( c );   return 0;}

When the above code is compiled and executed, it will wait for you to enter some text. When you enter a text and press the Enter key, The program will continue and will only read a single character, as shown below:

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

gets() & puts() Function

char *gets(char *s) The function reads a line from stdin into the buffer pointed to by s until a terminator or EOF.

int puts(const char *s) The function writes the string s and a trailing newline character to stdout.

#include <stdio.h>int main( ){   char str[100];

   printf( "Enter a value :");
   gets( str );

   printf( "\nYou entered: ");
   puts( str );   return 0;}

When the above code is compiled and executed, it waits for you to enter some text, when you enter a text and press the enter key, the program continues and reads a whole line until that line At the end, the display is as follows:

$./a.out<b>Enter a value :</b> this is test<b>You entered:</b> This is test

scanf() and printf() functions

int scanf(const char *format, ...) Function from the standard input streamstdin Read input and browse the input according to the provided format.

int printf(const char *format, ...) The function writes output to the standard output stream stdout and generates output according to the provided format.

format can be a simple constant string, but you can specify %s, %d, %c, %f, etc. to output or read strings, integers, characters respectively or floating point number. There are many other formatting options available which can be used according to your needs. For complete details, check out the reference manuals for these functions. Now let us deepen our understanding through the following simple example:

#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 above code is compiled and executed, it will wait for you to enter some text. When you enter a text and press the Enter key , the program continues and reads the input, displaying the following:

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

Here, it should be noted that scanf() expects the input to be in the same format as the %s and %d you gave it, which means you Valid input must be provided, such as "string integer", if you provide "string string" or "integer integer" it will be considered bad input. In addition, when reading a string, scanf() will stop reading as soon as it encounters a space, so "this is test" is three strings to scanf().