Saturday 21 September 2013

Variables

What are Variables?

Variables are containers. They can be filled with anything. But there are some restrictions too.

Before using a variable you must always declare it. In C, you must initialize it with a name and a type. You may or may not provide an initial value for the variable that you have declared. For example you initialize a variable with type "integer", this means that throughout the program this variable can store only an integer.


In this code snippet, I have shown how to initialize an integer variable called number. In line 33 I have shown how to do it without an initial value. And in line 35, I have initialized it with a value of 4.

Types of variables:

These are the four most commonly used types.



  1. int : Used to store integer values. It generally takes up 4 bytes of space, but it depends on your machine. It can store negative and positive values(In case you don't know what an integer means!)
  2. float : Used to store Decimal numbers. 4.0, 5.63 etc. It generally takes up 4 bytes of space and this again depends on your machine.
  3. double : The advanced version of float. It also stores decimal point values but It can store them with higher precision and it has a larger range too.
  4. char : used to store a single character. it takes up 1 byte of storage. It can store any one character. Either on your keyboard or some other special character. You can look up the whole list on the internet. Just Google it in case you are interested.
We will be looking at some other types soon enough. But these are the four basic types that you must be totally familiar with before we can start doing anything. The following is the kind of things you can do with these four types:


So this was about the very commonly used variable types. We will delve into some more variable types in the next post and then we will go on to input/output constructs which will be a lot more interesting than this!

No comments:

Post a Comment