DEV Community

Prasad Saya
Prasad Saya

Posted on

Learning C programming again...

I am revisiting C programming after nearly 30 years.

I first studied C programming language around 1991-1992. I was working as a programmer at a small software consulting company in Bangalore, India. I was mostly working with COBOL programming. I had taken a few days off from work and enrolled into a training program in Unix and C. The training was conducted by Digital Equipment Corporation (DEC) in India (DEC's Unix flavor was called as Ultrix).

Though I learnt the C, I never got to use it; UNIX in various flavors, yes. Few weeks back I had purchased a copy of the book, C Programming Language (second edition) by Brian W. Kernighan and Dennis M. Ritchie. I am studying the book, trying the code samples and also solving few exercises.

I completed the first chapter titled, CHAPTER 1: A Tutorial Introduction. It is an introductory chapter, with basic programming topics, examples and exercises. The topics in general are: printing hello world, declaring variables and symbolic constants, using arithmetic expressions, using for and while statements, character input and output, arrays and character arrays, functions and arguments, external variables and scope.

I had tried most of the exercises and here are eight of them from various sections in the chapter. The exercise solutions are based upon what I came to know about C from the first chapter of the book.

I have posted the code from couple of exercise solutions below. The remaining are in a GitHub Gist C Programming Exercise Solutions.

I had used the MinGW C compiler on a Windows computer to compile the programs.


Here are the sections and the exercises:

1.1 Getting Started
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.

#include <stdio.h>

/* The Hello world program */
int main() 
{ 
    printf("Hello World!\n");
}
Enter fullscreen mode Exit fullscreen mode

1.2 Variables and Arithmetic Expressions
Exercise 1-3 Modify the temperature conversion program to print a heading above the table. Solution

1.3 The For Statement
Exercise 1-5 Modify the temperature conversion program to print the table in reverse order, fom 300 degrees to 0.

#include <stdio.h>

/* Fahrenheit to celsius table - prints in reverse,
   for the fahrenheit 300 to 0 in steps of 20 */
int main()
{ 
    int fahr;

    printf("Fahrenheit\tCelsius\n");
    for (fahr = 300; fahr >= 0; fahr = fahr - 20)
        printf(" %3d\t\t%6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32));
}
Enter fullscreen mode Exit fullscreen mode

1.5 Character Input and Output
Exercise 1-8 Write a program to count blanks, tabs and newlines. Solution

1.6 Arrays
Exercise 1-13 Write a program to print a histogram of the lenghts of the words in its input. It is easy to draw a histogram with the bars horizontal; a vertical orientation is challenging. Solution

1.7 Functions
Exercise 1-15 Rewrite the temperature conversion program of Section 1.2 to use a function for conversion. Solution

1.9 Character Arrays
Exercise 1-19 Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time.

#include <stdio.h>

#define MAX_LINE_LEN    1000 /* Assume each line is limited to this length */

int get_line(char line[], int maxline);
void reverse(char s[]);

/* Program that reverses its input a line at a time.
   Uses a function reverse(s) that reverses the character string s. */
int main()
{
    char line[MAX_LINE_LEN];

    while((get_line(line, MAX_LINE_LEN)) > 0) {
        reverse(line);
        printf("%s\n", line);
    }

    return 0;
}

/* get_line: read a line into s, return length */
int get_line(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;
        ++i;
    }
    s[i] = '\0';

    return i;
}

/* length: returns the length of string s */
int length(char s[]) 
{
    int i;

    i = 0;
    while (s[i] != '\0')
        ++i;
    return i;
}

/* copy: copy from into to; assume to is big enough */
void copy(char to[], char from[])
{
    int i;

    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

/* reverse: reverses the input string */
void reverse(char s1[])
{
    int i, j, len;

    len = length(s1); /* get length of the string */

    /* make copy of the original string */
    char s2[len];
    copy(s2, s1);

    /* make a reverse of the original string */
    j = 0;
    for (i = len-1; i >= 0; --i, ++j)
        s1[j] = s2[i];
}
Enter fullscreen mode Exit fullscreen mode

1.10 External Variables and Scope
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


Top comments (0)