kernighan-and-ritchie

Recursion Control flow

拜拜、爱过 提交于 2019-12-24 02:20:59
问题 #include<stdio.h> void printd(int n) { if(n/10) printd(n/10); putchar(n%10+'0'); } In the above code consider n as a positive integer and its value be 123. First time,123 is passed to printd (first printd) Second time,12 is passed to printd (second printd) Third time,1 is passed to printd (third printd) and putchar prints 1 Then after the control flows to second printd,what is the value of n? Since it is an automatic variable it's value of 1 in third printd vanishes once the function ends and

Bit invert function in K&R exercise 2-7

送分小仙女□ 提交于 2019-12-23 12:42:06
问题 Exercise 2-7 of The C Programming Language : Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed to 0 and vice versa), leaving the others unchanged. I understood the question like this: I have 182 which is 101(101)10 in binary, the part in parentheses has to be inverted without changing the rest. The return value should be 10101010 then, which is 170 in decimal. Here is my attempt: #include <stdio.h> unsigned int getbits(unsigned

getchar() and conditioning a loop to break with a specific char

我的梦境 提交于 2019-12-23 04:49:07
问题 I am working on the infamous book "Prentice Hall Software Series" and trying out the Code they write and modifying it to learn more about C. I am working with VIM on Fedora 25 in the console. The following code is a quote from the book, I know "int" is missing as well as argc and argv etc. Kernighan and Ritchie - The C Programming Language: Page 20 #include <stdio.h> /* copy input to output; 1st version */ main(){ int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } } With

getop() function K&R book p 78

余生颓废 提交于 2019-12-21 17:23:57
问题 I'm studying K&R book. Currently i'm reading function getop() at p.78. I do understand the code but i need clarifications about 2 things. The code of getop() is as follows: int getch(void); void ungetch(int); /* getop: get next character or numeric operand */ int getop(char s[]) { int i, c; while ((s[0] = c = getch()) == ' ' || c == '\t') ; s[1] = '\0'; if (!isdigit(c) && c != '.') return c; /* not a number */ i = 0; if (isdigit(c)) /* collect integer part */ while (isdigit(s[++i] = c = getch

EOF exercise 1-6 K&R The C programming language

假装没事ソ 提交于 2019-12-19 04:06:02
问题 This is taken directly from the K&R book: The precedence of != is higher than that of = , which means that in the absence of parentheses the relational test != would be done before the assignment = . So the statement c = getchar() != EOF is equivalent to c = (getchar() != EOF) This has the undesired effect of setting c to 0 or 1, depending on whether or not the call of getchar returned end of file. (More on this in Chapter 2.) Exercise 1-6. Verify that the expression getchar() != EOF is 0 or

Hex to Decimal conversion [K&R exercise]

落花浮王杯 提交于 2019-12-18 16:56:52
问题 I'm learning C and I can't figure out one of the K&R exercises, the listing: Exercise 2-3, Write the function htoi(s) , which converts a string of hexadecimal digits (including an optional 0x or 0X ) into its equivalent integer value. The allowable digits are 0 through 9 , a through f and A through F . I suppose I need to do some recursion here, I just guess that I don't know a lot about the numbering types, and their various conversion methodologies, etc. Could someone give me some pointers

“extern” inside a function?

[亡魂溺海] 提交于 2019-12-18 14:24:49
问题 Well, reading "a bit old" book ("The C programming language", second edition, by Dennis Ritchie), I came a cross the following: An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it and I was like - what?! "The variable must also be declared in each function that wants to access it". Then, I was shocked one more time: int max; /* ... */ int main() { extern int max

C : How to simulate an EOF?

元气小坏坏 提交于 2019-12-17 01:02:54
问题 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? 回答1: To enter an EOF, use: ^Z ( Ctrl Z ) in Windows ^D

What are the applications of the ## preprocessor operator and gotchas to consider?

限于喜欢 提交于 2019-12-16 20:03:28
问题 As mentioned in many of my previous questions, I'm working through K&R, and am currently into the preprocessor. One of the more interesting things — something I never knew before from any of my prior attempts to learn C — is the ## preprocessor operator. According to K&R: The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a ## , the parameter is replaced by the actual argument, the ## and

What are the applications of the ## preprocessor operator and gotchas to consider?

烂漫一生 提交于 2019-12-16 20:01:14
问题 As mentioned in many of my previous questions, I'm working through K&R, and am currently into the preprocessor. One of the more interesting things — something I never knew before from any of my prior attempts to learn C — is the ## preprocessor operator. According to K&R: The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a ## , the parameter is replaced by the actual argument, the ## and