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 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

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile