kernighan-and-ritchie

Signal EOF in mac osx terminal

人盡茶涼 提交于 2021-02-07 10:45:25
问题 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, macOS (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

C, error: Conflicting types for getline

吃可爱长大的小学妹 提交于 2021-02-04 19:49:27
问题 Can somebody please take a look at this and tell me what is wrong. I have 3 errors. 1) error: Conflicting types for getline 2) error: too few arguments to function call, expected 3 have 2 3) error: conflicting types for getline . I'm sure I have overlooked something simple but I cannot find my error. Thank you, here is the code... #include <stdio.h> #define MAXLINE 1000 int getline(char line[], int maxline); /*conflicting types for getline*/ void copy(char to[], char from[]); main(){ int len;

Understanding printf better - What does it print with “%c” when the value provided is negative?

两盒软妹~` 提交于 2020-05-09 07:33:47
问题 In Kernighan & Ritchie, it says that "all printable characters are positive when though char datatype being signed or unsigned is machine-dependent." Can somebody explain to me the meaning of this line ? My system has signed chars but even with a negative value say of -90, printf does print a character (even though its not a very familiar character). 回答1: ASCII character set defines codepoints from 0x00 to 0x7F . It doesn't matter if they are represented with unsigned or signed byte values

How can a C compiler be written in C? [duplicate]

我的梦境 提交于 2020-01-22 04:09:05
问题 This question already has answers here : Writing a compiler in its own language (12 answers) Closed 6 years ago . This question may stem from a misunderstanding of compilers on my part, but here goes... One can find the following statement in the preface to the first edition of K&R (page xi): The operating system, the C compiler , and essentially all UNIX applications programs (including all of the software used to prepare this book) are written in C. (my emphasis) Here's what I don't

Using `read` system call on a directory

旧巷老猫 提交于 2020-01-11 09:52:32
问题 I was looking at an example in K&R 2 (8.6 Example - Listing Directories). It is a stripped down version of Linux command ls or Windows' dir . The example shows an implementation of functions like opendir , readdir . I've tried and typed the code word-by-word but it still doesn't work. All it does is that it prints the a dot (for the current directory) and exits. One interesting thing I found in the code (in the implementation of readdir ) was that it was calling the system calls like open and

Error while writing strcat() using pointers

∥☆過路亽.° 提交于 2020-01-11 07:27:14
问题 I am trying to learn C with The C programming Language by K&R . I am trying to write a the strcat() program using pointers. char *strcat (char *s, char *t){ char *d; d = s; while(*s++); s--; while(*s++ = *t++); return d; } int main () { char *name1; char *name2; name1 = "stack" ; name2 = "overflow"; printf("%s %s\n", name1, name2); printf("strcat out : %s", strcat(name1, name2)); return 0; } But I am getting the ouput as stack overflow Segmentation fault Why is it not working ? Can anybody

K&R Exercise 1-20 - Need some clarification

青春壹個敷衍的年華 提交于 2020-01-01 04:37:07
问题 I don't fully understand what the following exercise is asking: "Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?" Could someone clarify the bolded part? 回答1: This exercise is asking you to emulate the behavior of tabs by adding the correct amount of spaces, such that the output is still aligned on tab stops. For example :

Exercise 1-18 in K&R The C Programming Language

陌路散爱 提交于 2019-12-25 06:51:04
问题 This exercise is driving me crazy. Sorry if my programming problems seem petty, but I'm a beginner so please bear with me. The exercise asks for the following: Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. Can anybody tell me why my code doesn't work? I haven't yet tackled the issue of entirely blank lines, but I can't find any reason why my code won't remove trailing blanks and tabs. Here it is: #include <stdio.h> #define

Does specifying the use of void in the declaration of a function that takes no arguments address The Most Vexing Parse?

女生的网名这么多〃 提交于 2019-12-25 06:46:22
问题 Is the Most Vexing Parse rooted in the ambiguity about whether or not to use void as the parameter of a function declaration that takes no arguments? As an example, the following code compiles without error, and runs fine, on both the g++ (v7.2.1) and Xcode (Apple LLVM version 7.0.2 (clang-700.1.81)) compilers. #include <iostream> int asdf(void); int asdf(int a) { return a; } int main() { std::cout << asdf(6) << std::endl; //-> 6 return 0; } This goes all the way back to the ANSI-C standard

C programming - K&R example 1.5.2 - modified program not functioning as intended

为君一笑 提交于 2019-12-24 02:23:15
问题 My question is simply "Why does my code on line 10 and 11 not function properly?" The intended purpose of my code is to do exactly as the original K&R code intended, but to NOT count nc whenever (getchar() == '\n') will you please enlighten me? slightly modified K&R code: /** K&R - 1.5.2 Character Counting **/ #include <stdio.h> /* count characters in input; 1st version */ main(){ long nc; nc = 0; while (getchar() != EOF){ if (getchar() != '\n'){ ++nc; } } printf("%ld\n", nc); } I use 64-bit