kr-c

K&R C Exercise Help

瘦欲@ 提交于 2019-12-03 13:55:11
I've been going through the K&R C Programming Language book and I'm stuck on Exercise 2-6 which reads: Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged. I'm having trouble understanding the exact thing they're looking for me to do. I looked at a possible answer here , but I still don't really understand. I think it's the wording that's throwing me off. Can anyone maybe explain what they're looking for me to do in a different way? I'm hoping that different wording will help me understand

What is the purpose of ungetc (or ungetch from K&R)?

末鹿安然 提交于 2019-12-03 12:25:51
问题 Can anyone explain to me the purpose of ungetch? This is from K&R chapter 4 where you create a Reverse Polish Calculator. I've ran the program without the call to ungetch and in my tests it still works the same. int getch(void) /* get a (possibly pushed back) character */ { if (bufp > 0) { return buf[--bufp]; } else { return getchar(); } } void ungetch(int c) /* push character back on input */ { if (bufp >= BUFSIZE) { printf("ungetch: too many characters\n"); } else { buf[bufp++] = c; } } (I

What is the purpose of this line? (Function declaration)

人走茶凉 提交于 2019-12-01 23:28:00
问题 I'm working through K & R to learn programming. Going well so far, but I'm unclear about the role of a line of code from section 1.8 (functions). In section 1.8, the authors show you how to create a function to raise one integer to the power of another integer. I've pasted the code below, as it was written in the book. Everything outputs fine. But I don't know why they've included this line at the top: int power(int m, int n); The book doesn't mention it, apart from saying that the program

At least the first 31 or 63 characters of an internal name are significant? [closed]

一个人想着一个人 提交于 2019-12-01 01:43:25
Here's a direct quote from the Book (K&R, 2nd ed, p. 35): "At least the first 31 characters of an internal name are significant. For function names and external variables, the number may be less than 31, because external names may be used by assemblers and loaders over which the language has no control. For external names, the standard guarantees only for 6 characters and a single case." And in C99 there is no length limitation on its internal names, but only the first 63 are guaranteed to be significant (§5.2.4.1 Translation Limits). My question is why are these limits specifically 31 or 63 ?

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

How to convert a K&R function declaration to an ANSI function declaration automatically?

自作多情 提交于 2019-11-28 14:05:33
// K&R syntax int foo(a, p) int a; char *p; { return 0; } // ANSI syntax int foo(int a, char *p) { return 0; } As you see, in K&R style, the types of variables are declared in new lines instead of in the braces. How to convert a K&R function declaration to an ANSI function declaration automatically? Does anybody know such an easy-to-use tool in Linux? You can use cproto or protoize (part of GCC) to generate function prototypes or convert old style (K&R) functions to ANSI format. Since You wanna convert a multiline string, you chould consider perl you have void old_style( c , a ) char c; int a;

(K&R) At least the first 31 characters of an internal name are significant?

非 Y 不嫁゛ 提交于 2019-11-27 14:05:21
问题 When taken literally, it makes sense, but what exactly does it mean to be a significant character of a variable name? I'm a beginning learner of C using K&R. Here's a direct quote from the book: "At least the first 31 characters of an internal name are significant. For function names and external variables, the number may be less than 31, because external names may be used by assemblers and loaders over which the language has no control. For external names, the standard guarantees only for 6

How to convert a K&R function declaration to an ANSI function declaration automatically?

女生的网名这么多〃 提交于 2019-11-27 08:06:22
问题 // K&R syntax int foo(a, p) int a; char *p; { return 0; } // ANSI syntax int foo(int a, char *p) { return 0; } As you see, in K&R style, the types of variables are declared in new lines instead of in the braces. How to convert a K&R function declaration to an ANSI function declaration automatically? Does anybody know such an easy-to-use tool in Linux? 回答1: You can use cproto or protoize (part of GCC) to generate function prototypes or convert old style (K&R) functions to ANSI format. 回答2:

What are the major differences between ANSI C and K&R C?

折月煮酒 提交于 2019-11-27 00:58:56
The Wikipedia article on ANSI C says: One of the aims of the ANSI C standardization process was to produce a superset of K&R C (the first published standard), incorporating many of the unofficial features subsequently introduced. However, the standards committee also included several new features, such as function prototypes (borrowed from the C++ programming language), and a more capable preprocessor. The syntax for parameter declarations was also changed to reflect the C++ style. That makes me think that there are differences. However, I didn't see a comparison between K&R C and ANSI C. Is

What are the major differences between ANSI C and K&R C?

佐手、 提交于 2019-11-26 09:35:18
问题 The Wikipedia article on ANSI C says: One of the aims of the ANSI C standardization process was to produce a superset of K&R C (the first published standard), incorporating many of the unofficial features subsequently introduced. However, the standards committee also included several new features, such as function prototypes (borrowed from the C++ programming language), and a more capable preprocessor. The syntax for parameter declarations was also changed to reflect the C++ style. That makes