Interesting things in C Language _ input and output stream in C Language _ continued 1

Source: Internet
Author: User

I caught up with the College Entrance Examination in the past two days. I attended the College Entrance Examination in my house, so I didn't come to the garden for a stroll. Today, my college entrance examination is finished and I am idle.

The last time I wrote some text about the printf () function, I felt that I was not very familiar with the input and output, and I was not very complete. There were a few bugs, so I want to continue with the last topic.

So what are you doing this time? Let's start with the input and output ..................

1. Stream

I don't know why stream is translated into a stream in China, but this translation is quite vivid. We know that C language is growing along with the great system, Unix. in UNIX and its derivative systems,

System resources are viewed as files. C inherits this tradition. When we input characters from the keyboard, C treats the input device (that is, the keyboard) as a file for processing. When we use the input and output functions provided by C development tools

C processes the input and output content as a "stream.

In fact, a stream is an ideal object. The content processed by the input or output device is mapped to this data stream, and C will get the information it needs in this data stream.

2. Files

File: I don't know how to define this term accurately. If we look at most of the information, a file is a collection of 0101 pieces of information stored on the storage media, or it can be considered as a storage area on the storage medium.

As mentioned above, system resources are processed as files in C. Here we mainly discuss device files.

After running C, three device files are automatically opened by default: standard input file, standard output device, and standard error output file.

That is, stdin standard input file (stdin)

Stdout standard output file (stdout)

Stderr standard error output file (stderr)

In some systems, it should be defined as follows: # define stdin_fileno 0

# Define stdout_fileno 1

# Define stderr_fileno 2

Some systems are defined as follows:

# define stdin (& _ iob [0])
# define stdout (& _ iob [1])
# define stderr (& _ iob [2])

3. buffered and non-buffered Input Systems

Buffer input system: When C wants to read data streams from the input file (here the value is standard input device, usually the keyboard), only when the user presses enter -- (char) after 13, the content entered by the user can be used by C.

Non-buffered system: When c reads data from an input file such as a data stream, C can be used immediately without waiting for the user to press Enter.

As shown in:

For C, or for execution entities implemented by C, whether it is a buffer or not is determined by the system. Currently, most systems are buffer-type input systems for C.

4. GETC (), putc (), getchar (), putchar ()

It should be noted that the two groups of functions should be effective: GETC () = getchar ()

Putc () = putchar ()

However, their prototype or definition form is different.

First, let's look at GETC () and getchar ()

The function prototype of GETC () in C:

Exp:

Char GETC (File *);

That is to say, if you want to use getch (), you need to pass a pointer to the function.

How is getchar () implemented? There are two implementation methods: function and Macro. Let's talk about macro definition:

Exp:

# Define getchar () GETC (stdin)

Similarly, putc () and putchar (); similarly, putc () requires a file pointer, that is, its function prototype is as follows:

Exp:

Int putc (File *);

Therefore, the macro definition of putchar () is:

Exp:

# Define putchar () putc (stdout)

5. EOF

When getchar () is used, we will read the entered characters from the standard input device one by one, including blank characters, such as spaces, tabs, and carriage returns; you can use an EOF

To determine whether the input is complete, so that you can enter any characters you want to enter.

Exp:

Char chinput;

While (EOF! = (Chinput = getchar ()))

{

Putchar (chinput );

}

When we enter a B c dd

There will be output: A B C DD

6. GETC ()

There are two get () Definitions in VC 6.0. One is a macro and the other is a function.

Macro is defined as follows:

# Define GETC (_ stream) (-- (_ stream)-> _ CNT> = 0? 0xff & * (_ stream)-> _ PTR ++: _ filbuf (_ stream ))

The function is defined as follows:

_ Cribd int _ cdecl GETC (File *);

Each compiler provider in C language has a hidden rule. If an identifier starts with an underscore, such an identifier is usually a predefined macro of the compiler, or a predefined identifier.

Let's look at the macro definition. The macro used here actually uses a predefined function:

_ Cribd int _ cdecl _ filbuf (File *)

From this function, we can see that the _ stream used in the GETC () macro is a predefined identifier of the file pointer type. To understand this macro, we need to know the various fields of the file type;

In fact, the file type is another type of redefinition:

Exp:

Struct _ iobuf

{

Char * _ PTR;
Int _ CNT;
Char * _ base;
Int _ flag;
Int _ file;
Int _ charbuf;
Int _ bufsiz;
Char * _ tmpfname;
};

Typedef _ iobuf file;

We can see from the above that the structure of the file type has a _ CNT domain; then we will parse the macro of GETC (); first, let's look:

-- (_ Stream)-> _ CNT> = 0

In the C language specification, we know that the budget priority is the same as that of the suffix -- and higher than the prefix --. Therefore, every time you execute this macro, is to first judge _ stream-> CNT-1

If the value is greater than or equal to 0, the macro returns: 0xff & * (_ stream)-> _ PTR ++. If the value is less than 0, then this macro returns: _ filbuf (_ stream) when it is greater than or equal

When the value is 0, it indicates that there are text stream characters in the buffer. At this time, the current character is taken out and Its bitwise with 0xff is used as the return value of GETC (_ stream), and the character pointer is removed one bit later.

When the value of _ stream-> CNT-1 is smaller than 0, it indicates that all characters in the buffer have been traversed. However, the content in the buffer still exists, but the pointer pointing to the buffer has been moved to the back of the buffer, so this

You must update the content in the buffer by using the function _ filbuf (_ stream). The function _ filbuf (File *) also assigns a value to each field of _ stream.

Careful friends may find that _ iob [0] In # define stdin (& _ iob [0]) is actually a file-type pointer array, which is referenced: extern file _ iob.

Like va_list, The _ stream identifier is defined by the compiler provider and interpreted by the compiler. Here we will not discuss the pre-defined identifier of this file type.

If the hero needs to make a C compilation tool, you can study it.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.