K&安培; R C语言程序设计1.5.1(文件复制) [英] K&R C Programming Language 1.5.1 (File Copying)

查看:143
本文介绍了K&安培; R C语言程序设计1.5.1(文件复制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我读过一些几个月前又一个众所周知C书(在我的语言),我从来没有learn't这个没什么。 。该K&放的方式; R写入20页3章这是简直太神奇了,当然,我不能指望巨大的解释,但也上升问题

我有一个关于这点1.5.1问题
书上说的(PAG 16):

 的main(){
  INT℃; //< - 这是个问题
  C =的getchar();
  而(C!= EOF){
    的putchar(C);
    C =的getchar();
  }
}


  

[...] char类型是专指用于存储这样的字符
  数据,但任何整数类型都可以使用。我们使用int作为一个微妙但
  重要原因。
   问题是区分输入从末尾
  有效的数据。该解决方案是getchar函数返回一个独特的价值
  当没有更多的输入,这是不能用任何被cinfused一个值
  真正的性格。这个值称为EOF,为文件结束。我们必须
  c声明是一个类型的大到足够多认为的getchar任何值
  回报。我们不能用CHAR由于C必须足够大,持有在EOF
  除了任何可能的字符。因此,我们使用int类型。[...]


在为另一种解释谷歌搜索:


  

EOF是一个特殊的宏重新presenting文件结束(Linux系统:使用CTRL + D上
  键盘来创建此,Windows命令:使用CTRL + Z(可能要
  处于新一行的开头,后跟RETURN)):通常EOF = ​​-1,但
  实现有关。必须是一个值不是一个有效的值
  任何可能的字符。出于这个原因,c是int类型的(不
  焦炭为一个可能期望)。


所以我修改的源从int到char,看看是什么问题,关于采取EOF价值......但没有任何问题。以同样的方式。

我也没undestrood怎么做的getchar需要每一个角色我写的,打印的一切。整型4字节是很长,所以它可以采取一个变量中4个字符。
但我可以把任何数目的字符,它会读取和写入的一切一样。
并用char,发生在同一...
什么真正发生的?在哪里存储的值时,有超过1-4个字符?


解决方案

  

所以我修改的源从int到char,看看是什么问题,
  关于采取EOF价值......但没有任何问题。以同样的方式。


我的发生的工作方式相同。这一切都取决于实际键入字符,即如果它是带符号。还有一个Ç常见问题解答了解这个主题。你更可能看到的错误,如果你的字符是无符号的。


  

该错误可能不被发现很长一段时间,但是,如果是字符
  签署,如果输入的是所有7位字符。


修改


  

最后一个问题是:char类型是一个字节长,int是4字节
  长。因此,焦炭将只需要一个ASCII字符。但是,如果我输入
  堆栈溢出超过1字节长,输出将是堆
  溢出超过1字节长。哪里是粘性溢出超过1字节
  长存储,以及如何做的putchar,把整个字符串


每个字符将被 C 依次存储。所以第一次,的getchar()将返回取值的putchar 将发送它的道路上。然后 T 将沿着等来的。在任何时候将 C 存储多个字符。所以,虽然你给它一个大的字符串,它通过一次吃一​​个字符处理它。

Well, i've read some months ago another "well know" C book(in my language), and i never learn't nothing about this. The way that K&R writes 3 chapters in 20 pages it's simply amazing, and of course that i can't expect huge explanations, but that also rises questions.

I have a question about this point 1.5.1 The book says(pag 16):

main(){
  int c;// <-- Here is the question
  c=getchar();
  while (c != EOF){
    putchar(c);
    c = getchar();
  }
}

[...] The type char is specifically meant for storing such character data, but any integer type can be used. We used int for a subtle but important reason. The problem is distinguishing the end of input from valid data. The solution is that getchar returns a distinctive value when there is no more input, a value that cannot be cinfused with any real character. This value is called EOF, for "end of file". We must declare c to be a type big enought 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.[...]

After searching google for another explanation:

EOF is a special macro representing End Of File (Linux: use CTRL+d on the keyboard to create this, Windows command: use CTRL+z (may have to be at beginning of new line, followed by RETURN)): Often EOF = -1, but implementation dependent. Must be a value that is not a valid value for any possible character. For this reason, c is of type int (not char as one may have expected).

So i modified source from int to char to see what is the problem, about taking EOF values... but there is no problem. Works the same way.

I also didn't undestrood how does getchar takes every character i write, and prints everything. Int type is 4bytes long, so it can take 4 characters inside a variable. But i can put any number of characters, it will read and write everything the same way. And with char, happens the same... What does really happens? Where are the values stored when there are more than 1-4 characters?

解决方案

So i modified source from int to char to see what is the problem, about taking EOF values... but there is no problem. Works the same way

I happens to work the same way. It all depends on the real type of char, i.e. if it's signed or unsigned. There's also a C FAQ about this very subject. You're more likely to see the bug if your chars are unsigned.

The bug can go undetected for a long time, however, if chars are signed and if the input is all 7-bit characters.

EDIT

The last question is: char type is one byte long, and int is 4bytes long. So, char will only take one ascii character. But if i type "stack overflow is over 1byte long", the output will be "stack overflow is over 1byte long". Where is "tack overflow is over 1byte long" stored, and how does putchar, puts an entire string

Each character will be stored by c in turn. So the first time, getchar() will return s, and putchar will send it on its way. Then t will come along and so on. At no point will c store more than one character. So although you feed it a large string, it deals with it by eating one character at a time.

这篇关于K&安培; R C语言程序设计1.5.1(文件复制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆