Updated date:

# Computer Programming Tutorial - With C-Like Languages

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

## Programming Is Just a Way to Solve Puzzles

Computer programming is not supposed to be hard. The idea that we have the technology to make machines run programs is pretty amazing, but you're used to that and you don't have to understand it all the way down to the silicon to start programming.

In this article, I'm going to show you in plain terms what can be done with programs and use C-like languages to demonstrate.

## Variables

A variable is just a piece of the puzzle. Variables store data, and that's all. Here are the three main variable types:

• int - An int or "integer" is used to store a count. How many? The answer's an int. An int can also be used to indicate which item from a list. Some famous ints: -3, -2, -1, 0, 1, 2, 3 (there are more in either direction, you get the idea).
• float - A float or "floating point number" is used to store a measurement. How much? The answer's a float. A float is also used for basic math when you want to store a fraction. Some famous floats: 3.14159 (also known as Pi), 2.71828 (e), 0 (it can also be an int).
• string - A string or "string of characters" is used to store anything you type on a keyboard. Anything written down in any language, descriptions, names, and computer programs are all stored as strings. Use strings when you don't want to do math. Some famous strings: "http://hubpages.com", "Computer Programming", "" (also known as the empty string). In C you can use either "char *" or "char []" as a string, and later languages have a dedicated string type called "String" or "string".

Any of these can be in an array, which is just a list of variables with the same type. In C we use [] to mean an array, and * to tell where an array starts.

There are other types of variables, but they're all variations on int, float, and string. There are also some limitations on these types, such as maximum or minimum values for int and float, or maximum length for strings. Don't worry about it for now.

## Functions

A function is how you add motion to variables. Data just sits around idle until you do something, and functions are how you do it.

When a function gets something done, we either call it a "return value" which means the function gives back a variable to explain what happened, or we call it a "side effect" which means the function changed some other variables in the program, or put something on the screen, or put data in a file.

If you think about a program solving a puzzle, you can think of a function as taking a step toward the solution. Some famous C functions: main, printf, strcat (in more modern languages, the strcat function is replaced by using + on a string).

## Hello World

Look at that, it's a function. Let's break it down.

## int main

This means that the function is called main, and it returns an int. In C, your program always starts with main (which can call other functions). When main hits return your program is finished.

## ( int argc, char *argv[] )

This means that the main function wants two inputs (also called arguments). The first input is an int. With main, this int tells you how many parts are in the second input. The second input is an array of strings (or an array of starting points for arrays of characters).

## printf( "Hello World!\n" );

Another function. This function just puts its input onto the screen. In this case, the string "Hello World!" is put on the screen. The \n means go to the next line.

## return 0;

This means main is finished. It sends an int back to the operating system as promised way back in "int main". A return value of zero in this case means there were no errors and everything is fine.

## Math

Almost all programs do some math. If you don't like doing math, that's fine because you're not supposed to do the math. Your program tells the computer to do math and you just get the answer. I know which way I prefer.

Your basic math operations are supported on ints and floats: + (add), - (subtract), * (multiply), / (divide). Also handy is % (integer remainder) which is the amount left over when you divide one int by another.

## Testing With IF

Besides doing math, this is another way to tell a program to do work for you. You test variables in an if statement, and if the test is true, then your program does some work. Otherwise, the work is skipped.

A test is made up of logic and compares. A compare is just one of: == (equals), != (not equal), > (is greater than), < (is less than), >= (is greater than or equal), or <= (is less than or equal). Logic is usually just && (AND, meaning it's true only if both sides are true), or || (OR, meaning it's true if either side is true). You can also use ! which takes one argument and makes true into false, and false into true, but it's not used that often.

## Testing With Else

In the above examples, the first two tests are exactly the opposite of one another so you could combine the two if statements into an if-else.

If you use one comparison or two, it's usually not important except that you should choose the way that will lead to fewer mistakes. Do it the way that seems obvious and clear.

## Testing With Loops

Looping means you want your program to do almost the same thing over and over again as long as a test is true. There are three different loops in C and modern languages add a few others, but the best and most popular is the for loop.

A for loop has four parts. Let's break it down.

int i=0;

This part runs only once, and is used to give the loop some variables to work with. In this case, a new int variable called i is assigned the value 0.

i<10;

This is the test. If it's true, then the work in the for body (between the {}s) is done. When the test is false, the for loop is finished.

i++

This adds one to i. It happens after every time the work in the for body is done.

{ printf( ... }

This is work that gets done so long as the test is true.

## Finished

Is that all there is? That's the basics of programming, with C as an example.

There are lots of other things that you can use to make programming easier. For example struct can be used to put one or more variables with different types into the same name, and modern variants have something called a class that's just a fancy struct that can include functions.

There are also ways for you to include functions that other people have written which is a real time saver, and also a nightmare because you also include all of their mistakes. But you can learn about time-saving nightmares later. For now, read some C programs. If you see something you don't understand, it's either the name of a function or a bit of programming that you can learn in terms of the basics you just read about.