kernighan-and-ritchie

How do I complete K&R Exercise 2-4?

£可爱£侵袭症+ 提交于 2019-11-30 08:52:43
问题 I'm learning how to write programs in C using the k&r book (The C Programming Language) and I have a problem with one of the exercises. It's asking me to detect and remove a character in string s1, which matches any characters in the string s2. So, say s1 = "A"; And s2 = "AABAACAADAAE" I want it to return "BCDE" I know I'm on the right path towards it, i just don't know how to design programs very well, could you give me any additional tips. I tried to read about the binary search tree

K&R Exercise 1.16 - Limitation on line length

假如想象 提交于 2019-11-30 08:34:13
I'm learning C from K&R's " The C Programming Language " book. I'm doing the exercises specified in the book. I'm on exercise number 1.16, but I don't understand it. Exercise 1.16: Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text. My questions: "...as much as possible of the text..." - is there some limitation on string length? Maybe in standard headers there's a variable with the max allowed value of string length? "...the length of arbitrarily long input lines..." - but in the code

K&R Exercise 1.16 - Limitation on line length

断了今生、忘了曾经 提交于 2019-11-29 11:58:19
问题 I'm learning C from K&R's "The C Programming Language" book. I'm doing the exercises specified in the book. I'm on exercise number 1.16, but I don't understand it. Exercise 1.16: Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text. My questions: "...as much as possible of the text..." - is there some limitation on string length? Maybe in standard headers there's a variable with the max

How do I complete K&R Exercise 2-4?

我们两清 提交于 2019-11-29 08:46:56
I'm learning how to write programs in C using the k&r book (The C Programming Language) and I have a problem with one of the exercises. It's asking me to detect and remove a character in string s1, which matches any characters in the string s2. So, say s1 = "A"; And s2 = "AABAACAADAAE" I want it to return "BCDE" I know I'm on the right path towards it, i just don't know how to design programs very well, could you give me any additional tips. I tried to read about the binary search tree algorithm, but felt it was a little too advanced for this mundane task. Thanks everyone! /* An alternate

Where does `getchar()` store the user input?

喜夏-厌秋 提交于 2019-11-29 02:23:35
I've started reading " The C Programming Language " (K&R) and I have a doubt about the getchar() function. For example this code: #include <stdio.h> main() { int c; c = getchar(); putchar(c); printf("\n"); } Typing toomanychars + CTRL + D (EOF) prints just t . I think that's expected since it's the first character introduced. But then this other piece of code: #include <stdio.h> main() { int c; while((c = getchar()) != EOF) putchar(c); } Typing toomanychars + CTRL + D (EOF) prints toomanychars . My question is, why does this happens if I only have a single char variable? where are the rest of

What's a good example of register variable usage in C?

眉间皱痕 提交于 2019-11-28 17:27:46
问题 I'm reading through K&R and came to the small section on register variables, and was wondering if people here have some good examples of this put into practice. From section 4.7 in K&R: The register declaration looks like register int x; register char c; To be clear, I'm just hoping to see some cool code samples. I (am pretty sure that I) understand the subject matter so don't feel the need to type up a verbose explanation (unless you want to). 回答1: There is no good example of register usage

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

可紊 提交于 2019-11-27 18:08:48
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=getchar()) !=EOF && c != '\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i;

Where does `getchar()` store the user input?

泄露秘密 提交于 2019-11-27 16:52:14
问题 I've started reading "The C Programming Language" (K&R) and I have a doubt about the getchar() function. For example this code: #include <stdio.h> main() { int c; c = getchar(); putchar(c); printf("\n"); } Typing toomanychars + CTRL + D (EOF) prints just t . I think that's expected since it's the first character introduced. But then this other piece of code: #include <stdio.h> main() { int c; while((c = getchar()) != EOF) putchar(c); } Typing toomanychars + CTRL + D (EOF) prints toomanychars

Why does “The C Programming Language” book say I must cast malloc?

廉价感情. 提交于 2019-11-27 16:50:51
Today I reached page 167 of The C Programming Language (second edition Brian W. Kernighan & Dennis M. Ritchie) and found that the author says I must cast malloc . Here is the part from the book: 7.8.5 Storage Management The functions malloc and calloc obtain blocks of memory dynamically. void *malloc(size_t n) returns a pointer to n bytes of uninitialized storage, or NULL if the request cannot be satisfied. void *calloc(size_t n, size_t size) returns a pointer to enough free space for an array of n objects of the specified size, or NULL if the request cannot be satisfied. The storage is

Why does “The C Programming Language” book say I must cast malloc?

て烟熏妆下的殇ゞ 提交于 2019-11-27 04:09:43
问题 Today I reached page 167 of The C Programming Language (second edition Brian W. Kernighan & Dennis M. Ritchie) and found that the author says I must cast malloc . Here is the part from the book: 7.8.5 Storage Management The functions malloc and calloc obtain blocks of memory dynamically. void *malloc(size_t n) returns a pointer to n bytes of uninitialized storage, or NULL if the request cannot be satisfied. void *calloc(size_t n, size_t size) returns a pointer to enough free space for an