All Questions
Tagged with kernighan-and-ritchie c
372 questions
3
votes
5
answers
141
views
getting a stream of -1 when I pass EOF in an infinite getchar printf loop
I have just started the K&R book. It introduces a loop using getchar() and putchar() to copy characters from input to output until EOF is reached:
main()
{
int c;
while ((c = getchar()) !=...
0
votes
2
answers
87
views
C Unisgned Short is output correctly with %d but Unsigned Long is not unless using %u: 2-1 K&R C Book
after testing for 2-1 Exercise I found it interesting that the output from printing the value of the unsigned long integer with %d was not correct while using %u it was - the book did not mention %u ...
0
votes
2
answers
109
views
Why do I get a SEGFAULT when I try to sort an array of integers using K & R code examples? [closed]
I'm trying to work my way through The C Programming Language, 2nd Edition, and I can't get my head around exactly what is happening in the following code.
The example from the book works just fine ...
0
votes
1
answer
94
views
How to prevent blank lines and remove extra spaces in C word-per-line output?
I'm reading K&R's The C Programming Language and currently working on Exercise 1-12. The issue I'm facing isn't with completing the exercise itself, but with fixing a bug that has come up as a ...
1
vote
1
answer
102
views
C Programming language ansi c edition line counter [duplicate]
I have the following code from Kernighan & Ritchie's book:
#include <stdio.h>
int main() {
int c, nl;
nl = 0;
while((c = getchar()) != EOF)
if (c == '\n')
++...
0
votes
1
answer
109
views
Where is index i ? - K&R [duplicate]
I am reading the function on page 29 from "C programming" by K&R.
In this example:
int getline(char s[], int lim) {
int c, i;
for (i = 0; i < lim - 1 && (c = getchar())...
0
votes
2
answers
223
views
Triggering EOF From Keyboard Input (Terminal)
I'm a new[er] programmer and going through K&R's "ANSI C" 2nd edition book. The example has the following program that counts characters using getchar() until EOF is reached.
include &...
0
votes
2
answers
91
views
What's wrong with this quick sort?
I tried to adapt this example from K&R (5.11, ANSI edition) to sort an array of ints but it crashes randomly and I can't figure why.
The original version sorted "lines" : char *lineptr[...
0
votes
1
answer
114
views
Why does this C word histogram program print an overflow of 0?
I am studying from the K&R C Programming Edition and one of the exercises (1-13) is to print a word length histogram which I mostly succeeded in doing. However, there is one issue.
if (nc < ...
0
votes
1
answer
460
views
K&R C Programming Language - Reverse Polish Calculator
I'm relatively new to C (but decently well versed in programming in general) and I've been trying to self study via the K&R C book. I've found myself stumped when trying to understand how the ...
1
vote
2
answers
404
views
K&R exercise 1-21
Exercise 1-21. 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 as for detab. When either a tab or a ...
-1
votes
4
answers
240
views
Replacing spaces for TAB with (or without) arrays
I tried to follow some advice and started reading C programming language book.
In the book there are several exercise and end of each chapter.
I've just did the following exercise:
Write a program ...
1
vote
2
answers
130
views
Why do I need to explicitly putchar (' ') in this code?
I am working through Kernighan & Ritchie and have got to exercise 1.9. In fact I wrote some code which appears to solve the exercise, and I have tested it on Windows (with Git Bash and gcc) and ...
0
votes
1
answer
176
views
Are typedef declarations for bare function types (ie: not function pointers) legal in C89/C90?
Let's consider the following code:
#include <stdio.h>
#include <string.h>
typedef int INTFUNC(char *, char *);
INTFUNC lencmp;
int main(void) {
printf("%d\n", lencmp("...
1
vote
2
answers
223
views
Is it really legal for K&R to write "PFI strcmp, numcmp;" where PFI is typedef'd as "int (*)(char *, char *)"?
In The C Programming Language (Kernighan and Ritchie, 2nd ed) on p147, the authors show a typedef declaration
typedef int (*PFI)(char *, char *);
(PFI stands for "pointer to function returning ...