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.

Source

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.

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

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

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.

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 );


Outputs:

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

Else

if (0 == x % y)
  printf( "x divides ...
else
  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 );
}

Outputs:

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.

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.

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

Comments

    0 of 8192 characters used
    Post Comment

    • charles griffiths profile imageAUTHOR

      charles griffiths 

      2 years ago

      mahimasharma - Glad you enjoyed it.

    • mahimasharma profile image

      kiyara 

      2 years ago

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

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, turbofuture.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://turbofuture.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)