Skip to main content

All Questions

Filter by
Sorted by
Tagged with
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()) !=...
saberton's user avatar
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 ...
Giorgio Vitanza's user avatar
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 ...
Mike T.'s user avatar
  • 365
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 ...
Elian Adolfo Argañaraz Velazco's user avatar
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') ++...
Bence Sárközi's user avatar
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())...
wantice's user avatar
  • 11
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 &...
Xejo's user avatar
  • 191
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[...
qkzk's user avatar
  • 297
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 < ...
Sean's user avatar
  • 27
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 ...
Larry Ling's user avatar
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 ...
hansoko's user avatar
  • 389
-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 ...
drain_'s user avatar
  • 53
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 ...
harlandski's user avatar
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("...
Lover of Structure's user avatar
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 ...
Lover of Structure's user avatar

15 30 50 per page
1
2 3 4 5
25