C Guide – Variables


We’ve all done Maths in high school, so you should know what a variable is? Say you had the formula:

2x+3 = 11

Then here, ‘x’ is the variable. However, in a program, ‘x’ is not just one value, ‘x’ can be any value you want to to be – so an integer or a string or just a single character!

 

Initialization

In C there aren’t that many types of variables, here is a select few that are very commonly used:

Variables

What is shown above is also how we initialize a variable in C. if you want a variable, say ‘aVariable’, and you want ‘aVariable’ you be an integer value, then you would initialize ‘aVariable’ like:

AssigningVars 

You have probably already noticed the semi-colons again. Remember what i said earlier about them signifying the end of things, well this time they’re here to signify the end of an initialization.

 

Printing Variables

Just like we printed the words “Hello World” earlier, we can also print variables in must the same way!

PrintingVariables

 

Most of that code above you should recognise. However, take a look at line 5. Notice anything?

PrintingVariables

OK, well you may have noticed that we have initialized a variable called ‘aVariable’ and declared it as an integer; but what will be new to you it the ‘=10’ at the end. This is an assignment – so yes we need the ‘;’ again 😛 – This time, when we initialize the variable, we automatically give it the value ‘10’.

PrintingVariables

This is the line that we’re interested in: line 6. At first glance this may seem fairly complicated, but it isn’t really – honest! 🙂

The first part printf(“aVariable = “) should be obvious, we’re just printing the word ‘avariable’ to the screen. However, also with in that string we have the letters ‘%d’, this signifies the fact that we wish to include an integer (or a double to be more accurate) into the string to be printed.

When we compile the code, the compiler see’s this, and will expect to see the variable that we wish to print come after the string, both being separated via a comma. If all goes well, we should get the output:

aVariable = 10

 

Initializing and Assigning the other variable types

AssigningVars

Like the initialization of an integer variable, char’s and float’s are done very much the same way. Sadly, C doesn’t have a type for string, so you have to use something a little more advanced – but not hard 😛 –

 

Next we’ll see how to get a users input – the simple way for now

Advertisements

About Badgerati
Computer Scientist, Games Developer, and DevOps Engineer. Fantasy and Sci-fi book lover, also founder of Cadaeic Studios.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: