kr-c

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

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

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

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 custom getline() function

Can anyone explain to me why this isn't working? #include <stdio.h> #include <stdlib.h> char *getline(int lim) { char c; int i; char *line; line = malloc(sizeof(char) * lim); i = 0; while((c = getchar()) != '\n' && c != EOF && i < lim-1) { *line = c; line++; i++; } *line = '...

K&R Chapter 1 - Exercise 22 solution, what do you think?

I'm learning C from the k&r as a first language, and I just wanted to ask, if you thought this exercise was being solved the right way, I'm aware that it's probably not as complete as you'd like, but I wanted views, so I'd know I'm learning C right. Thanks /* Exercise 1-22. Write a program to "fold" long input lines into two or * more...

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 Qsort example with Pointers and Arrays confusion.

I find it difficult to understand the following snippet of code. I understand the pointer to function mannerism showed, but where I find confusion is in the indicated lines. void qsort(void **v, int left, int right, int (*comp) (void *, void *)) { int i, last; void swap(int **v, int i, int j); if (left >= right) /* do no...

Writing into c-string

Hi, my code segfaults and I don't know why. 1 #include <stdio.h> 2 3 void overwrite(char str[], char x) { 4 int i; 5 for (i = 0; str[i] != '\0'; i++) 6 str[i] = x; 7 } 8 9 int main(void) { 10 char *s = "abcde"; 11 char x = 'X'; 12 overwrite(s, x); 13 printf("%s\n", s); 14 return 0; 15 } The gdb ...

K&R C Exercise Help

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

How did C look like before I was born?

Here is the question, How did C (K&R C) look like? The question is about the first ten or twenty years of C's life? I know, well I heard them from a prof in my uni, that C didn't have the standard libraries that we get with ANSI C today. They used to write IO routines in wrapped assembly! The second thing is that K&R book, is one the be...

I don't understand itoa() in K&R book

I am reading K so far I'm doing well with it, but there is something in function itoa() which I don't understand. Here in itoa() they say they reverse the numbers themselves. For example 10 is 01 (they reverse the string): void itoa(int n, char s[]) { int i, sign; if ((sign = n) < 0) /* record sign */ n = -n; /* make n p...

How to convert from K&R C to ANSI C?

I am trying to execute following code which is the 1988 entry of Obfuscated C Code Contest. #define _ -F<00||--F-OO--; int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO() { _-_-_-_ _-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-...

correctly declaring the main() function in ANSI C

The C standard say: The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: int main(void) { /* ... */ } or with two parameters (referred to here as argc and argv, though any names may b...

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

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

(K&R) Looking for personal help with material in the 2nd edition of K&R (ANSI C)!

I'm a beginning learner of C currently reading K&R. I was debating whether I should constantly post questions about things that confuse me in the 2nd edition of K&R (ANSI C) or find someone whom I can turn to specifically to ask questions whenever I stumble upon a confusing sentence or paragraph. Finally I have decided to choose the lat...

(K&R) How exactly are data types represented in a computer?

I'm a beginning programmer reading K&R, and I feel as if the book assumes a lot of previous knowledge. One aspect that confuses me is the actual representation, or should I say existence, of variables in memory. What exactly does a data type specify for a variable? I'm not too sure of how to word this question... but I'll ask a few quest...

k&r exercise confusion with bit-operations

The exercise is: 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. My attempt at a solution is: #include <stdio.h> unsigned setbits(unsigned, int, int, unsigned); int main(void) { printf("%u\n", setbits(256, 4, 2, 255)); ...

Learning C (via K&R) using xcode

Hi, I'm learning C with The C Programming Language (K&R). Since I don't particularly want to bob back and forth between a text editor and running gcc, I've decided to use xcode as an IDE. So far, I've been able to follow the book's examples without a problem up until section 1.5.2. When given the valid (?) program... #include <stdio.h...

Mixing data types results in heart output

I was fooling around with one of the sample programs in the K&R, and found that this #include <stdio.h> main() { double nc; for (nc = 0; getchar() != EOF; ++nc) ; printf("%lf\n", nc ); putchar(nc); } produces output that is 3.000000 (which I totally expected) then a new line with a heart on it (which I totall...

K&R Exercise 1-21 - Mental incomprehension.

The "impossible" K&R exercise. "Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. Use the same tab stops, say every n columns. Should n be a variable or a symbolic parameter?" The problem I'm having is, I'm unsure about how to even do this corre...