C Variables: theory

Variables create the opportunity re-use the same value multiple times. Variables store a value that you assign to it, which you can later retrieve whenever and however many times you want.

This lesson is all about the theory of variables, which probably isn’t very exciting.  But so long as understand this, you’ll be able to move onto the more useful, and interesting, business of actually using variables in a practical way.

Variable types

Because C is a relatively low level programming language, all variables must have a “type”. You chose the type depending on what’s going to be stored in the variable. There are lots of variable types, but here are the four most common:

Type What it stores
char The “character” type stores a single character. This can be any letter (upper or lower case), any digit or various punctuation marks- basically anything on your keyboard.
int “Integer” types, as the name suggests, stores integers. This number can be positive or negative (positive by default), but there are upper and lower limits- the integer must be between -32768 and 32768.
float The “floating point” type stores decimal numbers. Floats can store positive or negative decimals, so long as it’s within this range: 1.17549435×10-38 to 3.40282347×1038.
double “Double precision floating points” allow you to store decimals of a wider range: 2.2250738585072014×10-308 to 1.7976931348623157×10308.

Creating variables

The process of “creating” a variable can be split up into two parts: declaring and assigning.  In order to declare a variable, you first need to know what type it will be.  And then you need to think of a name for it, which you will use from then on to defer to the value stored within it.

Naming variables

You can name a variable almost anything you like, but there are a handful of rules that you must follow. Variable names cannot:

  • begin with a number,
  • contain spaces,
  • contain punctuation characters, with the exception of underscores,
  • be a keyword;

Now, more on that last point. “Keywords” are used by C to perform pre-defined actions. There’s a list of the most common at the bottom of this post. However, variable names can contain these keywords- just so long as the keyword is not used on its own.  For example, ‘else’ is a keyword- so you can’t call your variable else, but you are allowed to call it else_car, for example.

Variable names should also be descriptive and relate to the value stored within it.  So if you’re using a variable to store your height, name it something like height or my_height, rather than variable1, or something equally un-descriptive.

And one final note: variable names are case-sensitive.  This means that a variable named height is not the same thing as a variable named Height.  Capital letters can be used anywhere in a variable name, and indeed they’re often used to separate out multiple words: my_height and myHeight are equally acceptable.

Syntax

The code used to create a variable is probably the most straight-forward part about it.  So let’s jump straight to it.

char first_letter;

This simple line of code declares the variable.  As you can probably tell, there are two parts to this: ‘char’ is the variable type, and first_letter is the name.  Finally, the semi-colon terminates the statement.

So now we have a character variable called first_name, but it’s not much use at the moment.  Because we haven’t assigned a value to it, the variable is sitting empty.  So let’s put that right.

first_name = 'j';

This second line of code assigns a value to the variable- in this example, that value is the letter j.  But you could use any character you want- any letter (upper or lower case) or any punctuation/symbol.  Just remember to surround the character you chose with single quotes.  Also remember that the variable can only store a single character- if you want to store a whole word or sentence, you’ll need to wait until a future lesson.

With these two lines of code, you can declare and assign a value to a variable.  But actually you can reduce this to a single line of code, like so:

char first_name = 'j';

This single line is more efficient than two lines, so if it’s possible to do it this way, you should.

Now that we have the theory out of the way, we can move on to actually making use of variables in a practical sense.  Go and find the next lesson when you’re ready.

This entry was posted in C programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>