Learning C The Easy Way - Part One

Learning C The Easy Way
Chapter One - Getting Started

Introduction:

Welcome to this series on learning the C language. C is a very different language especially if you come from a different language background (such as BASIC or Pascal) though Pascal might find some similarities. Nonetheless, C is something that can actually be learned and put to good use. This is the purpose of this series, to teach you to write programs in C and make them do what you want them to do.

Note that throughout the series, if the opportunity to create a real world example (something you might actually want to you use in your day to day life) can be used, it will be. Seeing how a language is used in these real world situations, even if you never use them personally, is a great way to see how the language fits into this real world

A First Look At A C Program:

It's common knowledge that the best way to learn any programming language is with programming examples, and this is what this series will rely on heavily as I believe that it is true. Examples not only give you a hint of what syntax elements you'd need to achieve this or that functionality it also shows you a bit of how to put instructions together and start learning how you can code your own future projects.

Here is the classic hello world in C:

#include <stdio.h>

void main()
{
    printf("\nHello World\n");
}

As you can see, this example is rather short. But it represents alot of things you need to know from the C language. Indeed, if you're going to code in C, you should know that C has no procedures (or subs) it only has functions and as such, if you don't want a function to return a value (more on functions later) you need to declare it's type as void as shown here. If you want the function to return a value you need to include a return statement. For example, let's take the above example and make it return a zero (0) to the system.

#include <stdio.h>

int main()
{
    printf("\nHello World\n");
    return 0;
}

int is a data type that means numeric value that has no decimal point (we'll cover these data types a little later in this first part). The program uses the printf() function which is defined in the stdio library (which is included by the #include statement at the beginning of the program) to print Hello World on the screen. the printf() function has different formatting characters that can be used. In this example the \n is used to tell the program to print Hello World on a new line on the screen then do a new line after so that the next printf() prints on a new line.

Variables In C:

Needless to say that if you want any programs to be worth using, you might want somekind of input from the user. So that yuo can perform some work on what the users gives us as answers to some questions. In C, you declare a variable by first indicating it's type, then giving the variable a name. A variable name must start with a letter or the underscore (_) character followed by other alphabetic and numeric characters to give it meaning.

Here's a few examples.

int   Age;
float Distance;
char  MaleOrFemale;

This creates 3 variables of different types. Now, let's say you wante to ask the user a few questions based on these variables? C has a function to do just that for you. The function is called scanf() and simply needs a format attribute and a variable name between the parenthesis to wait for the user to enter a value. Note than scanf() has another use that has to do with files which we'll cover later in the series. here is an example program of how to get the user to give you some values.

#include <stdio.h>

int main()
{

    int    Age;
    float  Distance;
    char   MaleOrfemale;

    printf("\nEnter Your Age  ");
    scanf(" %i", &Age);
    printf("\nEnter a distance");
    scanf(" %d", &Distance);
    printf("\nEnter M for male or F for female");
    scanf(" %c". &MaleOrFemale);
   return 0;
}

the format attributs here (%i, %d and %c) in the scanf() calls indicate the data type we want the user to enter. They are not a mean of validation, but rather a means of giving a variable of a given type a given value. There are more formatting attributes that are used with scanf() and printf() that we will look at later in this first part of the series.

More About C Data Types Specifics:

These are variable declarations in their simplest forms. C has a set of data types that allow for specific range of values and types of values to be valid. here they are.

TYPE NAME DESCRIPTION RANGE OF VALUES
short int short integer -32768 to 32767
unsigned short int unsigned short integer 0 to 65535
unsigned int unsigned integer 0 -> +4,294,967,295
int integer variable -2,147,483,648 -> +2,147,483,647
long long integer -2,147,483,648 -> +2,147,483,647
float single precision real (floating point) variable
double double precision real (floating point) variable
char character variable (single byte) -128 to 127
unsigned char character variable (single byte) 0 to 255

As a basis, these are the data types available to you. Depending on the range of values you know you'll be playing with (for example, a human hardly lives longer than 120 years so age could be treated as a short integer or even an unsigned char. Keep in mind that the bigger the data type used to handle values, the more space they require in your computer's memory. It's a good idea to stream line your data types whenever you know that a value can't and won't get any bigger than the range allowed by one of these given data types.

A FEW RULES OF THUMB:

For the sake of learning a few ground rules, Here are 5 of them that you might want to learn and memorize, we'll cover them all in detail later in the series, for now, just read them and remember what they state. It should all fit together very soon. Here are the 5 rules of thumbs.

  • In C Just About Everything Is In A Library That Should be #Included: Indeed, statements like printf(), scanf() and the others are all part of the stdio library. There are others, will cover them in detail later in the series.
  • C is Case Sensitive: The first set of things to remember when coding in C is that of course everything is case sensitive. MyVariable, myvariable, MYVARIABLE are all different adn independant variable names in C.
  • C Keywords Are Lowercase: if you try to Printf("My String Text"); this will give you an error, it should be printf() not Printf().
  • C uses { and } a whole lot: From our examples you know it uses at least one set of opened and closed brace. You'll see later that the { and } are used in a very wide range of statements. In all cases they mean that everything between the { and } are part of the same code level that will get executed as a block of code.
  • In C The ; Ends Most Statements: as you've noticed from our examples the ; is pretty much at the end of every line, this is how C knows that a statement line ends so you can expect to see the ; quite often in our series.

AND IN CONCLUSION:

This concludes the first part of this series. We've learned quite a lot even though this tutorial is short in itself. By reading tutorial parts of this size, I believe that leaning becomes easier and well C needs all the easy helpers we can give it so that the learning material sinks in as much as needed. Until our next part of the series, I encourage you to experiment all you want with what you have learned so far. The more experimenting and playing around with code you do, the more everything will become more familiar and easier for you.

In our next part of the series we will cover all the aspects of making things look good on the screen and we'll take a deeper look at how to get information from the user via the keyboard. Among other things we'll create a simple but useful menu system so that the users can select their choice from the available options and have code execute for these choice. Remember that the first goal of this series is of course for you to learn C programming, but also to equip you with useful C code that you can use and adapt to your liking in your future C projects. Until next time, have fun with C all you want.

MystikSHadows
St├ęphane Richard

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.