In our last article we left off discussing some of C's Operators and how to use them. We will pick up on the same subject in this article, and cover arrays as well. As always, there is a lot to cover, so let's get to it.
Contributed by James Payne Rating: / 1 February 19, 2008
Our good buddy the conditional operator is made up of two symbols: the question mark (?) and the colon(:). Consider the following:
mine = 100;
yours = 200;
ours = (mine > yours) ? mine : yours
In this example, the variable ours will be assigned the value of the variable yours, or 200. This occurs because the expression (mine > yours) is false. Had it been true, then the value of ours would have been the value of the variable mine, or 100.
Here is an example:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int mine,yours,bigger;
printf ("Input my number and your number : ");
scanf_s("%d %d",&mine, &yours);
bigger = mine > yours ? mine : yours;
printf("The largest of two numbers is %d \n", bigger);
}
This code asks the user for two numbers. It stores the first number in the variable mine, and the second in the variable yours. Next it asks if mine is greater than yours. If so, bigger will get the value of the mine variable. If not, it will get the value of the yours variable. Finally it prints some text and appends the value of the variable bigger.
If the user had entered the numbers 20 and 50, the result would be:
Though these operators are a bit beyond the scope of this particular article, it is good to know of their existence; otherwise when I teach them to you in a future article, you will have a heart attack and that insurance policy your mom and I just took out on you will pay us handsomely.
Operator
What it Means
&
For Bitwise AND
|
For Bitwise OR
^
For Bitwise Exclusive
<<
For Shift left
>>
For Shift right
These operators manipulate given data at the bit level, operating on each bit of the data. They cannot be used on floats or doubles.
Well they can, but it won't work. Chump.
Comma Kazi
When we want to link a series of related expressions together, we use the comma operator. The list of expressions is evaluated from left to right and the last expression becomes the value of the expressions combined.
Behold my mighty example:
hercules = (a=5, b=12, a+b);
In the above example, we assign a the value of 5, and b the value of 12. Then we add a and b together, and that value becomes the value of hercules. So in the end, hercules becomes 17.
We will discuss the comma operator more in depth when we cover Loops in a future tutorial.
Does This Variable Make Me Look Fat?
The "size of" operator returns the size of a data type, variable, arrays, constants, and data type qualifiers. It tells you how many bytes the operand takes up in memory. You can also use it to allocate memory to variables when the program executes.
The "size of" operator is a little beyond the scope of this article.
There are other operators in C, such as the pointer operators and selection operators, which we will cover in upcoming episodes.
One last thing regarding operators. When we want to give a part of an equation precedence, we do so by encapsulating it with parentheses(). Consider the following:
5+2*5= 35
5+(2*5)= 15
In the first example, math is performed left to right, result in the answer thirty-five. In the second example however, we give the equation 2*5 precedence by wrapping it in parentheses. This way the program does the multiplication first, then adds 5, resulting in the answer 15.
If we think of variables as a file folder, then we would think of an array as a file cabinet, capable of holding not just one value, but many. They operate in much the same fashion as variables, with a few exceptions. They still must be declared and given a type. Here is an example of how to declare an array and assign it some values:
To declare an array:
int mytestscores[5];
The above code declares an array with the data-type of an integer, names it mytestscores, and sets it to hold six elements, or values. You will note that the [5] portion is where we declare how many elements are in the array. I know what you're thinking: "but you said there were six elements!" There are. In programming languages, elements start off in the 0 position, so our element index (or where each piece of data would be stored) is: 0, 1, 2, 3, 4, 5.
Let's say we want to store my last six test scores at the same time we declare the array. Here is how we would do so:
int mytestscores[5]={100, 99, 98, 97, 96, 95}
Now let's say we wanted to assign a variable one of the values in our array. This is where the indexing will start to make sense:
int myworstscore;
myworstscore=mytestscores[5];
The above code creates a variable with the data-type of integer named myworstscore. It then assigns a value to the variable by calling upon an element in our mytestscores array, specifically, the value at index 5. In this case, 95.
We can also assign values to our array in the following manner:
Like most programming languages, C allows you to create two-dimensional and multi-dimensional arrays. A two-dimensional array works like a grid, or a spreadsheet, creating rows and columns to store data in. In the example below, the first number represents the number of rows, while the second number represents the number of columns:
char name[2] [4];
This creates a two-dimensional array with two rows and four columns. If we wanted to assign values to our array, we could do this:
char name[2] [4] = {{'B','i','l','l'},
{'D','i','n','g'}};
This creates an array that contains the name Bill Ding. If I wanted to create two variables to hold the initials of Mr. Bill Ding, I could do so like this:
char firstInitial;
firstInitial = name[0][0];
char lastInitial;
lastInitial = name[1][0];
Remember that arrays start at 0, so row 0, column 0 would equal "B" and row 1, column 0 would equal "D". We could also change a value in the array. Let's say we wanted to change the name to Bill Dung. We would have to access the "i" in Ding to do so:
name[1] [1] = 'u';
Note that the "i" in Ding is located in the second row and the second column. The n is located in the second row, third column, etc.
Also note that we don't always need to set the size of an array, but it is good practice to do so.
And finally, we can also set the values of a multidimensional array like this:
char name[2] [4];
name [0] [0] = 'B';
name [0] [1] = 'i';
name [0] [2] = 'l;
name [0] [3] = 'l';
name [1] [0] = 'D';
name [1] [1] = 'i';
name [1] [2] = 'n';
name [1] [3] = 'g';
There are also ways to Loop through arrays and ways to create dimensional arrays larger than two dimensions, but those are beyond our scope (we will cover looping through arrays in a future article, however, when we discuss loops).
Now that you know how to work with arrays and operators, you will be able to make larger programs. And often when we make larger programs, things can go wrong. There it is, six months after you have released your program and BAM! some wacky user has found a way to use it in some way you didn't intend. So now you have to look through your code and try to figure out what you were thinking six months ago. It's worse if you are looking at someone else's code and trying to fix it. In C, and every other language, you are able to comment, which is text that the computer ignores or can't even see, that lets you/other programmers know what you were trying to achieve with parts of your code.
To write a single line comment you use the //, like so:
printf("I like to eat monkey stew!"); //print some stupid text
To do multi-line comments:
/*This code below
will print some stupid text*/
printf("I like to eat monkey stew!"); //print some stupid text
When the computer sees the // it ignores the text following it on that line. When the computer sees /* it ignores all text after it until it sees the */.
That's it for this episode. Join us next time when I discuss Statements and MAYBE Loops in C.