Consumer ElectronicsComputersCell PhonesHome Theater & AudioGraphic Design & Video EditingInternetIndustrial Technology

Computer Programming - With C-Like Languages

Updated on October 1, 2016
charles griffiths profile image

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 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.


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: "", "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.


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

#include <stdio.h>

int main( int argc, char *argv[] )
  printf( "Hello World!\n" );

  return 0;

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) and 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, and 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, and 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.


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.

int x = 19, y = 5;

printf( "%d / %d is %d and %d %% %d is %d\n",
       x, y, x/y, x, y, x%y );

printf( "%d * %d + %d is %d\n",
       y, x/y, x%y, x );


19 / 5 is 3 and 19 % 5 is 4

5 * 3 + 4 is 19

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.

int x = 12, y = 4;

if (0 == x % y) printf( "x divides y with no remainder\n" );

if (0 != x % y) printf( "x does not evenly divide y\n" );

if (1 == x % y || 2 == x % y)
  printf( "x %% y is %d (1 or 2)\n", x % y );

if (1 != x % y && 2 != x % y)
  printf( "x %% y is %d (neither 1 nor 2)\n", x % y );


if (0 == x % y)
  printf( "x divides ...
  printf( "x does not ...

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.

for (int i=0; i<10; i++)
  printf( "This is the value of i: %d\n", i );


This is the value of i: 0
This is the value of i: 1
This is the value of i: 2
This is the value of i: 3
This is the value of i: 4
This is the value of i: 5
This is the value of i: 6
This is the value of i: 7
This is the value of i: 8
This is the value of i: 9

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.


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.


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.


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.

Ask questions, learn more, take an advanced programming course. It's all just as easy, and welcome to the world of solving puzzles.


    0 of 8192 characters used
    Post Comment

    • charles griffiths profile image

      charles griffiths 10 months ago

      mahimasharma - Glad you enjoyed it.

    • mahimasharma profile image

      kiyara 10 months ago

      thank you so much for thins amazing tutorial. i have watched everything and explanation is very precise clear and on point.