kernighan-and-ritchie

In a function call, what is the operator, and what are the operands?

吃可爱长大的小学妹 提交于 2019-12-06 17:50:47
问题 I am trying to understand some basics of C. KRC's The C Programming Language says A function call is a postfix expression , called the function designator, followed by parentheses containing a possibly empty, comma-separated list of assignment expressions (Par.A7.17), which constitute the arguments to the function. In a function call, what is the operator, and what are the operands? Is () the operator? Is the function name an operand? Are the arguments inside () operands? Is a function

K&R 2nd Edition, Example 1.9 Character Arrays

纵然是瞬间 提交于 2019-12-06 04:44:25
问题 I have a question in regards to the getline() function and parameter definition in the following code. The code is taken directly from K&R chapter 1.9: "Character arrays". I have reproduced it here verbatim. The issue is that when I compile the program as is I get three errors,(which I have reproduced at the end). When I change the function, and the function parameter definition to get_line() (with an underscore instead of just getline) in the three places where I get the errors, the errors

Help With K&Rs Counting Chars Example

余生颓废 提交于 2019-12-06 00:37: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 pool-000:Desktop user$ ./a.out asdf 2 pool-000:Desktop user$ ./a.out asfasf 3 So something is obviously

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

心已入冬 提交于 2019-12-05 11:43:40
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 (i=0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) { if (c != ' ' && c != '\t') {

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

拈花ヽ惹草 提交于 2019-12-05 03:55:07
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 code in C# in a code review, I'd probably go nuts. (C# being one of my specialties.) Can anyone offer

getop() function K&R book p 78

限于喜欢 提交于 2019-12-04 08:06:32
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())) ; if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = getch())) ; s[i] = '\0';

How to not invoke warning: type specifier missing?

我们两清 提交于 2019-12-04 03:54:37
问题 I am reading "The C programming Language" by Brian W. Kernighan and Dennis M. Ritchie. In chapter 1.2 "Variables and Arithmetic Expressions" they demonstrate a simple Fahrenheit to Celsius converter program. When I compile the program (Terminal.app, macOS Sierra), I get this warning: $ cc FtoC.c -o FtoC.o FtoC.c:5:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] main() ^ 1 warning generated. This is the C program: FtoC.c: 1 #include <stdio.h> 2 3 /* print Fahrenheit

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 does char * argv[] means?

一世执手 提交于 2019-12-03 08:17:11
问题 I'm new to C programming, I encountered a problem. In case of complicated declarations i found this int *daytab[13]; // daytab is an array of 13 pointers to int which means daytab is the name of the array and the name of the array points to the first element of the array. The array name is not compatible with pointer manipulation like daytab++ etc (correct me if I'm wrong). But I found this code written in Dennis Ritchie main(int argc, char * argv[]) { while( --argc > 0 ) printf("%s%s",*+

I'm trying to understand the K&R's exercise 1-21 [duplicate]

ぐ巨炮叔叔 提交于 2019-12-02 23:22:46
问题 This question already has answers here : K&R answer book exercise 1.21 (2 answers) Closed last year . This is the question I am trying to understand: Write a program 'entab' that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. When either a tab or a single blank would suffice to reach a tab stop, which should be given preference? Decoding the question: a . It's a program that injects 'tabs' in the input. b . If a string consists of continuous