Latest web development tutorials

C input & output

When we refer tothe input, which means that the program would like to fill in some data.Input can be in the form of a file from the command line or carried out. C language provides a set of built-in functions to read the given input, and filled as needed into the program.

When we talk aboutoutput, that means, any file on the printer or display some data on the screen.C language provides a set of built-in functions to output the data on the computer screen and save the data to a text file or a binary file.

Standard file

C language put all the equipment as a file. So the device (such as a display) to be treated the same way with the files. The following three files are automatically opened when the program is implemented in order to access the keyboard and screen.

标准文件文件指针设备
标准输入stdin键盘
标准输出stdout屏幕
标准错误stderr您的屏幕

File pointer is access to the file, this section will explain how to how to read the results of the output value from the screen and onto the screen.

getchar () & putchar () function

int getchar (void) function from the screen to read the next available character, and returns it as an integer.This function in the same time will only read a single character. You can use this method in a loop to read more characters from the screen.

int putchar (int c) function to output characters to the screen, and returns the same character.This function in the same time will output a single character. You can use this method in a loop to output a plurality of characters on the screen.

Consider the following examples:

#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 waits for you to enter some text, when you enter a text and press the Enter key, the program will continue and will read a single character, is shown below:

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

gets () & puts () function

char * gets (char * s) function reads a line from stdininto the buffer pointed to bys,until a terminator or EOF.

int puts (const char * s) function to string s and a trailing newline character is written 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 will continue and read an entire line until the end of the line, it is shown below:

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

scanf () and printf () function

int scanf (const char * format, ...) function reads input from the standard input stream stdin,according toformatand provide input to the browser.

int printf (const char * format, ...) function to write the output to the standard output stream stdout,and produces output according to format.

format may be a simple constant string, but you can specify% s,% d,% c ,% f or the like to read the output string, integer, floating point or character.There are many other formatting options are available, depending on the needs. For full details, you can view these functions reference manual. Let's look at the following simple examples to deepen understanding:

#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 waits for you to enter some text, when you enter a text and press the Enter key, the program will continue and read the input, is shown below:

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

Here, it should be noted that, scanf () format and look forward to the input you give same as% d% s, which means that you must provide a valid input, such as "string integer", if you provide a "string string "or" integer integer ", it would be considered erroneous input. In addition, when reading the string, as long as the encounter a space, scanf () stops reading, so "this is test" for scanf () is three strings.