Clear, practical technology insights About · Contact

Input && Output in C

Explore Input && Output in C with clear explanations, practical examples, and useful tips.

Published: 7 minutes read
Table of Contents

This guide covers input && output in c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

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 output

Standard error

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

Frequently Asked Questions

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Was this article helpful?

Your feedback helps us improve.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.