Programming in C (Part 2 - Variables and Constants) — 07 July, 2015

This is the second part of our tutorial, this time we will make a simple Hello World and talk about Variables and Constants.

Hello World

The Hello World it's a little program that simply prints Hello World in the screen, here is the code.

#include<stdio.h>
int main()
{
    printf("Hello World\n");
    return 0;
}

What's the meaning of this?

Variables and Constants

When you declare a variable or a constant you are reserving a memory address, you can use these address to store values. When you declare a variable or a constant you have to assign them a name, to access it later you have to call it by the name you've assigned.

Data Types

Today I will only talk about fundamental data types, we will get deeper into data types later.

Variables

Variables are used to store values, you can think of a variable like a little box where you can store one thing (a number for example), you can change the value of the variable along the code.

You declare variables this way: type name;, you can also assign a value while declaring: type name = value.

Let's add some apples to our Hello World:

#include<stdio.h>

int main()
{
    int apples = 12; //This is the variable apples and 12 is its value.
    printf("Hello World\n");
    printf("I have %d apples", APPLES);

    apples = apples + 1; //We add an apple
    printf("Now I have %d apples, APPLES);

    return 0;
}

The // is used to make comments on the code, comments are not interpreted by the compiler.

Suggestion: Use camel case for the variable names, in camel case each word begins with a capital letter except the first one, like grayApples and messageContent.

Constants

Constants are used to store constant values, this means that after you declare a constant you cannot change its value.

Examples of constants in C:

You can declare constants almost the same way you declare variables, the only difference is the use of the const keyword.

#include<stdio.h>
int main()
{
    int const AGE = 22;
    const int YEAR = 2015;
    const char NEWLINE = '\n';
    ...

You can also define a constant in the preprocessor:

#include<stdio.h>
#define AGE 22
#define NEWLINE '\n'

int main()
{
...

The last one may seem confusing to you, you can think of if like a Search and Replace operation, before the compilation (the process of converting your code to machine code) the C compiler replaces all AGE for 22 and NEWLINE for '\n'. In this way the constant isn't stored in memory because the constant name was replaced by its value in the compiled code.

Our apples are constants now:

#include<stdio.h>

#define BASKETS 2
int main()
{
    const int APPLES = 12;
    printf("Hello World\n");
    printf("I have %d apples in %d baskets", APPLES, BASKETS);
    return 0;
}

The %d is a format specifier, you use the format specifiers as "placeholders", and then you pass the values as arguments.

Normally you use type const or const type if you want to use the constants inside the function.

If you want your constants to be used in the whole program you may use the #define.

Suggestion: It's a good practice to name your constants in UPPERCASE this way you won't confuse constants with variables while coding.

Naming Restrictions

You can't give any name to a variable, all names must obey to all this rules:

Reserved Keywords

auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double      

How to compile/run the code

You will need a C compiler for this, if you are on Linux you can use gcc, please check how to install it in your distribution.

To run a program you just need to run the following programs:

$ gcc code.c -o outputprogram
$ ./outputprogram

What do you think?

It's the first time that I'm doing a tutorial, if you have some sugestions or corrections you can contact me

References