Beginners C

2017-08-10

C is a pretty old language, now almost entirely rendered obsolete by C++ to the best of my knowledge. My interest in learning C is that it's kinda like an old grandfather. I believe working with C is necessary in order to appreciate what Java, Python, Rust, and so forth, give us. And I'm honestly just plain curious what this old dinosaur felt like to program with "back in the day".

I'm not an expert in C, so please bear that in mind as you read. I simply enjoy writing about what Ive learned and have found it to be a great way to retain information. That and I also think that in order to be happy in life, you've got to give something back to the community that you're a part of. I want to make this a kind of "living article" that I slowly add to, week-by-week, until it is finished.

So without further ado, let's get crackin!

 

Step 1: Installation

The quickest way I've found to install C is to use Minimalist GNU for Windows, or MinGW for short. Go here, click download and select the next download button next to "MinGW - Minimalist GNU for Windows":

 

Next, you need to run the executable you just downloaded. You really just want to select all the default options here so I'm not going to upload images of this in order to save space/time. You just click "Install","Continue", (wait a few seconds), "Continue" again, then you get to pick a few packages. Select the developer toolkit and base package, then click on installation followed by apply:

It'll ask you to apply your changes again, so just go ahead and select "Apply". Once its finished, close the windows. Next, we'll have to update our path environment variable so that it knows about MinGW. You can do this by typing "environment" into your search bar and selecting "Edit the system environment variables". Alternatively you can find this option under your control panel. Click on the "Environment Variables" option when you find the right window, then open up the "Path" system variable:

Edit it by clicking the edit button, then add the MinGW directory:

Click okay, apply all changes, and you're almost done! Lastly, we have to check that it's actually working. Open up the command prompt and type in "gcc -v". If you see something like the below image, you successfully installed an antiquated language! Congratulations! 

Step 2: Hello World and the basics

Thenext thing we'll want to cover is the creation of a basic hello world program. Despite C's reputation as some kind of "monolithic, impossible-to-ever-use-without-breaking-your-computer, dinosaur" reputation, it is not quite as hard as one would think. Create a C file called "tutorial" (Create a text file, then put ".c" at the end of the filename and save it under "all files") and write the following code inside the file:

#include <unistd.h>
#include <stdio.h>

int main()
{
    printf("Hello World!");
    sleep (1);
}

In your command prompt, type "gcc tutorial.c" to compile the program. Then run the resultant executable file from this command. You should see the following, for a single second:

So congratulations again! You just wrote your first PROGRAM in C! What more can you accomplish in one day?! Well, we could try to deconstruct exactly what we just typed out in order to learn a little bit more about C.

  • #include is the keyword we use to tell the compiler that the rest of THAT line of code is the name of a library we want to use. It is also known as a "preprocessor directive".
  • <unistd.h> is a miscellaneous library that we need in order to use C's sleep function.
  • <stdio.h> is a library that allows us to use printf and scanf (We'll be using the latter later on).
  • int main() is C's entry point. A more well-known equivalent would be the "public static void main" method in Java.
  • printf("Hello World"); just prints "Hello World" to the screen of the command prompt.
  • sleep(1) tells our computer to make the programs thread sleep for exactly one second.

 

Now that we have a foothold in the language, let's try something a little more complex. A crud app. I can hear your cries of joy already.

In order to create some sweet-sweet-create-update-read-destroy-goodness, we're going to need:

  1. A collection of some kind.
  2. A method of allowing the user to input data.
  3. Some kind of menu for user navigation.
  4. A method of allowing the user to delete data.

Since creating pretty GUI's isn't the point of this tutorial, we're just going to use the command line, so that's one step down (3. Some kind of menu for user navigation.)

(To be continued)