Learning C programming again...
Tags: c, programming, beginners
Published: 2023-08-12 10:10:43

I am revisiting C programming after nearly 30 years. 

I first studied [C programming language]( https://en.wikipedia.org/wiki/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](https://en.wikipedia.org/wiki/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](https://gist.github.com/prsaya/97b57414a33c2c15bc4f3392a8c6aeae). 

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");
}
```

**1.2 Variables and Arithmetic Expressions**
_Exercise 1-3_ Modify the temperature conversion program to print a heading above the table. [Solution](https://gist.github.com/prsaya/97b57414a33c2c15bc4f3392a8c6aeae#file-ex_1_3-c)

**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));
}
```

**1.5 Character Input and Output**
_Exercise 1-8_ Write a program to count blanks, tabs and newlines. [Solution](https://gist.github.com/prsaya/97b57414a33c2c15bc4f3392a8c6aeae#file-ex_1_8-c)



**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](https://gist.github.com/prsaya/97b57414a33c2c15bc4f3392a8c6aeae#file-ex_1_13-c)



**1.7 Functions**
_Exercise 1-15_ Rewrite the temperature conversion program of Section 1.2 to use a function for conversion. [Solution](https://gist.github.com/prsaya/97b57414a33c2c15bc4f3392a8c6aeae#file-ex_1_15-c)


**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];
}
```

**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](https://gist.github.com/prsaya/97b57414a33c2c15bc4f3392a8c6aeae#file-ex_1_23-c)


---