knr

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

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...

How do I learn C?

I'm interested in learning C better. I have read K & R, and I have even done some simple C extension work in R and Python. What's a worthwhile project idea for doing something more substantial with C? Any good online resources, similar to Dive Into Python? In particular, resources focused on programmers of newer languages who are try...

Basic C Question

I am reading over the KR book, and am a little stuck. What is wrong with the following? void getInput(int* output) { int c, i; for(i=0; (c = getchar()) != '\n'; i++) output[i] = c; // printf("%c", c) prints the c value as expected output[++i] = '\0'; } When I run the program it never gets out of the loop and I have to c...

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

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 ...

C Programming Exercise from the K&R Book.

Any idea why the following code doesn't print the amount of characters in the input? I've taken this straight from the K&R book. Learning C at the moment and this is really confusing, looks to me like I'm never reaching EOF. If that's the case then why would this be used as an example? #include <stdio.h> main() { double nc; fo...

K&R Exercise 2-4

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 rig...

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

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[--bu...

Help with K&R exercise: Multidimensional array into pointer array.

Exercise (5-9): Rewrite the routines day_of_year with pointers instead of indexing. static char daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; /* day_of_year: set day of year from month and day */ int day_of_year(int year, int month, int day) { ...

Pointer type mismatch warning in example from K&R C

Duplicate (or near 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...

k&r C trouble

This is a quick slightly subjective question I need to ask. In order to become a proficient C programmer, I felt I'd learn C from the k&r. I find the book a little easygoing, difficult to understand sometimes but easygoing on the whole. My question here is, do I have to absoultely do all the exercises (even those that stumped me) to b...

Hex to Decimal conversion [K&R exercise]

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 d...

K&R Exercise 2-3 "Hex to int converter" Problem.

The program I wrote works in demographics consisting of only single Hexadecimal values. (Probably not the most elegant solution, but I'm a new programmer) My question is, how would I go about handling of multiple hexadecimal digits, such as 0xAF, or 0xFF, etc? I'm not exactly sure, and I've seemed confuse myself greatly, in the attempt...

K&R: array of character pointers

On pg. 109 of K&R, we see: void writelines(char *lineptr[], int nlines) { while (nlines -- > 0) printf("%s\n", *lineptr++); } I'm confused about what *lineptr++ does exactly. From my understanding, printf requires a char pointer, so we provide that with *lineptr. Then we increment lineptr up to the next char pointer in the array? Is...

how does this code from "The C Programming Language" work?

I'm reading "The C Programming Language (2nd ed.) and near the beginning, it has examples like this: while((c = getchar()) != EOF) if(c == '\n'){ ++n1; I can see how this would work while reading from a file, and I understand this syntax... But this is just reading from the console--how does one signal end of file when ent...

getchar() question

Hi, this is such a newbie question that I'm almost embarrassed to ask it :$ ... but anyway: 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"); } given the entry "toomanyc...

Stack vs. heap storage

This program sorts input lines lexicographically, and I've come to this exercise in K&R: Rewrite readlines to store lines in an array supplied by main, rather than calling alloc to maintain storage. How much faster is the program? The original program is the same, just it used alloc to maintain storage for lines. So I edited it th...

Modify detab to accept list of tab stops

This is my version of detab, from this K&R exercise: Modify detab to accept a list of tab stops as arguments. Use the default tab setting if there are no arguments. #include <stdio.h> #include <stdlib.h> #define TAB_STOP 8 /* replaces tabs from input with the proper amount of blank spots */ int Detab() { int c, x; int co...

I need help understanding what Exercise 5-12 is asking for in the C Programming Language book.

K&R C Programming Language: pg. 105 Extend entab and detab to accept the shorthand entab -m +n to mean tab stops every n columns, starting at column m. entab replaces a number of spaces with a tab character and detab does the opposite. The question I have concerns the tab stops and entab. I figure that for detab it's ...

K&R Exercise 4-6 Problem thinking of a solution.

In k&r we have managed to create an RPN. The exercise now is to: Add commands for handling variables, (It's easy to provide twenty-six variables with single letter names.) Add a variable for the most recently printed value. So this is meant to act somewhat like the Python interpreter, where we can do: >>>5 >>>_ (where _ prints 5) >>>...

Why won't this C program pick up escaped backslashes?

I'm doing K&R's Exercise 1-10 Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way. I came up with this... #include <stdio.h> int main () { int c; printf("\n"); // For readability while (...