/C-exercises

Solutions to exercises in "The C Programming Language, 2nd Edition", by Kernighan B.W. and Ritchie D.M.

Primary LanguageC

C Exercises

Introduction

As I delve into C, the lingua franca of programming, I find that the language is really helping me understand Python—and programming in general—much better. So far, I am really enjoying C!

This repository, a hobby, contains my solutions to the exercises of the classic C book "The C Programming Language, 2nd Edition", by Kernighan B.W. and Ritchie D.M. The solutions only contain code elements studied up to that point in the book. As for commented sections, I am trying to follow the NASA C style guide.

Hopefully, I will also pick up Assembly and Java one day.

How to run the code

On Terminal (UNIX), compile the desired exercise with the GNU compiler:

$ gcc -ansi <filename>.c

And execute using:

$ ./a.out

Example

To run the first exercise, open Terminal and, while in the same folder, digit (omitting the $ prompt):

$ gcc -ansi 1-01.c
$ ./a.out

The first command, gcc -ansi 1.01.c, compiles file 1-01.c according to the ANSI standard, the same used in the book. On macOS, there exists a faster version, cc -ansi 1.01.c. The command generates output a.out in the same folder. a.out is replaced every time a new program is run. To execute it, it is necessary to include ./, which locates the file is in the same folder.

The program:

#include <stdio.h>

main()    /* Print hello, world */
{
    printf("hello, world\n");
}

Prints:

hello, world

Exercises

Chapter 1

Exercise Difficulty Description
1-01 ⭐︎ Run the "hello, world" program on your system.
1-03 ⭐︎ Modify the temperature conversion program to print a heading above the table.
1-04 ⭐︎ Write a program to print the corresponding Celsius to Fahrenheit table.
1-05 ⭐︎ Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.
1-06 ⭐︎ Verify that the expression getchar() != EOF is 0 or 1.
1-07 ⭐︎ Write a program to print the value of EOF (end of file).
1-08 ⭐︎ Write a program to count blanks, tabs, and newlines.
1-09 ⭐︎⭐︎ Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
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.