C: More Operators and Arrays

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
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
February 19, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Conditional OR Ternary Operator

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:

  Input my number and your number: 20 50

  The largest of the two numbers is 50

Bitwise Operators

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.

Arrays

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:

  int mytestscores[5];

  mytestscores[0] = 100;

  mytestscores[1] = 99;

  mytestscores[2] = 98;

  mytestscores[3] = 97;

  mytestscores[4] = 96;

  mytestscores[5] = 95;

Two-Dimensional Arrays

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).

Commenting on Your Code

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.

Till then...

blog comments powered by Disqus
BRAINDUMP ARTICLES

- Microsoft Windows 8 Committed to Cloud Compu...
- Independent Developers Favor Windows Phone 7
- Dell Introduces VMware-based Cloud
- Microsoft and Skype Agree to Acquisition Deal
- Transfer Contacts in Microsoft Outlook
- Zune`s Next Steps
- Safari Books Online Review
- Does Microsoft Get Touch Screens Now?
- Microsoft`s Record Quarterly Earnings Not En...
- Basic Operations and Registers in Assembly
- Assembly Coding within Visual C/C++ IDE
- New Microsoft Office Coming with a Twist
- Microsoft`s FUSE Labs Unveils Spindex Social...
- HP Slate with Windows 7: Dead or Alive?
- Windows Phone 7 Mobile OS to Rival Android a...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials