kernighan-and-ritchie

Subtlety in conversion of characters to integers

不羁岁月 提交于 2019-12-13 03:45:40
问题 Can someone explain clearly what these lines from K&R actually mean: " When a char is converted to an int, can it ever produce a negative integer? The answer varies from machine to machine. The definition of C guarantees that any character in the machine's standard printing character set will never be negative , but arbitrary bit patterns stored in character variables may appear to be negative on some machines,yet positive on others". 回答1: You need to understand several things first. If I

Why is some code from K&R not working in Code:Blocks?

爷,独闯天下 提交于 2019-12-12 21:37:58
问题 Some of the examples in K&R don't work in Code:Blocks when I type them exactly. For example, this program: #include <stdio.h> main() { long nc; nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); } When I type this code and run it, the program either freezes or doesn't do anything when I press enter. The program below does the same thing (count characters in a string) and it works. #include <stdio.h> int main() { char s[1000]; int i; scanf("%s",s); for(i=0; s[i]!='\0'; ++i); printf(

Example 2.1, K&R: Wrong symbolic constants value for LONG_MIN and LONG_MAX?

不想你离开。 提交于 2019-12-12 05:01:55
问题 This is the code which I use to find the symbolic constant values for LONG #include <limits.h> //These header files contains the symbolic constants #include <float.h> //for different datatypes #include <stdio.h> int main(void){ printf("\tMininum numeric value for long type: %ld\n", LONG_MIN); printf("\tMaximum numeric value for long type: %ld\n", LONG_MAX); printf("\tMaximum numeric value for unsigned long type: %lu\n", (unsigned)ULONG_MAX); return 0; } Output which I get is: Mininum numeric

Simple C Program

故事扮演 提交于 2019-12-11 03:57:16
问题 This program is based on the program in K&R in the input/output section #include <stdio.h> main(){ double sum, v; sum = 0; while (scanf("%1f",&v)==1) printf("\t%.2f\n",sum+=v); return 0; } It compiles ok. But when trying to run, from any input the output is "-NAN", presumably NOT A NUMBER. I have no idea why. Any advice would be appreciated. 回答1: The format code is wrong in scanf. It should be %lf (with lower case L), not %1f . while (scanf("%lf",&v)==1) This is because %lf scans for a double

K&R atoi-general memory leak

你说的曾经没有我的故事 提交于 2019-12-11 01:53:56
问题 I am following K&R second edition examples to learn C and coding as I think this is correct way of doing things. Anyhow when I run this program post compilation the program get stuck. I used valgrind for execution of compiled script. #include <ctype.h> int atoi(char s[]) { int i, n, sign; for(i = 0; isspace(s[i]); i++) ; sign = (s[i] == '-')? -1 : 1; if (s[i] == '+'|| s[i] == '-') i++; for (int n = 0; isdigit(s[i]); n++) n = 10 * n + (s[i]-'0'); return sign * n; } int main() { char input[] =

K & R Exercise: My Code Works, But Feels Stinky; Advice for Cleanup?

白昼怎懂夜的黑 提交于 2019-12-10 03:12:31
问题 I'm working on the K&R book. I've read farther ahead than I've done exercises, mostly for lack of time. I'm catching up, and have done almost all the exercises from chapter 1, which is the tutorial. My issue was exercise 1-18. The exercise is to: Write a program to remove trailing blanks and tabs from line of input, and to delete entirely blank lines My code (below) does that, and works. My problem with it is the trim method I implemented. It feels ... wrong ... somehow. Like if I saw similar

Pointer type mismatch warning in example from K&R C [duplicate]

拜拜、爱过 提交于 2019-12-08 17:34:16
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Problem compiling K&R example Lately I have been working my way through the C Programming Language by K&R. In section 5.11 they cover pointers to functions and after typing in their example -- a quicksort implementation where we provide a pointer to the comparison function we want to use -- I'm getting a warning from the compiler: pointer type mismatch in conditional expression. (My compiler is gcc 4.0.1 on OS X

Help With K&Rs Counting Chars Example

£可爱£侵袭症+ 提交于 2019-12-07 13:11:55
问题 I'm working my way through K&R's 2nd edition, and I've been stumped with this seemingly simple example: #include <stdio.h> main(){ double c; for(c = 0; ((getchar() != EOF) && (getchar() != '\n')); ++c) ; printf("%.0f\n",c); } It simply isn't working correctly. I added in the (getchar() != '\n') portion to end the program when I press enter, but that doesn't really help either. Here's some sample output, using the gcc that comes with Mac OSX 10.6 dev tools. pool-000:Desktop user$ ./a.out a 0

Does this small C program satisfy the K&R exercise?

£可爱£侵袭症+ 提交于 2019-12-07 06:09:14
问题 I'm on to K&R's Exercise 1-18 Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. This is what I've came up with so far #include <stdio.h> #define MAXLINE 1000 int getline(char line[], int maxline); void copy(char to[], char from[]); int main () { int len; char line[MAXLINE]; while (getline(line, MAXLINE) > 0) { printf("%s", line); } return 0; } int getline(char s[], int lim) { int c, i, lastNonBlankIndex; lastNonBlankIndex = 0; for

argv pointer to an array of pointers

徘徊边缘 提交于 2019-12-07 03:54:12
问题 I am confused as to how the following passage matches up with the code that follows it: Since argv is a pointer to an array of pointers , we can manipulate the pointer rather than index the array. This next variant is based on incrementing argv, which is a pointer to pointer to char, while argc is counted down: #include <stdio.h> /* echo command-line arguments; 2nd version */ main(int argc, char *argv[]) { while (--argc > 0) printf("%s%s", *++argv, (argc > 1) ? " " : ""); printf("\n"); return