We can manipulate the data contained in a variable by using operators.
Sum = Number1 + Number2; /*add Number1 and Number2, store result in Sum*/
Sum = Number1 - Number2; /*subtract Number2 from Number1, store result in Sum*/
Sum = Number1 / Number2; /*divide Number1 by Number2, store result in Sum*/
Sum = 1 + 2; /*Sum is equal to 3*/
Sum++; /*increment Sum (add one)*/
Sum--; /*decrement Sum (subtract one)*/
We can also copy variables
Number1 = Number2; /*Number1 now contains same data as Number2*/
Taking Input
We can use the scanf() function to take input from the person running the program and assign the value to a variable. Here's how it looks:
scanf("%d", &Number);
As with printf() we must use the right sign, here we will be dealing with an integer so we use %d. The second parameter is the variable to which we want to assign the value.
#include
int main()
{
int Number1, Number2, Sum;
printf("Enter a number:");
scanf("%d", &Number1);
printf("Enter another number:");
scanf("%d", &Number2);
Sum = Number1 + Number2;
printf("The sum of %d and %d is %d", Number1, Number2, Sum);
}
Now this program is more interesting. It asks the program user for input. It then assigns the first integer value to Number1:
scanf("%d", &Number1);
Then it asks them for another number and assigns that to Number2:
scanf("%d", &Number2);
The it adds the two values together and prints out the result:
Sum = Number1 + Number2;
printf("The sum of %d and %d is %d", Number1, Number2, Sum);
But you ask, why does the variable name have a '&' in front of it? Well when we're dealing with scanf() we must preceed the variable name with that '&' sign. You must do this because you are storing the input directly into a memory location. For now, just don't forget the '&'.
Let's do it with a char.
#include
int main()
{
char Letter;
printf("Enter a letter:");
scanf("%c", &Letter);
printf("Your letter is %c", Letter);
}
scanf()is a very useful function, play around with it for a while, try modifying the program a little, maybe take two letters. When you're happy continue.
Pointers
Pointers are an important part of C. If you don't understand how pointers work, you use alot of the power and flexibility of C. Pointers are difficult to understand if you don't try.
We define a pointer like so:
int *p;
The * shows that this isn't a variable, it's a pointer. A pointer does what it says, it points. Also, the pointer must be a certain type, as with variables. if it's going to point to an integer, we define it as an integer pointer. Let's have another look:
int num, *p;
num = 10;
p = #
Remember the '&' sign from the last chapter? What this sign means is, instead of assigning the "value" of num to the pointer, we actually assign the address in memory where the data for num is stored. Let's say that again, p will contain the address in memory of num.
Now one thing about pointers is that, logically, they contain two values.
p is the location holding the address in memory of num.
*p is the location pointed to by that address.
So if we use p we can manipulate WHERE in memory the pointer is pointing to. Yet if we use *p we can manipulate the data contained within the address that the pointer is poining to. If we alter *p, we also alter the variable it points to, let's look at an example. p contains the pointed-to address, *p is another name for the variable it is pointing to. Get it? I hope so, it's kind of confusing at first.
#include
int main()
{
int num, *p; /* define variable and pointer */
num = 7; /* number now equal to 7 */
p = # /* p contains ADDRESS IN MEMORY of num */
*p = 5; /* num now equals 5 */
num = 3; /* num AND *p now equal 3 */
}
Think of *p as an alias for the variable it points to, it allows you to manipulate the data within that variable. While p controls which variable, which address in memory we are pointing to. Pointers allow us to jump around memory addresses and manipulate the data in them, allowing us to get into a lower-level, into the workings of things. To fully understand we should discuss memory addresses.
Memory addresses
All computers have memory, also called RAM. RAM holds the programs your computer is currently running, along with the data the programs are currently manipulating. Each byte of memory has it's own address, the address of the first byte is 0, followed by 1, 2, 3... etc. Memory can group bytes together if it needs to form a larger variable, this happens when we declare an int variable.
An int variable is assigned four consecutive bytes in memory. When the program runs, the computer reserves space for that variable somewhere in memory, and from here on the data in the variable can be accessed by the computer by the address at which it is located.
Consider the following:
int number;
number = 10;
Address Data
220, 123 -
220, 124 -
220, 125 -
220, 126 10
220, 127
220, 128
220, 129
Do you see? Now the computer knows that the data for number is stored in four consecutive memory addresses starting at 220, 126. Now if we declare a pointer and do:
int *pointer = &number;
Memory could now look something like this:
Address Data
220, 123 -
220, 124 -
220, 125 -
220, 126 (number) 10
220, 127 (number)
220, 128 (number)
220, 129 (number)
220, 130 -
220, 131 -
220, 132 -
220, 133 (pointer) 220, 126
220, 134 (pointer)
220, 135 (pointer)
220, 136 (pointer)
The value we assigned to number is stored in its area in memory, and the pointer also has it's own place in memory. The thing about the pointer is that the data it contains is the address in memory of the variable to which is "points". If we change the value stored in the pointer, we change where it points. One thing i must mention, most computers today use 32-bit (4 byte) memory addresses. So the pointer is assigned 4 bytes of memory. Some computers use 64-bit memory addressing, meaning pointers on that computer would take up 8 bytes in memory.
But, why use them?
I asked the same question, it seems pointless, and in the above examples i suppose it is. I'm not showing you why pointers are used, i'm trying to explain how they're used, and to help you understand how they work. The advantages are pretty unclear, but as you progress and learn more about different data types you will realise the advantages. You can quickly change something with a pointer rather than changing much more data. It's just an easy way to work at the memory address level, manipulate data contained there, it allows you to manipulate data in memory more accurately. Make good use of pointers, they make C programs much more efficient.
Control Structures and Conditionals
There are a few control structures in C that you can make use of, very similar in syntax to those in Perl and PHP.
If/Else
These allow you to execute code IF a boolean expression is true:
if(1 == 1) {
printf("1 is equal to 1");
}
else {
printf("1 is not equal to 1");
}
This speaks for itself really. If 1 is equal to 1, print out so, if not, print out that it isn't. Obviously 1 will always be equal to 1, so the printf() statement between the curly braces after the if() function will be executed. If 1 wasn't equal to 1, then the else would be executed.
#include
int main() {
int number;
printf("Enter a number:");
scanf("%d", &number);
if(number == 1) {
printf("The number you entered is 1.");
}
else if(number == 2) {
printf("The number you entered is 2.");
}
else if(number == 3) {
printf("The number you entered is 3.");
}
else if(number == 4) {
printf("The number you entered is 4.");
}
else {
printf("The number you entered is not 1, 2, 3 or 4.");
}
}
It's pretty easy to see what's going on here, these functions allow is to test if a boolean expression, or series of boolean expressions are true, and execute code depending on those tests.
No comments:
Post a Comment