Skip to main content

Computer Programming Tutorial - Intermediate C-Like

Charles is a software engineer and college professor interested in technology, medicine, economics, and nutrition.

C Language Keywords, by Dennis Ritchie 1973

C Language Keywords, by Dennis Ritchie 1973

Solving More Puzzles

This article assumes you know something about programming in C-like languages. If that's not the case, then start here and come back when you're ready.

Back so soon? You can already solve most puzzles pretty well with the simple tools from my previous article. You can read most C-like programs (written in C, C++, C#, Java, Cg, and others that borrow heavily from C syntax) and get a gist of what's happening. You can also write simple programs. This article will add some tools to your programming toolbox. That means learning more of the C language keywords pictured above.

More Variables

There are still only three main variable types, but there are subtypes that use different amounts of memory, processing time, and have different limitations. If you want to use ints, floats, and strings your whole programming life then you can, but the following variants can sometimes make things easier.

  • long - A long in C is usually the same as an int but may be larger.
  • short - A short, or "short integer" is just like an int but smaller. It has a narrower range, takes up less memory, and the time to process is complicated. On most modern hardware the time to add two shorts will not be less than the time to add two ints, and it might take a bit longer. Why? It's because hardware is optimized to process ints (when 16-bit computers were more common, ints were 16 bits or two bytes just like a short).
  • double - A double, or "double precision floating point number" is just like a float but may be larger. Just like long is a variant of int, same idea.
  • void - Use void when you want a function to return nothing, or a pointer to be able to point to any type.

Strictly speaking, a C compiler has a lot of leeway about how big these variables are, while other C-like languages define the same types with less variation. For the minimal guarantees in C, see Wikipedia. The version of events I give here is what you'll typically find in C programs written for a modern PC. Integer types can also be signed or unsigned, with the unsigned variant having twice the maximum and always >= 0. Variants of float also differ on how close a positive number can get to zero, or the minimum difference between two numbers. This is called the epsilon. Be aware it exists but don't worry about it for now.

Type NameSize In BytesRange



+/- 32767 (2^15 - 1)



+/- 2147483647 (2^31 - 1)



+/- 2147483647 (2^31 - 1)



+/- 3.4e38



+/- 1.8e308

Double Check

If you ever want to check the size of a type use sizeof(), and if you want to check a bit of code without firing up a compiler, try ideone or codepad.

Here's an example on codepad.

Include Import Using

You may recall my reference to using functions written by other people as a time-saving nightmare. Usually it's just time-saving, and generally a good idea whenever possible. The way you achieve it in C is with a #include directive that you've already seen at the top of example C programs. #include tells C the function declaration (what a function looks like, return type and input variables) so you can use it in your program, and the appropriate function definition (what a function does, inside the function body) can be found in a library or object file when you build an executable.

Other C-Like languages might use #include (Cg), import (Java), or using (C#) for the same effect.

More Functions

As an intermediate level programmer, it's time to get familiar with your language's standard library. In C, this means the hundreds of functions of the C standard library. You should learn these as you need them, but here are some of the more common functions. Don't forget to use #include.

  • putch( char ch ) puts( char *string ) - Write a character or string to the screen.
  • getch() getche() gets() - Get a character or string from the keyboard.
  • strcpy( char *target, char *source ) - Copy a string from source to target.
  • strcmp( char *a, char *b ) - Compare two strings by alphabetical order.
  • malloc( size_t amount ) - Get some memory that must later be sent to free().
  • free( void *p ) - Free memory that was granted with a call to malloc().
  • fopen( char *filename, char *mode ) - Open a file for reading or writing.
  • fread/fwrite( void *data, size_t size, size_t count, FILE *fp ) - Read from or write to a file.
  • fclose( FILE *fp ) - Close a file that was opened with fopen().

A strength and a weakness of C is that none of these functions are part of the C language. There are standard definitions and they are often included with C compilers, but the functions themselves are written in C and you are free to substitute your own versions. This is most often done with malloc() / free().

C implementations of standard library functions are not only widely available, you have choices. Reading the library source and documentation is not a bad way to dive into intermediate programming if you're not sure where to start.

More Loops

The C for loop is the best and most common and, once you understand it, there are two others that are easy to understand too.

Think of a for loop as: for ( Initialize; Test; Advance ) { Body }

Another kind of loop called while just leaves out Initialize and Advance, like so: while ( Test ) { Body }. Yet another variant, the do-while is a while that always performs the work in Body at least once, and looks like this: do { Body } while ( Test );

In any kind of loop, break means the loop is finished, and continue means the current Body is finished so do the next Advance and Test.

More IFs

C and most variants support two additional types of conditional statements. The first is a multi-path if statement called switch. The second is a three argument (ternary) operator designed to assign a variable ?:. Some people say they're confusing, but nothing could be further from the truth. Like anything, they can be used in a confusing way so try to use them in a straightforward manner that takes advantage of their strengths whenever possible.

A switch statement is just a multiple path if that searches a list of constant integer values for equality to an integer expression.

switch( choice )
  case 0:
    printf( "Choice is 0.\n" );
    break;   // break means the switch() is finished
  case 1:
    printf( "Choice is 1.\n" );
    break;   // in C and C++ the break after each case is optional
  case 2:
    printf( "Choice is 2.\n" );
    break;   // in C# it is required, each case must end in break
    printf( "Choice is not 0, 1, 2.\n" );

The ?: operator is known as the ternary operator because it has three parts. It works like this:

int result = Test ? value_if_test_is_true : value_if_test_is_false;

int maximum = 10;

for (int i=0; i<maximum; i++)
  printf( "%d This is%s the last time.\n", i,
           (maximum-1) == i ? "" : " not" );


0 This is not the last time.
1 This is not the last time.
2 This is not the last time.
3 This is not the last time.
4 This is not the last time.
5 This is not the last time.
6 This is not the last time.
7 This is not the last time.
8 This is not the last time.
9 This is the last time.

Integers and Floats in Memory

You're a Programmer Now

When you understand the ideas in this article through reading examples and solving a few puzzles on your own, you may consider yourself a C or C-Like language programmer. Your programming skills will transfer pretty easily to other C-Like languages, with the most difficulty being different styles of I/O (input/output, data from the keyboard, to the screen, to and from files and so forth), and memory management (C uses malloc()/free(), C++ uses new/delete, C# and Java give back memory automatically when it's no longer being used).

There are still inconvenient gaps in your programming knowledge. You don't know about data structures. You lack appropriate caution when comparing floats. You have a limited understanding of how to go about solving puzzles, or comparing two solutions which are also called algorithms. All these are very typical of intermediate programmers, but if you wish to improve your skill there are books, courses, and other resources available to help.

Good luck programmer!

This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.