Input & Output in C

When we talk about Input, we are talking about input data for the program. It can be provided from the command line or from a certain file. Program C language provides a set of functions available to read the entered data and provide it for the required programs.

When we talk about Output, we are talking about the results displayed on the screen, printer or any file. The C language provides a set of functions for exporting result data on a computer screen as well as being able to store that data in text or binary files.

Standard files in C

C language treats all devices as files. Therefore devices such as display screens are located in the same way as files and accordingly there are 3 files that are automatically opened when a program performs to provide access to the keyboard and screen.

Standard file Point to File Device

Standard input

Standard input

stdin Keyboard

Standard output

Standard output

stdout Screen

Standard error

Standard error

stderr Your screen

File pointers mean accessing that file for reading and writing purposes. This area will explain how to read the value from the screen and how to print the results on the screen.

The functions getchar () & putchar () in C

The function int getchar (void) reads the next available character from the screen and returns an integer number. This function only reads one single character at a time. You can use this method in the loop in case you want to read more than one character from the screen.

The function int putchar (int c) places the transmitted character on the screen and returns the character itself . This function only puts a single character at a time. You can use this method in the loop in case you want to display more than one character on the screen. Check the following example:

 #include int main ( ) { int c ; printf ( "Nhap mot gia tri: " ); c = getchar ( ); printf ( "nGia tri ban da nhap la: " ); putchar ( c ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

When the above code is compiled and executed, it waits for you to enter the text and press ENTER, the program processes and reads only a single character and then looks at the result:

The function gets () & puts () in C

The char * gets (char * s) function reads a line from stdin in the buffer pointed to by s until either the new line ends or EOF.

The function int puts (const char * s) writes the string s and a new line to stdout .

 #include int main ( ) { char chuoi [ 100 ]; printf ( "Nhap mot gia tri: " ); gets ( chuoi ); printf ( "nGia tri ban da nhap la: " ); puts ( chuoi ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

When the above code is compiled and executed, it waits for you to enter text and press ENTER, then the program processes and reads the whole line and sees the result:

Function scanf () and printf () in C

The function int scanf (const char * format, .) reads the input from standard input stdin and scans the input according to the format provided.

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

The format may be a simple string, but you can specify% s,% d,% c,% f, . to print or read the corresponding string, integer, character, or number. There are many options available that can be used on demand. For more details about these functions, you can access the help page. We now handle a simple example:

 #include int main ( ) { char chuoi [ 100 ]; int i ; printf ( "Nhap mot gia tri: " ); scanf ( "%s %d" , chuoi , & i ); printf ( "nGia tri ban da nhap la: %s %d " , chuoi , i ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

When the above code is compiled and executed, it waits for you to enter text and press ENTER, then the program processes and reads the input and sees the result:

You should keep in mind that scanf () expects that the input you enter is in the same format as you provided:% s and% d, meaning you must provide valid input as "string integer", if you providing "string string" or "integer integer" then it will assume that the input you entered is wrong. The second thing, while reading a string, the scanf () function stops reading as soon as it encounters a space, so "this is test" is the three strings for the scanf () function.

According to Tutorialspoint

Previous post: Keyword typedef in C

Next article: Read - Write File in C

3.5 ★ | 2 Vote

May be interested

  • Read - Write File in CRead - Write File in C
    the previous chapter explained the standard input and output devices handled by the c language. in this chapter we will see how programmers create, open and close text files or binary files with data. storage.
  • Store the output of a Linux command to a fileStore the output of a Linux command to a file
    usually, while working on a linux terminal, you may want to save the output of a command to a file. here are 4 different ways in which to save terminal content in a file.
  • How to Change Your Input Method in Max OS X LionHow to Change Your Input Method in Max OS X Lion
    depending on where you live, the placement of keys on your keyboard will differ. this can be confusing if you're used to using a certain keyboard to type in a certain language. for this reason, you can customize the input your mac receives...
  • How to fix 'Input Signal Out of Range' error on WindowsHow to fix 'Input Signal Out of Range' error on Windows
    when connecting an external monitor to a windows pc, you may encounter an input signal out of range error. this error usually occurs if you have a high refresh rate monitor connected to a lower end graphics device.
  • Instructions on how to create input forms in Excel extremely fast and simpleInstructions on how to create input forms in Excel extremely fast and simple
    creating an input form in excel is a very necessary tip if you are an accountant or office worker. it helps you to make statistics and input data for excel simpler and faster. below, tipsmake will guide you the most basic steps to create a simple input form.
  • How to Reflash Your BIOSHow to Reflash Your BIOS
    bios is short for basic input-output system. it is a set of electronic instructions stored on a chip inside your desktop or laptop. these instructions tell the computer how to perform the post (power on self test) and allow rudimentary...
  • What connection ports do projectors usually have?What connection ports do projectors usually have?
    projectors have connections for devices ranging from laptops to game consoles. this can make it difficult to find the right input or output. let's learn about the types of connectors for projectors through the following article!
  • How to limit data with Data Validation in Google SheetsHow to limit data with Data Validation in Google Sheets
    the data validation function is used to prevent users from entering anything other than correctly formatted data within a specific range. and this is how to use it.
  • 5 tips for using the BIOS to help you master your computer5 tips for using the BIOS to help you master your computer
    most computer users often don't care much about the bios. however, when the problem occurs, you need to tweak a setting and don't know how. you will wonder what the bios is? really need to know about it?
  • Input element attributes in HTMLInput element attributes in HTML
    the article below introduces the properties of input elements when creating forms in html.