What are they ?
Every program language has variables, in them you can store data, consult and modify that data. Imagine a simple program calculator that only adds 2 numbers, in the program each number needs to be stored in a variable and a third variable would be created to save the result of the operation.
How to use them in
To use a variable in
C you must declare it, that means that you need to describe the variable before use it. In the declaration you specify what type of data will store your variable and what will be its name.
C there are several type of variables, but let’s keep it simple and check the basic ones now and see the others in the future lessons:
||Integer Numbers||1, 2, 400, -250, -1000|
||Floating Numbers||0.5, 1.01545, -2548.53, -999.99|
||Character (only one character)||a, A, e, W, -, _, ?|
The name of a variable can contain letters, numbers and underscores, you just need to make sure that it must start with a letter or an underscore, it can not start with a number, keep that mind.
So, let’s see how a variable declaration looks like in
Now, let’s declare the variables of our calculator program, that adds only integer values:
To store data in a variable we use an assignation, the assignation could be after the declaration or in the declaration itself:
To write floating numbers in
Cyou specify it by adding an
fat the end of the value and the decimal separator is the dot
To complete our calculator, imagine that we what to add 4 plus 5: