The C Programming Language 笔记

回顾一下C语言的经典教材K&R,顺便把读书的笔记记在这里,以便以后需要时,不用再从头翻起。

这本书是2007.3.30网上买的,看了两遍,这次再回顾一遍

 

Introduction:

    C is a general-purpose programming language.It has been closely associated with the Unix system where it was developed, since both the system and most of the programs that run on it are writen in C.

    Many of the important ideas of C stem from the language BCPL and B.

    Similarly, C offers only straightward, single-thread control flow: tests, loops, grouping, and subprograms, but not multiprogramming, parallel operations, synchronization, or coroutines.(C语句仅提供单线程控制流:检查、循环、分组、子程序,不支持多线程、并行操作、同步与协同控制)

    C is independent of any parcular machine architecture.

    C is not a strong-type language, but as it has evolved, its type-checking has been strengthened.

    C, like any other language,has its blemishes.Some of the operators have the wrong precedence; some parts of the syntax could be better.  Nonetheless, C is a expressive programming language.
   

Chapter 1: A Tutorial Intruduction
   

    the C compiler will produce an error message because printf never supplies a newline automatically, so several calls may be used to build up an output line in stages.

    In C, all vars must be defined before they are used, usually at the beginning of the function before any executeable statements.

    Array subscripts always start at zero in C.

    Function definitions can appear in any order, and in one or several source files, although no function can be split between files.

    In C,all function arguments are passed "by value". this mean that the called function is given the values of its different properties rather than the originals. (C语言,传参,值传递)


Chapter 2: Types, Operators, and Expressions


    Variables and constants are the basic data manipulated in a program. Declarations list the variables to be used. Operators specify what is to be done to them. Expressions combine variables and constants to produce new values.

    Variable :  Don't begin variable names with underscore, however, since library routines often use such names. Upper case and lower case letters are distinct.

    Traditional C practice is to use lower case for variables names, and all upper case for symbolic constants.

    At least the first 31 characters of an internal name are significant. For extenal names, the standard guaratnees uniqueness only for 6 characters and a single case.

    short, int, long, signed, unsigned, float, double... Each compiler is free to choose appropriate sizes for its own hardware.

    Constant : As in 12345678L is a long constant writen with a terminal l (or L). Unsigned constants are writen with a terminal U or u.   

    Declarations : All variables must be declared before use.

    External and static variables are initialized to zero by default. Automatic varialbes for which there is no explicit initializer have undefined values.

    Arithmetic Operators :

    The % operator cannot be applied to float or double. The direction of truncation for / and the sign of the result for % are machine-dependent for negative operands, as is the action taken on overflow or underflow.

    Type Conversions : When an operator has operands of different types, they are converted to a common type according to a small number of rules, without losing information. In Section 6 of Appendix A states the conversion rules precisely.

    notice: sometimes, -1L > 1U,because -1L is promoted to unsigned long.

 Bitwise Operators : The shift << and >> perform left and right shifts of their left operand by the number of bit positions given by the right operand, which must be nonnegative.
    Thus x << 2 shifts the value of x left by two positions, filling vacated bits with zero; this is equivalent to multiplication by 4. Right shifting an unsigned quantity always fills vacated bits with zero. Right shifting a signed quantity will fill with sign bits ("arithmetic shift") on some machines and with 0-bits ("logical shift") on others.
   
    Precedence and Order of Evaluation :
    C, like most languages, does not specify the order in which the operands of an operator are evaluated. For example , some statements like :
    x = f() + g();
    printf("%d %d/n", ++n, power(2,n));
    a[i] = i++;
    the order in which expression are evaluated is not specified.
     
     
Chapter 3: Conrol Flow

    The control-flow statements of a language specify the order in which computations are performed.
    There is no semicolon after the right brace that ends a block.
   
    Switch : As a matter of good form, put a break after the last case (the default here) even thought it's logically unnecessary. Some day when another case gets added at the end, this bit of defensive programming will save you.
   
 
Chapter 4: Functions and Program structure

    C has designed to make functions efficient and easy to use; C programs generally consist of many small functions rather than a few big ones.
    
    


    External variables: IF a large number of variables must be shared among functions, external variables are more convenient and efficient that long argument lists. However, this reasioning should be applied with some caution, for it can have a bad effect on program structure and lead to program with too many data connection bewteen functions.
   
    Scope rules: can u answer the questions ?
    1. How are declarations written so that the variables are proprely declared during complation?
    2. How are declarations arranged so that all the pieces will be properly connected when the prgram is loaded?
    3. How are declarations organized so there is only one copy?
    4. How are external variables initialized?

   
    Initialization of an external variable goes only wih the definition.
   
    Static variables: If a function is declarated static, however, its name is invisible outside of the file in which it is declared.
   
    Register variables: The register declaration can only be applied to automatic variables and to the formal parameters of a function.
   
    Initialization :
    In the absence of explicit initialization, external and static variables are guaranteed to be initialized to zero; automatic and register variables have undefined initial values.
    For external and static variables, the initializer must be a constant expression; the initialization is done once, conceptually before the program begins execution. For automatic and register variables, it is done each time the function or block is entered. For automatic and register variable, the initialization is not restricted to being a constant expression: it may be any expression involving previously defined values, even function calls.
    If there are fewer initializers for an array than the number specified, the missing elements will be zero for external, static, and automatic variables.
   
    Recursion : C functions may be used recursively.
   
    Macro Substitution:
    If a # character in the replacement text, the combination will be expanded into a quoted string with the parameter replaed by the actual argument. Such as:
     
    When this is invoked, as in, dprint(x/y);   the macro is expanded into statement
     
    
    The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion. 
     
    
   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The C programming Language By Brian W. Kernighan and Dennis M. Ritchie. Published by Prentice-Hall in 1988 ISBN 0-13-110362-8 (paperback) ISBN 0-13-110370-9 目录结构: Contents Preface Preface to the first edition Introduction Chapter 1: A Tutorial Introduction Getting Started Variables and Arithmetic Expressions The for statement Symbolic Constants Character Input and Output File Copying Character Counting Line Counting Word Counting Arrays Functions Arguments - Call by Value Character Arrays External Variables and Scope Chapter 2: Types, Operators and Expressions Variable Names Data Types and Sizes Constants Declarations Arithmetic Operators Relational and Logical Operators Type Conversions Increment and Decrement Operators Bitwise Operators Assignment Operators and Expressions Conditional Expressions Precedence and Order of Evaluation Chapter 3: Control Flow Statements and Blocks If-Else Else-If Switch Loops - While and For Loops - Do-While Break and Continue Goto and labels Chapter 4: Functions and Program Structure Basics of Functions Functions Returning Non-integers External Variables Scope Rules Header Files Static Variables Register Variables Block Structure Initialization Recursion The C Preprocessor File Inclusion Macro Substitution Conditional Inclusion Chapter 5: Pointers and Arrays Pointers and Addresses Pointers and Function Arguments Pointers and Arrays Address Arithmetic Character Pointers and Functions Pointer Arrays; Pointers to Pointers Multi-dimensional Arrays Initialization of Pointer Arrays Pointers vs. Multi-dimensional Arrays Command-line Arguments Pointers to Functions Complicated Declarations Chapter 6: Structures Basics of Structures Structures and Functions Arrays of Structures Pointers to Structures Self-referential Structures Table Lookup Typedef Unions Bit-fields Chapter 7: Input and Output Standard Input and Output Formatted Output - printf Variable-length Argument Lists Formatted Input - Scanf File Access Error Handling - Stderr and Exit Line Input and Output Miscellaneous Functions String Operations Character Class Testing and Conversion Ungetc Command Execution Storage Management Mathematical Functions Random Number generation Chapter 8: The UNIX System Interface File Descriptors Low Level I/O - Read and Write Open, Creat, Close, Unlink Random Access - Lseek Example - An implementation of Fopen and Getc Example - Listing Directories Example - A Storage Allocator Appendix A: Reference Manual Introduction Lexical Conventions Syntax Notation Meaning of Identifiers Objects and Lvalues Conversions Expressions Declarations Statements External Declarations Scope and Linkage Preprocessor Grammar Appendix B: Standard Library Input and Output: <stdio.h> File Operations Formatted Output Formatted Input Character Input and Output Functions Direct Input and Output Functions File Positioning Functions Error Functions Character Class Tests: <ctype.h> String Functions: <string.h> Mathematical Functions: <math.h> Utility Functions: <stdlib.h> Diagnostics: <assert.h> Variable Argument Lists: <stdarg.h> Non-local Jumps: <setjmp.h> Signals: <signal.h> Date and Time Functions: <time.h> Implementation-defined Limits: <limits.h> and <float.h> Appendix C: Summary of Changes

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值