Mar 1, 2012

K & R C Chapter 1 Exercise Solutions

We have already provided solutions to all the exercises in the book "C Programming Language (2nd Edition)" popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 1 of the book for easy reference.

Chapter 1: A Tutorial Introduction

  1. Exercise 1-1. Run the "hello, world" program on your system. Experiment with leaving out parts of the program to see what error messages you get.
    Solution to Exercise 1-1.
  2. Exercise 1-2. Experiment to find what happens when printf's argument string contains \c, where c is some character not listed above.
    Solution to Exercise 1-2.
  3. Exercise 1-3. Modify the temperature conversion program to print a heading above the table.
    Solution to Exercise 1-3.
  4. Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.
    Solution to Exercise 1-4.
  5. Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.
    Solution to Exercise 1-5.
  6. Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1.
    Solution to Exercise 1-6.
  7. Exercise 1-7. Write a program to print the value of EOF.
    Solution to  Exercise 1-7.

  8. Exercise 1-8. Write a program to count blanks, tabs, and newlines.
    Solution to  Exercise 1-8.
  9. Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
    Solution to  Exercise 1-9.
  10. 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.
    Solution to  Exercise 1-10.
  11. Exercise 1-11. How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?
    Solution to  Exercise 1-11.
  12. Exercise 1-12. Write a program that prints its input one word per line.
    Solution to  Exercise 1-12.
  13. Exercise 1-13. Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
    Solution to  Exercise 1-13.
  14. Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input.
    Solution to  Exercise 1-14.
  15. Exercise 1-15. Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
    Solution to  Exercise 1-15.
  16. Exercise 1-16. Revise the the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.
    Solution to  Exercise 1-16.
  17. Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.
    Solution to  Exercise 1-17.
  18. Exercise 1-18. Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines.
    Solution to  Exercise 1-18.
  19. Exercise 1-19. Write a function reverse(s) that reverses the character string s. Use it to write program that reverse thes its input a line at a time.
    Solution to  Exercise 1-19.
  20. Exercise 1-20. Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?
    Solution to  Exercise 1-20.
  21. Exercise 1-21. Write a program entab that replaces strings of blanks with the minimum number of tabs and blanks to achieve the same spacing. Use the same stops as for detab . When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?
    Solution to  Exercise 1-21.
  22. Exercise 1-22. Write a program to "fold" long input lines into two or more shorter lines after the last non-blank character that occurs before the n -th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
    Solution to  Exercise 1-22.
  23. Exercise 1-23. Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments do not nest.
    Solution to  Exercise 1-23.
  24. Exercise 1-24. Write a program to check a C program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don't forget about quotes, both single and double, escape sequences, and comments. (This program is hard if you do it in full generality.)
    Solution to  Exercise 1-24.
You can purchase the book from here or here.

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!

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

To browse more C Programs visit this link

Feb 27, 2012

C Program to find the size of a given file.

Write a C Program to find the size of a given file.
The function fseek() is used to set the file pointer at the specified position. In general, it is used to move the file pointer to the desired position within the file.
In this program we moved the file pointer to the end of file, and obtain the current position of the file pointer. Function rewind() also moves the file pointer to the beginning of the file, but syntax and number of parameters are different. 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<process.h>
long file_size(FILE *fp)
 {
     long len;
    //move file pointer to end of the file */
    fseek(fp, 0L, 2);
    //Obtain the current position of file pointer
    len = ftell(fp);
    return len;
 }
void main(void)
 {
   FILE *fp;
   char filename[20];
   printf("\nEnter the file name:\n\n");
   scanf("%s",filename);

   fp = fopen(filename, r);
   
   if(fp == NULL)
    {
       printf("Error:  file not found!\n\n");
       exit(0);
    }
  printf("File size of %s is %ld bytes\n\n",filename, file_size(fp));
  fclose(fp);
 }

Read more Similar C Programs
C Basic

C File i/o
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