What is a variable?
We all know how to add right? 2+2=4? Suppose for an instant though that we wanted write a formula on how to add to someone who had no concept of it. Like say...someone who works for the IRS. We could right it as a+b=c. Now, that lovable IRS guy could write it down and consult it when he forgets how to add. The A, B, and C are variables. Mister IRS has lots of different numbers other then 2 and 4 to deal with it and he can substite those numbers in the place of A,B, or C. "Let's see A = 3, B = 4, so C will equal 7!!!!, Ha and those guys in college mocked me when I decided to work for the government!!!" You can plug any formula you want into a computer; OHM'S LAW E=IR, etc. I know that last sentence wasn't necessary I just want to impress people with the fact I know OHM'S LAW.
All a variable is in our field is just a place set apart in a computers memory to hold a changing value. This is very useful, for it helps those hard working scientists do complex math, leaving plenty of time to eat nachos and watch star trek. Now don't shriek when you see all these numbers, doing math is simple with computers because the computer does all the work!
There are many different types of variables. Each "variable type" has a different range of values. You can only fit oh so much stuff into one variable type! Here are 4 of these variable types;
int - interger/ range = -32768 to +32767/ storage required = 2 bytes
long - long interger/ range = -2147483648 to +2147483647/ storage required = 4 bytes
float- floating point interger/ range = 3.4*10ex38 to 3.4*10ex-38/storage required 8 bytes
It's not a necessity that you memorize these types just yet. I recommend that you learn them through use. To use variables in this section, we will be using the scanf command. The scanf (scan function) basically reads your keyboard for a specific type of input. It stops scanning when you press the enter key. I'll explain more after you write the program.
We'll also using C's operators. An operator is just a cooky word for mathmatical doodad or symbol. The 4 we'll be using is (now pay attention IRS!) + for addition, - for subtraction, * for multiplication, and finally / for division. We will first write a program that squares a number. Let's code shall we? Type this program into your C compiler;
#include <
stdio.h
>
main ( )
{
int x,y;
printf ("Enter a number and I'll square it!!!\n");
scanf ("%d", &y);
x = y*y;
printf ("%d is your number squared!!! HAHAHAHA!", x);
return (0);
}
Compile it and run it. Remember to NOT click and paste!!!!
Let's breakdown lines 4,5,6, and 7 of the above source code.
Line 4
int x,y;
In this line we are declaring our variables. We are basically making the little sections of memory in this line of code. So here we have 2 'boxes' called x and y. They are integer variables ready to put their talents to work for you.
Line 5
printf ("Enter a number and I'll square it!!!\n");
Just a printf statement that asks a question.
Line 6
scanf ("%d", &y");
This is where scanf is used. The %d is what is called a place holder. We'll get into it's signifcance later. The scanf function here was patiently waiting for you to enter a number. When you pressed enter it took that number and "threw" it into the y variable. That is why you had to type in the &y. Soooooo now we have a number sitting in the little y box.
Line 7
x=y*y;
This tells your computer to do some number crunching. It takes what is in the y variable and multiplies it by itself. Afterwards, it takes that number and throws it into the x variable. So now we have the answer sitting in the x variable.
Line 8
printf ("%d is your number squared!!! HAHAHAHA!", x);
Okay it's a printf statment with some subtle differences. The %d is a placeholder as said before. %d is specifically used for int variables. Used in printf it will display the variable that you stored in x. At the end of the quotations and comma you see x. This basically 'tells' your compiler the x variable will be displayed in the %d place holder. Put y in there instead and see what happens.
Play around with the different math functions. REMEMBER THAT C DOES MATH BACKWARDS. Ie; 4=3+1.
You know what though? If your just using int, it will not work with everything. Case in point;
#include <
stdio.h
>
main ( )
{
int x,y;
printf ("Enter a number and I'll reciprocate it.\n");
scanf ("%d", &y);
x = 1/x;
printf ("%d is your answer.",x);
return (0);
}
Compile it and run it. When it asks you for a number enter 2. For those don't know the reciprocoal (a number divided into 1) of 2 is 0.5. Press enter and you should get something cooky. It will not work. The int variable will not work well with anything other then whole numbers. What to do? What to do? Real C programmers don't panic and move to Montana (unlike pascal people), they find another way!!! That last sentence wasn't necessary, I'm always searching for new ways to annoy people with my unique brand of dry humor. Try using a different variable type. In this case use float. Retype this into your compiler;
#include <
stdio.h
>
main ( )
{
float x,y;
printf ("Enter a number and I'll reciprocate it.\n");
scanf ("%f", &x);
y = 1/x;
printf (%f is your answer.", y);
return (0);
}
It should work now. I'll tell you how to fix those annoying decimal place 0's later. I'm at school now and I'm using valuable class time to work on my hobby. If your an experienced programmer and you detect mistakes just email me and let me know. I'll talk to all later you wondrful internet people later. - Digitalscribe.