Wednesday, 16 March 2011

Declaring and initialising variables

Today, I'll show you how to declare and initialize variables and talk about the possible types.

C has a few "core" data types called primitives these data types typically hold some sort of numerical value. The following are primitives:

int (integers)
long (integers)
float (real numbers)
double (real numbers)
char (characters)

The most used primitives are int, float, and char. Long and double are simply ints and floats respectively which allow a wider range of values.

Declaring a variable is easy. You first type the type of variable you want to declare, and then you give it an identifier (a name) which must start with a letter and may contain numbers and underscore characters as well. For example:

int x;

would declare an integer with the name x. You can now assign a value to x using the assignment operator =.


This would store a value of 5 in x. It is also possible to combine declaration and assignment in one line.

int x=5;

is also acceptable. floats are declared in the same way.

float y=2.7;

however, attempting something like:

int z=3.14;

would cause an error, because 3.14 is not an integer.
Declaring characters is done in the same way, however, assignment is slightly different.

char c='f';

This would declare a character and hold the character 'f' in it.

Every character has its own unique code to identify it. Given the 256 standard characters, these codes are called ASCII codes and each ASCII character has its own. In C, when you store a character in a variable, you are actually storing the value of its ASCII code. When you want to specify that you're talking about a character, you must put it between apostrophes.

Next time, I'll talk about modifying the contents of variables and displaying their contents using printf();


  1. Nice post. I think I might get into this a bit more. Currently I'm learning VHDL and the structure is actually pretty similar to this, things are just labeled a bit differently.

  2. Good lord. I dont think I'm smart enough for your blog. I wont leave you though buddy.

  3. Thankfully, I'm familiar with this (although only slightly), so I'm able to follow so far.

    This is pretty much lesson one of any programming textbook, isn't it?

  4. isnt this the way its done in most of the programing language?

  5. @~Fabi ,no the == is an equivalence operator and does not assign a value to a variable. But yet compares a variable to a value. Therefore in a declaration of a variable and assigning a value to it. You would use for example. int x = 6(or any integer of choice. Where as int x == 6, would give you an error for it is trying to compare x and 6 to see if they are of the same value. But you have not declared a value for x yet giving an error.

    Also excellent read. Always good to cover the basics.

  6. I've been trying to get into coding so this is very informative thanks

  7. @Fabi == means "is equal" it is used for comparison in if statements

  8. its nice to learn but i think to hard for me

  9. Shiiit, i have to learn it from the roots, cause i have no idea what all this means :D