Chapter 4 K & R study notes

Source: Internet
Author: User
Tags case statement

A comprehensive and systematic explanation of functions. The first thing that comes to my eye is a strange method of function declaration:

Main () {double sum, atof (char []); // function declaration char line [maxline]; int Getline (char line [], int max); sum = 0; while (Getline (line, maxline)> 0) printf ("\ t % G \ n", sum + = atof (line); Return 0 ;}

It declares the self-compiled atof (char []) function in the main function. I don't know why.
According to the instructions in the book, C code cannot define other functions in a function, and the function itself is external. The following code:

#include <stdio.h>int main(void){double a = 3.0, b = 4.0, c;double gou_gu(double x, double y){return sqrt(x*x + y*y);}c = gou_gu(a, b);printf("c = %.2f\n", c);return 0;}

In vs2010, compilation is not supported, but it seems that GCC can.
One notable feature in the book is that you have compiled many functions similar to a function in the standard library. This is a feature that I like very much. It allows us to better understand what C language can do.
This chapter has always centered around an example, which is to compile a calculator program to convert a string of input characters into a mathematical expression against Polish input to obtain the result. Why is this problem so complicated? The reason is that scanf has not been introduced in the book and can only be input using functions similar to getchar. So everything becomes so troublesome. For example:

Int getop (char s []) {int I, C; while (S [0] = c = getch ()) = ''| C = '\ t') // skip the leading space and tab; s [1] =' \ 0 '; // This sentence is used to prevent non-mathematical and mathematical symbols such as letters I = 0; If (! Isdigit (c) & C! = '.' & C! = '-') // If it is not a number, it is regarded as an operator number (except a negative number) and return C; If (C = '-') // The character following the checkcar '-'. If it is a number or a decimal point, it is not a minus sign (if (isdigit (C = getch () | C = '. ') s [++ I] = C; else {If (C! = EOF) ungetch (c); Return '-';} If (isdigit (c) while (isdigit (s [++ I] = c = getch ())); s [I] = '\ 0'; If (C! = EOF) ungetch (c); return number;} int getch (void) {return (bufp> 0 )? Buf [-- bufp]: getchar (); // if there are characters in the buffer, output the characters in the buffer; otherwise, directly read the characters in the input stream} void ungetch (int c) {If (bufp> = bufsize) printf ("ungetch: Too manycharacters \ n"); elsebuf [bufp ++] = C ;}

Get the input through the getop function, and then use the switch-case statement in the main function to determine how to execute it. Because it is a single character, such as a character, so each time you have to read more than one character, if it cannot constitute a number, then it is first overwhelmed by a buffer, each read operation reads data in the buffer and then reads data from the input stream.
In the program, there is another puzzling point: s [1] = '\ 0 ';. The reason for this is that if a letter a is input, the stack s [0] = A; s [1] and the subsequent values are random numbers, as a result, the entire stack does not have '\ 0'. Print it in the main function:
Printf ("error: unknow command % s \ n", S );
The error occurs.

The book also introduces recursive calls. It is a good example to use recursive calls for quick sorting, but it is a bit difficult to use recursive calls to reverse characters in the questions after class. It is of little practical significance.
At last, the content of preprocessing is introduced.
For example, macro is used. This article mainly introduces some easy-to-use errors. For example, when using macros, brackets must be included. Otherwise, if the macro input is x + y, an error occurs when you expand the parameter. Second, when using a macro, do not add the auto-incrementing operator to the parameter. Otherwise, the auto-incrementing parameter is used for multiple times:
# Define max (A, B) (a)> (B )? (A): (B ))
Another example is to use macros to write swap functions:
# Define swap (t, x, y) {T temp = x; X = y; y = temp ;}
T is the original purpose of the type design macro of the parameter. Because the overhead of function calling is relatively large, but macro expansion does not have these overhead, for some repeated functions, designing them into macros can improve program efficiency. However, sometimes macros are prone to errors (for example, in the above example), and sometimes they are not easy to read (you can look at the structure of MFC. Instead, he does not use the inheritance derived system, but uses macros ); therefore, the inventor of C ++ hated macros and used inline functions instead of macros.

The book also mentions the role of static, which is very different from what I usually understand. The book first mentions that it is to hide external objects: global variables or functions modified with static can only be used in this compilation unit. For a local variable modified with static, its value always exists, which I previously understood.

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.