The C Programming Language 学习笔记

来源:互联网 发布:雨人手机软件 编辑:程序博客网 时间:2024/06/06 10:29
  • 单引号的字符表示一个整数,该值等于此字符在机器字符集中对应的数值,我们称之为字符常量。但是他只不过是小的整型数的一种写法而已。例如,'A'是一个字符常量,在ASC字符集中其值是65(即‘A’的内部表示值是65),当然,用‘A’比65好,因为‘A’的意义更明确。
  • 习题1-8
    Exercise 1-8 Write a program to count blanks, tabs, and newlines. #include <stdio.h>int main(void){  int blanks, tabs, newlines;  int c;  int done = 0;  int lastchar = 0;  blanks = 0;  tabs = 0;  newlines = 0;  while(done == 0)  {    c = getchar();    if(c == ' ')      ++blanks;    if(c == '\t')      ++tabs;    if(c == '\n')      ++newlines;    if(c == EOF)    {      if(lastchar != '\n')      {        ++newlines; /* this is a bit of a semantic stretch, but it copes                     * with implementations where a text file might not                     * end with a newline. Thanks to Jim Stad for pointing                     * this out.                     */      }      done = 1;    }    lastchar = c;  }  printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines);  return 0;} 
    其中的lastchar != '\n'让我很困惑,注释的意思是如果文件不是以一个newline结束的,那么这行也应该加入到newlines中。但我在ubuntu下的终端下,如果不起新的行,无法通过ctrl + D(windows使用ctrl + z)输入EOF,也就不存在不以‘\n’结束的情况了。但是如果以管道的方式读进去,最后一行就可以是不以‘\n’结束的点击打开链接。为此研究了‘\n’, 具体研究结果参照这里
  • c语言传递参数时采用值传递,要想修改实参,需要传指针。当把数组名用作参数时,传递的数组首元素的位置或地址,并不复制数组本身,在被调用函数中,可以通过数组下标访问或者修改数组元素的值。

原创粉丝点击