kernighan-and-ritchie

Signal EOF in mac osx terminal

你说的曾经没有我的故事 提交于 2019-11-27 00:38:21
I am stumped by the 1.5.2 question in K&R. I googled for sometime and found out that i have to supply the EOF input after entering the characters. long nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); return 0; I tried both commnad-D and control-D as EOF inputs but nothing worked. Any Idea how to supply the EOF for mac osx? Eric Postpischil By default, OS X (formerly Mac OS X) terminals recognize EOF when Ctrl - D is pressed at the beginning of a line. In detail, the actual operation is that, when Ctrl - D is pressed, all bytes in the terminal’s input buffer are sent to the running

Why do I get a “conflicting types for getline” error when compiling the longest line example in chapter 1 of K&R2?

心已入冬 提交于 2019-11-26 19:18:43
问题 Here is a program I'm trying to run straight from section 1.9 of "The C Programming Language". #include <stdio.h> #define MAXLINE 1000 int getline(char line[], int maxline); void copy(char to[], char from[]); main() { int len; int max; char line[MAXLINE]; char longest[MAXLINE]; max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) printf("%s", longest); return 0; } int getline(char s[], int lim) { int c, i; for (i=0; i<lim-1 && (c

C : How to simulate an EOF?

我们两清 提交于 2019-11-26 10:29:44
I am currently reading K&R's book and typing in the examples from the first section, and there are a couple of examples such as this: while((c = getchar()) != EOF) { //do something } I am testing these examples on a Windows box and thus running the compiled exe files from the cmd prompt. To test the example above, how do I simulate an EOF ? That is, basically how can I make the loop stop when testing the example from the command prompt? Greg Hewgill To enter an EOF, use: ^Z ( Ctrl Z ) in Windows ^D on Unix-like systems Refer EOF Windows: Ctrl+Z Unix :Ctrl+D First, press: Ctrl^X, next: Ctrl^D

Signal EOF in mac osx terminal

旧城冷巷雨未停 提交于 2019-11-26 08:08:33
问题 I am stumped by the 1.5.2 question in K&R. I googled for sometime and found out that i have to supply the EOF input after entering the characters. long nc = 0; while (getchar() != EOF) ++nc; printf(\"%ld\\n\", nc); return 0; I tried both commnad-D and control-D as EOF inputs but nothing worked. Any Idea how to supply the EOF for mac osx? 回答1: By default, OS X (formerly Mac OS X) terminals recognize EOF when Ctrl - D is pressed at the beginning of a line. In detail, the actual operation is

Alternative (K&R) C syntax for function declaration versus prototypes

北慕城南 提交于 2019-11-25 21:56:05
问题 What is useful about this C syntax — using \'K&R\' style function declarations? int func (p, p2) void* p; int p2; { return 0; } I was able to write this in Visual Studios 2010beta // yes, the arguments are flipped void f() { void* v = 0; func(5, v); } I don\'t understand. What\'s the point of this syntax? I can write: int func (p, p2) int p2; { return 0; } // and write int func (p, p2) { return 0; } The only thing it seems to specify is how many parameters it uses and the return type. I guess