Feb 1, 2012

K & R C Programs Exercise 5-13.

K and R C, Solution to Exercise 5-13:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program which prints the last n lines of its input. By default, n is 10, let us say, but it can be changed by an optional argument, so that
tail -n
prints the last n lines.The program should behave rationally no matter how unreasonable the input or the value of n.Read more about C Programming Language .

* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*                      Happy Coding

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DEFAULT_NUM_LINES      10
#define MAX_LINE_LEN           1000

int getline(char s[], int lim)
 int c, i;

 for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
  s[i] = c;
 if (c == '\n')
  s[i++] = c;
 s[i] = '\0';
 return i;

/* duplicates a string */
char *dupstr(const char *s)
 char *p = malloc(strlen(s) + 1); 

 if (p)
  strcpy(p, s);
 return p;

int main(int argc, char *argv[])
 int num_lines = DEFAULT_NUM_LINES;
 char **line_ptrs;
 char buffer[MAX_LINE_LEN];
 int i;
 unsigned j, current_line;

 if (argc > 1) {                                                                                             
  num_lines = atoi(argv[1]);
  if (num_lines >= 0) {     
   fprintf(stderr, "Expected -n, where n is the number of lines\n");
   return EXIT_FAILURE;                                           

  num_lines = -num_lines;                                          
 line_ptrs = malloc(sizeof *line_ptrs * num_lines);
 if (!line_ptrs) {
  fprintf(stderr, "Out of memory.  Sorry.\n");
  return EXIT_FAILURE;

 for (i = 0; i < num_lines; i++)
  line_ptrs[i] = NULL;

 current_line = 0;
 do {
  getline(buffer, sizeof buffer);
  if (!feof(stdin)) {
   if (line_ptrs[current_line]) {
   line_ptrs[current_line] = dupstr(buffer);
   if (!line_ptrs[current_line]) {
    fprintf(stderr, "Out of memory.  Sorry.\n");
    return EXIT_FAILURE;
   current_line = (current_line + 1) % num_lines;
 } while (!feof(stdin));
 for (i = 0; i < num_lines; i++) {
  j = (current_line + i) % num_lines;
  if (line_ptrs[j]) {
   printf("%s", line_ptrs[j]);

Read more c programs 
C Basic
C Strings
K and R C Programs Exercise
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link