Programming in C

During my interview preparation, I had to learn the basics of datastructures and algorithms. I soon realized that Ruby makes things implicit by doing some work for the developers. For instance, we don't need to increment the index when looping through an array. In C, we have to be explicit and the code resembles more like the pseudo-code in the algorithm books. In fact, The Algorithm Design Manual by Steven S. Skiena uses C for implementing the algorithms. This is one of the best selling algorithm book on Amazon. In order to understand the code examples from that book, I decided to learn C. My examples are based on the book The C Programming Language by Kernighan and Ritchie. Here is the Hello World in C.

Hello World

#include <stdio.h>

int main()
{
  printf("Hello World!");
  printf("\n");
}

On Mac, if you have XCode with command line tools installed, you will have GCC compiler. You can compile this by:

cc hello.c

This will generate a.out, which can be run:

./a.out

While Loop

We can write a simple program to count using the while loop.

#include <stdio.h>

int main()
{
  int count;

  count = 0;

  while (count < 3) {
    printf("%d\t\n", count);
    count = count + 1;    
  }
}

For Loop

Instead of while we can use the for loop to count.

#include <stdio.h>

int main()
{
  int count;

  for(count = 0; count < 3; count++) {
    printf("%d \n", count);
  }
}

You can see the increment operator ++ that increments the count by one in the for statement.

Symbolic Constant

We can define symbolic constants as shown in this example:

#include <stdio.h>

#define LOWER 0
#define UPPER 3

int main()
{
  int count;

  for(count = LOWER; count <= UPPER; count++) {
    printf("%d \n", count);
  }
}

Accumulator

Let's write a simple accumulator that sums up a series of numbers.

#include <stdio.h>

int main()
{
  int count, i;

  count = 0;

  for(i = 1; i <= 3; i++) {
    count += i;
  }
  printf("%d \n", count);
}

This adds the numbers from 1 to 3.

Reading and Writing

We can read from the standard input and write to standard output.

#include <stdio.h>

int main()
{
  int c;

  c = getchar();
  while (c != EOF) {
    putchar(c);
    c = getchar();
  }
}

On Mac, EOF is Control+D.

Count Characters

Let's count the number of characters entered by a user.

#include <stdio.h>

int main()
{
  long count;

  count = 0;
  while (getchar() != EOF)
    ++count;
  printf("%ld", count);
}

Count using for Loop

We can rewrite the above example to use for instead of while loop.

#include <stdio.h>

int main()
{
  double count;

  for (count = 0; getchar() != EOF; ++count)
    ;
  printf("%.0f\n", count);
}

Notice the empty statement denoted by the semicolon after the for statement. Because all the work is done in the for statement.

Count Lines

We can count the number of lines in the user input.

#include <stdio.h>

int main()
{
  int character, count;

  count = 0;
  while ((character = getchar()) != EOF)
    if (character == '\n')
      ++count;
  printf("%d\n", count);
}

What is the point of all these examples? You can learn the programming basics by paying attention to the programming constructs in C. Notice that we have loop variables that are initialized before we enter the loop. We also need a way to exit out of the loop. If we don't, we will end up in an infinite loop. In the loop, we need to get closer to loop termination condition in each cycle of the loop. As you begin to see these patterns, it helps you to solve coding problems when someone throws a problem at you during the interview.


Related Articles


Software Compatibility Best Practices

I spoke to some of the most talented and experienced software developers. I have created a guide that is filled with valuable insights and actionable ideas to boost developer productivity.

You will gain a better understanding of what's working well for other developers and how they address the software compatibility problems.

Get the Guide Now