Interesting things in C Language _ bt_c_code _ chaotic programming code analysis _ 3

Source: Internet
Author: User

I haven't read books about C for a long time. I asked for help from csdn two days ago. Could you please introduce a book that describes the Runtime Library of Windows C language.

The content of the book requires that you be able to introduce the implementation methods of C language library functions, such as functions such as printf, or how functions such as fopen are implemented. As a result, many heroes did not understand what I meant,

I am introducing a very classic book: WindowsProgramI would like to express my gratitude to those heroes for the design and Windows core programming books. I don't know the hero in the garden

Does anyone know about this book? If you have any, please kindly advise. Thank you first.

I happened to see some C language library functions in Windows a few days ago.Source codeFirst, paste a piece of printfCodeLet's take a look. The source code ownership belongs to Microsoft. Here I just

In order to post it for your appreciation, let's see how simple the original printf function can be.

/***** Printf. c-print formatted ** copyright (c) 1985-1997, Microsoft Corporation. all rights reserved. ** purpose: * defines printf () -Print formatted data ************************************ **************************************** * ***/# include <cruntime. h> # include <stdio. h> # include <dbgint. h> # include <stdarg. h> # include <file2.h> # include <internal. h> # include <mtdll. h>/*** int printf (format ,...) -Pr Int formatted data ** purpose: * prints formatted data on stdout using the format string to * format data and getting as your arguments as called for * uses temporary buffering to improve efficiency. * _ output does the real work here ** entry: * char * format-format string to control data format/number of arguments * followed by list of arguments, number and type controlled by * Format String ** Exit: * return S number of characters printed ** exceptions: **************************************** **************************************** /INT _ cdecl printf (const char * format ,...) /** stdout 'print ', 'F' ormatted */{va_list Arglist; int buffing; int retval; va_start (Arglist, format); _ asserte (format! = NULL); _ lock_str2 (1, stdout); buffing = _ stbuf (stdout); retval = _ output (stdout, format, Arglist); _ ftbuf (buffing, stdout ); _ unlock_str2 (1, stdout); Return (retval );}

For the first time I read Microsoft code in windows, I had to say to Microsoft: the old man is like this. First, the code is very streamlined, and secondly, the style is very good.

A piece of code to be analyzed is a world of difference. I have been influenced by the Linux camp for a while, and I have always felt that Microsoft Windows is not good, and I even started to consider windows from my own

The computer was uninstalled and later came back for various reasons (mainly Step 7/rslogix and other industrial control software do not support Linux. Now I find that my previous views are absolutely biased.

Microsoft has made great contributions to the entire computer. Let's take a look at today's code:

 
Int I; Main () {for (; I ["] <I; ++ I) {-- I;}"]; read ('-'-'-', I ++ "Hell \ O, world! \ N ", '/');} Read (J, I, P) {write (J/P + P, I --- J, i/I );}

The purpose of this Code is to output: Hello, world!

The analysis code is still performed according to the original routine. The first step is to scale down and get the following code:

 
Int I; Main () {for (; I ["] <I; ++ I) {-- I;}"]; read ('-'-'-', I ++ "Hell \ O, world! \ N ", '/');} Read (J, I, P) {write (J/P + P, I --- J, i/I );}

Next, we will complete some code definitions. Obviously, the above Code is compiled according to the K & rRules. Many syntaxes are not valid in the ansi c standard.

# Include <stdio. h> int I; int main (INT argc, char ** argv) {for (; I ["] <I; ++ I) {-- I;}"]; read ('-', I ++ "Hell \ O, world! \ N ", '/'); Return 0;} int read (Int J, int I, int p) {write (J/P + P, I --- J, I/I );}

After completing the code, you need to explain the following issues:

1. I [STR] ==> STR [I] during parsing, where STR is a string literal.

2. the string literal value actually returns a pointer, that is, the return value type of "str" is char *

3. The '-' expression is actually => ('-')-('-'); that is, the return value of the expression is 0.

4. The '/' expression is actually => ('/')/('/'), that is, the return value of the expression is 1.

5. The last one to be noted is the write function. I do not know the usage of this function, but it can be Google or Baidu. Here is an excerpt from Baidu Encyclopedia:

Int write (INT Handel, void * Buf, int nbyte); Handel is the file descriptor; Buf is the buffer you specify, that is, pointer, pointing to a memory unit; nbyte indicates the number of bytes to write to the file. Return Value: number of bytes written to the file (successful);-1 (error)

The stdout definition is involved here. It is unnecessary to estimate the value of this macro definition. In addition, the UNIX-based method is used to process the device: Everything is a file. (No one has defined it. I understand it myself ).

Just like object-oriented: Everything is an object.

6. Here is another question to be explained:

I ++ "Hell \ O, world! \ N"

And

I --- J;

The parsing in this place involves the greedy method in the C language compilation and parsing process. If you are interested in the specific content, please refer to the books related to the compilation principles.ArticleWrite well, compile

So ++ and --- will be interpreted as (operand ++) + and (operand --)-during parsing --)-.

For 6th questions, go to the chinaunix forum.PmerofcDaxia said that the expression is incorrect. I have never understood it. I am also sorry to ask. I forgot the link to the original article. It was a blog post written by a girl,

Then there was a problem, and I forgot the link. I don't know if the hero in the garden can explain it.

I am a little annoyed and very annoyed. I have written so much and worried about some things ......................

Some deputies to the National People's Congress once said that they do not support rural children going to college. Many people disagree. In fact, there are still some things that need to be dialectical. I think it is still a bit reasonable (in order not to be misunderstood, I am from the countryside)

From my own experiences, it is still worth thinking about.

Here is a private question: I don't know if garbageman is in chinaunix.PmerofcMan, Because I searchPmerofcThe link to his article is "C Jiedu".

If yes, please reply to Garbageman and thank you first.

Forget it. Get ready to go to bed ........

 



Related Article

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.