《The C programming language》学习笔记

本文是关于《The C Programming Language》的学习笔记,涵盖了变量与表达式、for语句、符号常量、字符输入输出、数组、函数、外部变量与作用域、常量等内容,深入解析了C语言的基本概念和编程技巧。
摘要由CSDN通过智能技术生成
Brian和Dennis的《c程序设计语言》(The C Programming Language)真不愧为c语言方面的经典书籍,薄薄的一本,却让人爱不释手,每次阅读都能有所收获。好记忆不如烂笔头,有些规则、算法还是记录下来比较好,记录下来常常回顾,才是提升编程内功的不二法门。
变量与表达式

1 整数除法操作将执行舍位。

2 浮点常量取的是整数,在书写时最好还是为它加上一个显示的小数点,这样可以强调其浮点信息,便于阅读。

3 在允许使用某种类型变量值的任何场合,都可以使用该类型的更复杂的表达式。

for语句

4 for语句比较适合初始化和增长步长都是单条语句并且逻辑相关的情形,因为它将循环控制语句集中放在一起,且比while语句更紧凑。

符号常量

5 #define指令可以把符号常量定义为一个特点的字符串:

#define 名字   替换文本

6 符号常量名通常用大写字母拼音,这样可以很容易和小写字母拼音的变量名区别

7 #define指令行的末尾没有分号。

字符输入\输出

8 文本流是由多行字符构成的字符序列,而每行字符则由0个或多个字符组成,行末是一个换行符。标准库负责使每个输入输出流都能够遵循这一模型。

9 文件复制10 任何整型( int )也可以用于存储字符型数据。

  void main()
   {
       int c;
       c = getchar(); /*Each time it is called, getchar reads the next input 
                   character from a text stream and returns that as its value*/
       while (c != EOF) {
           putchar(c);
           c = getchar();
       }
   }

11 EOF定义在头文件<stdio.h>中,是一个整型数。其具体数值并不重要,它与任何char型的值都不相同。

12 赋值可以作为更大的表达式的一部分出现。

   #include<stdio.h>
   /* copy input tooutput; 2nd version  */
   void main()
   {
       int c;
       while ((c =getchar()) != EOF)
          putchar(c);
   }

13 for循环改写

    #include <stdio.h>
 
   /* count characters in input; 2nd version */
    void main()
   {
       double nc;
 
       for (nc = 0; gechar() != EOF; ++nc)
           ;
       printf("%.0f\n", nc);/*%.0f强制不打印小数点和小数部分*/
   }

14 行计数

   /*程序虽然简单,但是确实是经典!*/
   #include <stdio.h>
   /* count lines in input */
    void main()
   {
       int c, nl;
       nl = 0;
       while ((c = getchar()) != EOF)
           if (c == '\n')
               ++nl;
       printf("%d\n", nl);
   }
15 单词计数
   #include <stdio.h>
   /*常量,让程序更易读*/
   #define IN   1  /* 在单词内 */
   #define OUT  0  /* 在单词外 */
   /* 统计输入的行数、单词数与字符数 */
   void main()
   {
       int c, nl, nw, nc, state;
       state = OUT;/*state记录程序当前是否位于一个单词之中*/
       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);
   }

16 &&比||高一个优先级。由&&或||连接的表达式由左至右求值,并保证求值过程中只要能够判断最终的结果为真或假,求值就立即终止。

数组
17 统计各个数字、空白符(包括空格、制表符及换行符)及其他字符出现的次数

    #include <stdio.h>
    void 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
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、付费专栏及课程。

余额充值