Commenting is something that is used commonly in a program, /*This is a comment*/. When anything is put in between those two symbols it is disregarded by the compiler, commenting is used to provide clarity to other programmers or even yourself at times. So when you see this know that this isn't part of the code just a comment explaining the code, by the way, I will always put comments in a different color to provide clarity. Using data types is something that you absolutely need in a program so let us not waste anytime talking about them.
int x;
What I just did was declare a variable called x. Int is short for integer and x is the variable I declared. Now right now x is unassigned but that doesn't mean nothing is stored in it. Right now there is what we call garbage in the variable, some crazy number that nobody would usually use. How do we fix this? All we have to do is assign it a value.
x=5;
Now for the rest of the program (unless we say otherwise) x is assigned the value of 5. Notice I didn't have to put int in front of x again, this is because we only do this when we are declaring a variable, after we declare a variable we can just call it by its variable name. So now that x is equal to 5 lets see what we can do with it.
printf("%i",x);
Whoa! What is that? We already know what the printf function does but what about that %i? This is placeholder for x, the % symbol is what is going to hold x and the i symbol is just telling it that x is an integer, i for integer not to hard to remember. The next thing is the x after the quotations don't forget the coma it's telling the compiler that the very next thing you put is what you want to go into %i, in this case x. Don't forget to end it with parenthesis and that pesky little semi colon. Okay now lets put all the code together into a single program!
#include <stdio.h> #include <stdlib.h> int main() { int x; x=5; printf("%i\n",x); system ("pause"); return 0; }
So it prints out a 5 big whoop lets see what else we can do. What if we wanted to take the variable x and times it by something say another variable. First we will declare another integer alongside x.
int x,y,z;
Notice how I declared x,y,z as integers all in one line of code we could have also declared them in three separate line but lets be lazy and do it this way. I will initialize x to be 5 again and y to 3. So the x and y is what we will be multiplying and z will hold the product of the two, like this.
x=5; y=3; z=x * y;
Notice how x and y aren't together like xy. In math class this means multiplication but computers are different, they won't know what the heck you are talking about. Now that we have z lets print it out.
printf("%i",z);
Don't forget the place holder and the i since z is an integer. So you may already be thinking what if you wanted to multiply 5.2 and 3.7 would it make a difference? Yes, it would make a big difference as a matter of fact. The value z would truncated, meaning some of it's value would be cut off. Don't fear, we have a fix.
double x,y,z;
The data type double is used to hold decimal type numbers, this way they will hold their true value even if used as a decimal.
x=5.2; y=3.7; z=x * y; printf("%f",z);
If you were paying close attention you probably noticed a %f instead of %i, no this wasn't a typo this is what holds a double data type. It doesn't really make sense like the i and integer does but it is what it is try to remember it, there were countless of times when I couldn't figure out why my program wasn't working correctly and it turns out all it was was this simple mix up. Also we could have used double to just multiply 5 and 3 instead of using integers. It would have multiplied 5.0 and 3.0 which technically isn't any different. So why ever use integers if you can always just use doubles. The answer is space, using double data types takes up more memory than integers thus resulting in a loss of speed. In a small program it's impossible to notice the difference but in larger more sophisticated programs memory becomes more important. There are many other data types that you can use but to keep complexity at a minimum they won't be covered. If you are curious however a couple more for integers are long and short. For double there is float and long double,there are still more but these are the more used types. There is also a char type which is used for chararcter data types but we'll hold off on that for now. So until now we have been restricted to initializing data within the program. What if we wanted x to be 6 or 8 or 184. It is to much of a hassle to have to alter the source code within the compiler wouldn't it be easier to just initialize a variable while the program is running? This is possible through the scanf function.
scanf("%i",&x;);
What the scanf does is pause the program and wait for a value to be put into the variable. This is similar to the printf output with the %i for integers the only difference is the ampersand symbol before the x. What the & symbol does is take in the address of x in order to manipulate it don't forget this small but important step when using scanf. How about a sample program?
#include <stdio.h> #include <stdlib.h> int main() { int x; double y,z;
printf("Please enter a value for x and y "); scanf("%i",&x;); /*scans in a x/* scanf("%lf",&y;); /*scans in a y*/ z=x/y; /*z equals x divided by y*/ printf("%i divided by %f is %f\n",x,y,z); system ("pause"); return 0; }
The reason why I made y a double data type is because whenever you try to divide two integers that don't divide into eachother evenly your answer will be trucated to zero. For example 3 divided by 2 is 1.5 but 1.5 is not an integer. However, in C when two numbers are being divided and if atleast one of them is of the double type the other will be automatically promoted to a double type just for that calculation. Another thing to notice is whenever your scanning in a double variable be sure to put %lf not just %f. Go ahead and copy this code and see how it works. Notice how when the answer is displayed it has a lot of decimal places, if you don't like the way this looks there is a simple fix. On the printf("%i divided by %f is %f\n",x,y,z); line just go ahead and put a .1%f where you see %f. What this will do is display y and your answer which is z to the first decimal place, if you were to put a .2 they would displayed to the second decimal place and so on. Also notice in the printf("%i divided by %f is %f\n",x,y,z); line I printed out x,y,and z at the same time. Whenever you do this be sure to put the variable in the right order. For instance, x goes into %i because each one is displayed first %i is the first placeholder in the quotes and x is the first variable after the quotes. Now that we have these new founded tools we can write a more interesting and useful program. In the United States the unit of speed is miles per hour, but in many other countries they use kilometers per hour, wouldn't it be nice to build a simple program to convert mph to kph? Well we already have all the tools we need all that's left is a formula. One mph equals 1.609344 kph. So y=1.609344*x, x being our input in the program.
#include <stdio.h> #include <stdlib.h> int main() { double x,y; printf("Please enter a speed in miles per hour\n"); scanf("%lf",&x;); y=1.609344 * x; printf("The equivalent speed in kilometers per hour is %.2f\n",y); system ("pause"); return 0; }
So there you have it simple but useful. Now what if you wanted a program to convert for kph to mph. It would be hassle to actually have to alter the source code. Wouldn't it be convenient to be able to choose what conversion you would like to do while the program is running? Do I ever end a question without an answer? The answer is yes, by giving the program the ability to think for itself (to a certain extent, I'm sure we have all seen terminator 2).