C has a few "core" data types called primitives these data types typically hold some sort of numerical value. The following are primitives:
float (real numbers)
double (real numbers)
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:
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.
is also acceptable. floats are declared in the same way.
however, attempting something like:
would cause an error, because 3.14 is not an integer.
Declaring characters is done in the same way, however, assignment is slightly different.
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();