Exquisiteness对《The C Programming Language》的笔记(13)

The  C Programming Language
  • 书名: The C Programming Language
  • 作者: Brian W. Kernighan/Dennis M. Ritchie
  • 副标题: -
  • 页数: 274
  • 出版社: Prentice Hall
  • 出版年: 1988-4-1
  • 第8页 Preface to the first edition
    C was originally designed for and implemented on the UNIX operating system on the DEC PDP-11, by Dennis Ritchie. The operating system, the C compiler, and essentially all UNIX applications programs (including all of the software used to prepare this book) are written in C. Production compilers also exist for several other machines, including the IBM System/370, the Honeywell 6000, and the Interdata 8/32. C is not tied to any particular hardware or system, however, and it is easy to write programs that will run without change on any machine that supports C.
    引自 Preface to the first edition
    2015-01-08 14:39:15 回应
  • 第10页 Chapter 1 - A Tutorial Introduction

    1.1 解释 #include <stdio.h> main() { printf("hello, world\n"); } Functions, variables Function: main() 这里没有variables

    Functions Normally you are at liberty to give functions whatever names you like, but ``main'' is special - your program begins executing at the beginning of main. This means that every program must have a main somewhere. main will usually call other functions to help perform its job, some that you wrote, and others from libraries that are provided for you.
    引自 Chapter 1 - A Tutorial Introduction

    The standard library Arguments

    2015-01-23 11:22:30 回应
  • 第11页 1.2 Variables and Arithmetic Expressions

    comments, declarations, variables, arithmetic expressions, loops , and formatted output #include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } } The two lines /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */

    Comment comment, which in this case explains briefly what the program does. Any characters between /* and */ are ignored by the compiler; they may be used freely to make a program easier to understand. Declaration A declaration announces the properties of variables; it consists of a name and a list of variables C provides several other data types besides int and float, including char, short, long and double. Arrays, structures, unions, pointers, functions Assignment statements Computation in the temperature conversion program begins with the assignment statements lower = 0; upper = 300; step = 20; which set the variables to their initial values. Individual statements are terminated by semicolons. While loop Each line of the table is computed the same way, so we use a loop that repeats once per output line; this is the purpose of the while loop. The while loop operates as follows: The condition in parentheses is tested. If it is true (fahr is less than or equal to upper), the body of the loop (the three statements enclosed in braces) is executed. Then the condition is re-tested, and if true, the body is executed again. When the test becomes false (fahr exceeds upper) the loop ends, and execution continues at the statement that follows the loop. The body of a while can be one or more statements enclosed in braces, as in the temperature converter, or a single statement without braces. The indentation emphasizes the logical structure of the program. Although C compilers do not care about how a program looks, proper indentation and spacing are critical in making programs easy for people to read. Body of loop Most of the work gets done in the body of the loop. The Celsius temperature is computed and assigned to the variable celsius by the statement celsius = 5 * (fahr-32) / 9; The reason for multiplying by 5 and dividing by 9 instead of just multiplying by 5/9 is that in C, as in many other languages, integer division truncates: any fractional part is discarded. Since 5 and 9 are integers. 5/9 would be truncated to zero and so all the Celsius temperatures would be reported as zero.
    引自 1.2 Variables and Arithmetic Expressions
    2015-01-23 12:21:44 回应
  • 第17页 Chapter 1 - A Tutorial Introduction

    1.4 Symbolic Constants 大写,结尾没有分号,用#define定义 #define name replacement list

    It's bad practice to bury ``magic numbers'' like 300 and 20 in a program; they convey little information to someone who might have to read the program later, and they are hard to change in a systematic way. One way to deal with magic numbers is to give them meaningful names. A #define line defines a symbolic name or symbolic constant to be a particular string of characters... #define name replacement list e.g. #include <stdio.h> #define LOWER 0 /* lower limit of table */ #define UPPER 300 /* upper limit */ #define STEP 20 /* step size */ /* print Fahrenheit-Celsius table */ main() { int fahr; for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); } The quantities LOWER, UPPER and STEP are symbolic constants, not variables, so they do not appear in declarations. Symbolic constant names are conventionally written in upper case so they can ber readily distinguished from lower case variable names. Notice that there is no semicolon at the end of a #define line.
    引自 Chapter 1 - A Tutorial Introduction
    2015-02-02 13:55:16 回应
  • 第18页 Chapter 1 - A Tutorial Introduction

    1.5 Character Input and Output Key words: Standard Library;getchar; putchar;printf

    The model of input and output supported by the standard library is very simple. Text input or output, regardless of where it originates or where it goes to, is dealt with as streams of characters. A text stream is a sequence of characters divided into lines; each line consists of zero or more characters followed by a newline character. It is the responsibility of the library to make each input or output stream confirm this model; the C programmer using the library need not worry about how lines are represented outside the program.
    引自 Chapter 1 - A Tutorial Introduction
    2015-02-03 09:40:40 回应
  • 第19页 Chapter 1 - A Tutorial Introduction

    1.5.1 File Copying EOF (end of file)

    The solution is that getchar returns a distinctive value when there is no more input, a value that cannot be confused with any real character. This value is called EOF, for ``end of file''. We must declare c to be a type big enough to hold any value that getchar returns. We can't use char since c must be big enough to hold EOF in addition to any possible char. Therefore we use int.
    引自 Chapter 1 - A Tutorial Introduction

    这段没理解 " We can't use char since c must be big enough to hold EOF in addition to any possible char. Therefore we use int."??

    EOF is an integer defined in <stdio.h>, but the specific numeric value doesn't matter as long as it is not the same as any char value. By using the symbolic constant, we are assured that nothing in the program depends on the specific numeric value. #include <stdio.h> /* copy input to output; 1st version */ main() { int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } }
    引自 Chapter 1 - A Tutorial Introduction

    这里面为什么还有c=getchar()在最后呢?比较下面这段

    #include <stdio.h> /* copy input to output; 2nd version */ main() { int c; while ((c = getchar()) != EOF) putchar(c); } The parentheses around the assignment, within the condition are necessary. The precedence of != is higher than that of =, which means that in the absence of parentheses the relational test != would be done before the assignment =. So the statement c = getchar() != EOF is equivalent to c = (getchar() != EOF) 20 This has the undesired effect of setting c to 0 or 1, depending on whether or not the call of getchar returned end of file.
    引自 Chapter 1 - A Tutorial Introduction

    先运行 !=, 在运行=

    2015-02-03 09:59:16 回应
  • 第20页 Chapter 1 - A Tutorial Introduction

    1.5.2 Character Counting Keywords: long, double data type long integers are at least 32 bits int is 16 bits, with a maximum value of 32767 The conversion specification %ld tells printf that the corresponding argument is a long integer.

    The statement ++nc; presents a new operator, ++, which means increment by one. You could instead write nc = nc + 1 but ++nc is more concise and often more efficient. There is a corresponding operator -- to decrement by 1. The operators ++ and -- can be either prefix operators (++nc) or postfix operators (nc++); these two forms have different values in expressions, as will be shown in Chapter 2, but ++nc and nc++ both increment nc. For the moment we will will stick to the prefix form. #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); } The statement ++nc; presents a new operator, ++, which means increment by one. You could instead write nc = nc + 1 but ++nc is more concise and often more efficient. There is a corresponding operator -- to decrement by 1. The operators ++ and -- can be either prefix operators (++nc) or postfix operators (nc++); these two forms have different values in expressions, as will be shown in Chapter 2, but ++nc and nc++ both increment nc. For the moment we will will stick to the prefix form.
    引自 Chapter 1 - A Tutorial Introduction

    printf uses %f for both float and double; %.0f suppresses the printing of the decimal point and the fraction part, which is zero.

    The body of this for loop is empty, because all the work is done in the test and increment parts. But the grammatical rules of C require that a for statement have a body. The isolated semicolon, called a null statement, is there to satisfy that requirement. We put it on a separate line to make it visible. #include <stdio.h> /* count characters in input; 2nd version */ main() { double nc; for (nc = 0; gechar() != EOF; ++nc) ; printf("%.0f\n", nc); }
    引自 Chapter 1 - A Tutorial Introduction

    最后一段没看懂

    Before we leave the character counting program, observe that if the input contains no characters, the while or for test fails on the very first call to getchar, and the program produces zero, the right answer. This is important. One of the nice things about while and for is that they test at the top of the loop, before proceeding with the body. If there is nothing to do, nothing is done, even if that means never going through the loop body. Programs should act intelligently when given zero-length input. The while and for statements help ensure that programs do reasonable things with boundary conditions.
    引自 Chapter 1 - A Tutorial Introduction
    2015-02-03 10:28:30 回应
  • 第21页 Chapter 1 - A Tutorial Introduction

    1.5.3 Line Counting Newline, if statement, double equals sign, character constant Newline

    ...the standard library ensures that an input text stream appears as a sequence of lines, each terminated by a newline.
    引自 Chapter 1 - A Tutorial Introduction

    If statement

    The if statement tests the parenthesized condition, and if the condition is true, executes the statement (or group of statements in braces) that follows.
    引自 Chapter 1 - A Tutorial Introduction

    Double equals sign

    The double equals sign == is the C notation for ``is equal to'' (like Pascal's single = or Fortran's .EQ.). This symbol is used to distinguish the equality test from the single = that C uses for assignment.
    引自 Chapter 1 - A Tutorial Introduction

    Character constant

    A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set. This is called a character constant, although it is just another way to write a small integer. So, for example, 'A' is a character constant; in the ASCII character set its value is 65, the internal representation of the character A. Of course, 'A' is to be preferred over 65: its meaning is obvious, and it is independent of a particular character set.
    引自 Chapter 1 - A Tutorial Introduction

    '\n' is a single character

    You should note carefully that '\n' is a single character, and in expressions is just an integer; on the other hand, '\n' is a string constant that happens to contain only one character.
    引自 Chapter 1 - A Tutorial Introduction

    #include <stdio.h> /* count lines in input */ main() { int c, nl; nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl); }
    引自 Chapter 1 - A Tutorial Introduction
    2015-02-03 11:16:10 回应
  • 第22页 Chapter 1 - A Tutorial Introduction

    1.5.4 Word Counting Key words: The operator || means OR operator && for AND; its precedence is just higher than ||

    Expressions connected by && or || are evaluated left to right, and it is guaranteed that evaluation will stop as soon as the truth or falsehood is known. If c is a blank, there is no need to test whether it is a newline or tab, so these tests are not made. This isn't particularly important here, but is significant in more complicated situations, as we will soon see.
    引自 Chapter 1 - A Tutorial Introduction

    Key wrods: IF ELSE

    The example also shows an else, which specifies an alternative action if the condition part of an if statement is false. The general form is if (expression) statement1 else statement2 One and only one of the two statements associated with an if-else is performed. If the expression is true, statement1 is executed; if not, statement2 is executed. Each statement can be a single statement or several in braces. In the word count program, the one after the else is an if that controls two statements in braces.
    引自 Chapter 1 - A Tutorial Introduction

    #include <stdio.h> #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ /* count lines, words, and characters in input */ main() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == '\n') ++nl; if (c == ' ' || c == '\n' || c = '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf("%d %d %d\n", nl, nw, nc); }
    引自 Chapter 1 - A Tutorial Introduction
    2015-02-03 15:43:45 回应
  • 第23页 Chapter 1 - A Tutorial Introduction

    1.6 Arrays 下角标这段明白了

    The declaration int ndigit[10]; declares ndigit to be an array of 10 integers. Array subscripts always start at zero in C, so the elements are ndigit[0], ndigit[1], ..., ndigit[9]. This is reflected in the for loops that initialize and print the array. By definition, chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions. This is natural and convenient; for example c-'0' is an integer expression with a value between 0 and 9 corresponding to the character '0' to '9' stored in c, and thus a valid subscript for the array ndigit.
    引自 Chapter 1 - A Tutorial Introduction

    IF Else if Else这个判断明白

    The pattern if (condition1) statement1 else if (condition2) statement2 ... ... else statementn occurs frequently in programs as a way to express a multi-way decision. The conditions are evaluated in order from the top until some condition is satisfied; at that point the corresponding statement part is executed, and the entire construction is finished. (Any statement can be several statements enclosed in braces.) If none of the conditions is satisfied, the statement after the final else is executed if it is present. If the final else and statement are omitted, as in the word count program, no action takes place. There can be any number of else if(condition) statement groups between the initial if and the final else.
    引自 Chapter 1 - A Tutorial Introduction

    没怎么看懂。ndigit[i] 怎么数的?为什么排进array了?我实在是没看懂。

    #include <stdio.h> /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ++ndigit[c-'0']; else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digits ="); for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); } The output of this program on itself is digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345
    引自 Chapter 1 - A Tutorial Introduction

    2015-02-04 09:47:23 回应
<前页 1 2 后页>

Exquisiteness的其他笔记  · · · · · ·  ( 全部214条 )

英语口语话题王
1
世界上最精彩的电影对白
5
别告诉我你懂PPT
7
OCA Oracle Database 11g SQL Fundamentals I Exam Guide
1
与孩子一起学编程
4
Visual Basic6程序设计与应用教程
7
The Complete Grimm's Fairy Tales
1
beginning sql server 2008 for developers
1
Windows Server 2008 for Dummies
1
Oracle 11g For Dummies
4
Database System Concepts
12
Microsoft SQL Server 2008 for Dummies
38
怦然心動的人生整理魔法
6
Computer Systems(Second Edition)
9
汽车修理工技能
2
世界上最温馨的50道心灵鸡汤
3
Practical Skills in Biomolecular Science
1
统计基础知识与统计实务 第2版
2
Practical Statistics for Field Biology
2
Henderson's Dictionary of Biological Terms
1
世界上最经典的品牌
4
Windows 7 All-in-One For Dummies
2
Readers Digest Write Better Speak Better
1
英语报刊佳作精选
1
Oracle Database 11g A Beginner's Guide
2
Oracle Database 11g初学者指南
11
Biology (5th Edition)
1
菜根谭
2
Microsoft SQL Server 2008 Internals (Pro - Developer)
1
零基础学SQL
7
希腊神话故事
1
21天学通Oracle
13
普斐特英语慢阅读
1
为什么偏偏是英国
2
Going Solo
1
了解英国
3
英语漫谈时事热点
5
World History
1
法国
2
Human Genetics
2
OCP
2
SQL查询凡人入门
4
?????:??????????
3
How to Read Novels Like a Professor
1
神雕侠侣
1
探索数码摄影的奥秘
1
Bioinformatics
2
Biochemistry
2
Ocp
1
Excel实战入门
2
ChemOffice
1
软件测试
2
GRE阅读制胜法则
1
Excel/Access在数据与资料管理中的应用
1
Girl with a Pearl Earring
1
数据库系统导论
1
基督山伯爵(上下)
1
老外最想与你聊的100英语话题
1
探索式软件测试
2