Variables
What is a variable? You can think of it as a storage unit or like the letter x in math. Click here for a very detailed description of variables if you're interested. Variables have different types. Each type can hold a different type of value. For example, int holds integers and float holds floating points which are decimal numbers and there's also double which is like float but it has more digits which are useful in scientific applications and finally there is char which holds single characters. Here are their ranges.
char | -128 to 127 |
Int | -32768 to +32767 |
float | 3.4 e-38 to 3.4 e+38 |
double | 1.7 e-308 to 1.7 e+308 |
Now, you can either open our cHelloWorld project and delete the printf line or start a new project. If you're wondering why there's a "c" in front of "HelloWorld", the answer is I use Hungarian notation in which types are attached to names, in this case "cHelloWorld" means HelloWorld is a C language project and now maybe you're wondering why the "c" is a small letter. It's a naming convention called lowerCamelCase. Oh, one last thing about naming, variable names can't start with non-alphabetic characters.

First of all, we will declare and define a variable. So write this line of code above the line "return 0;"
int intFirstVariable = 0;
Here, the equal sign assigns 0 to our variable so "=" is the assign operator. So what does this line do? It allocates enough space to hold an integer value in memory and associates that location's memory address with the variable "intFirstVariable" and assigns the value 0 to it. You don't need to think about memory addresses for now. So why assign 0 to the variable? We could have just declared it like "int intFirstVariable;" and left it like that and assigned a value later on but in big and complex programs, if you use a variable which isn't defined then your program may crash because any value could be sitting at that memory location.
We declared and defined a variable, now what? Now we will learn how to print a value stored in a variable, on screen. So go ahead and write this line of code under the variable definition.
printf("Value stored in the variable intFirstVariable is %d\n", intFirstVariable);
If you compile and run this program, you should see "Value stored in the variable intFirstVariable is 0" in the console (the black box) which is the value we assigned to our variable. The printf function prints a stream on screen so here by writing "%d" we are telling the function there is going to be a value from an integer variable in the stream and we tell the function what variable that is after the comma. If you declared another variable called intSecondVariable and assigned a value to it, you could display both of them on screen so let's do that.
Write int intSecondVariable = 1; under the first one and change the printf function as the following one.
printf("Value stored in the variable intFirstVariable is %d\nValue stored in the variable intSecondVariable is %d\n", intFirstVariable, intSecondVariable);
If you want to display more integer variables then you just add "%d" to where you want but be careful about where you write variable names after the first comma because they will be replaced with those "%d"s in that order.
So our program looks like this now (I won't show #include directives from now on because they come by default)
int main()
{
int intFirstVariable = 0;
int intSecondVariable = 1;
printf("Value stored in the variable intFirstVariable is %d\nValue stored in the variable intSecondVariable is %d\n", intFirstVariable, intSecondVariable);
return 0;
}
There are different special characters for different variable types. They are called specifiers. For example integers get replaced with "%d", floats and doubles get replaced with "%f" (if you want to show certain number of digits in the decimal part, you can use "%.2f" which means there will be 2 digits in the decimal part), and chars get replaced with "%c".
Operators
There are different types of operators. I will show you the arithmetic ones in this post. Some of them are just like the basic operators in math which are add, subtract, multiply, and divide. Let's use them. Change intFirstVariable's value to 1, intSecondVariable's value to 2 and declare a float variable called floatResult under them and assign it to 0.0 then write the following just under the first printf.
floatResult = intFirstVariable + intSecondVariable;
printf("intFirstVariable + intSecondVariable = %.1f\n", floatResult);
floatResult = intFirstVariable - intSecondVariable;
printf("intFirstVariable - intSecondVariable = %.1f\n", floatResult);
floatResult = intFirstVariable * intSecondVariable;
printf("intFirstVariable * intSecondVariable = %.1f\n", floatResult);
floatResult = (float)intFirstVariable / intSecondVariable;
printf("intFirstVariable / intSecondVariable = %.1f\n", floatResult);
floatResult = intFirstVariable % intSecondVariable;
printf("intFirstVariable mod intSecondVariable = %.1f\n", floatResult);
Why use float? It was unnecessary for the first three and the last operations because we added, subtracted, multiplied and took modulo (remainder of division of two numbers) of integers (whole numbers) but in the fourth operation, we divided one of them by the other one and in this case, it was 1 divided by 2 which is a fraction so in order to show that the result for this is 0.5, we need to use a float type variable. What did that (float) do? (float) casted intFirstVariable to float type so we would get a float result from an integer division. If we didn't cast one side of the division, we would get 0.0 as the result. Type name in parentheses followed by a variable is casted to the specified type.
The ones I wrote about were binary operators. There are other arithmetic operators which work as shortcuts.
Unary Operators
+intFirstVariable; means intFirstVariable = 1 * intFirstVariable;
-intFirstVariable; means intFirstVariable = -1 * intFirstVariable;
Assignment Operators
intFirstVariable += 5; means intFirstVariable = intFirstVariable + 5;
intFirstVariable -= 5; means intFirstVariable = intFirstVariable - 5;
intFirstVariable *= 5; means intFirstVariable = intFirstVariable * 5;
intFirstVariable /= 5; means intFirstVariable = intFirstVariable / 5;
intFirstVariable %= 5; means intFirstVariable = intFirstVariable % 5;
Prefix and Postfix Operators
intFirstVariable++; means intFirstVariable = intFirstVariable + 1;
++ intFirstVariable; also means intFirstVariable = intFirstVariable + 1; but in this case, the operation is performed before. I will write about this when we get to loops.
intFirstVariable--; means intFirstVariable = intFirstVariable - 1;
--intFirstVariable; also means intFirstVariable = intFirstVariable - 1;
I will write about logical operators in the coming post. If you want to go ahead and take a look at all the operators, click here.

and works like this.



