Expressions And Operators In C Language - Online Article
One reason for the power of C is its wide range of useful operators. An operator is a function which is applied to values to give a result. You should be familiar with operators such as +, -, /.
Arithmetic operators are the most common. Other operators are used for comparison of values, combination of logical states, and manipulation of individual binary digits. The binary operators are rather low level for so are not covered here.
Operators and values are combined to form expressions. The values produced by these expressions can be stored in variables, or used as a part of even larger expressions.
- Assignment Statement
- Arithmetic operators
- Type conversion
- Comparison
- Logical Connectors
Assignment Statement
The easiest example of an expression is in the assignment statement. An expression is evaluated, and the result is saved in a variable. A simple example might look like:
y = (m * x) + c
This assignment will save the value of the expression in variable y.
Arithmetic Operators
Here are the most common arithmetic operators: *, / and % will be performed before + or - in any expression. Brackets can be used to force a different order of evaluation to this. Where division is performed between two integers, the result will be an integer, with remainder discarded. Modulo reduction is only meaningful between integers. If a program is ever required to divide a number by zero, this will cause an error, usually causing the program to crash.
Here are some arithmetic expressions used within assignment statements.
velocity = distance / time;
force = mass * acceleration;
count = count + 1;
C has some operators which allow abbreviation of certain types of arithmetic assignment statements. These operations are usually very efficient. They can be combined with another expression. Versions where the operator occurs before the variable name change the value of the variable before evaluating the expression, so These can cause confusion if you try to do too many things on one command line. You are recommended to restrict your use of ++ and - to ensure that your programs stay readable.
Type Conversion
You can mix the types of values in your arithmetic expressions. char types will be treated as int. Otherwise where types of different size are involved, the result will usually be of the larger size, so a float and a double would produce a double result. Where integer and real types meet, the result will be a double.
There is usually no trouble in assigning a value to a variable of different type. The value will be preserved as expected except where:
- The variable is too small to hold the value. In this case it will be corrupted (this is bad).
- The variable is an integer type and is being assigned a real value. The value is rounded down. This is often done deliberately by the programmer.
Values passed as function arguments must be of the correct type. The function has no way of determining the type passed to it, so automatic conversion cannot take place. This can lead to corrupt results. The solution is to use a method called casting which temporarily disguises a value as a different type.
eg. The function sqrt finds the square root of a double.
int i = 256;
int root;
root = sqrt( (double) i);
The cast is made by putting the bracketed name of the required type just before the value. (double) in this example. The result of sqrt( (double) i); is also a double, but this is automatically converted to an int on assignment to root.
Comparison
C has no special type to represent logical or boolean values. It improvises by using any of the integral types char, int, short, long, unsigned, with a value of 0 representing false and any other value representing true. It is rare for logical values to be stored in variables. They are usually generated as required by comparing two numeric values. This is where the comparison operators are used, they compare two numeric values and produce a logical result.
Note that == is used in comparisons and = is used in assignments. Comparison operators are used in expressions like the ones below.
x == y
i > 10
a + b != c
In the last example, all arithmetic is done before any comparison is made. These comparisons are most frequently used to control an if statement or a for or a while loop.
Logical Connectors
These are the usual And, Or and Not operators.
They are frequently used to combine relational operators, for example
x < 20 && x >= 10
In C these logical connectives employ a technique known as lazy evaluation. They evaluate their left hand operand, and then only evaluate the right hand one if this is required. Clearly false && anything is always false, true || anything is always true. In such cases the second test is not evaluated.
Not operates on a single logical value, its effect is to reverse its state. Here is an example of its use.
if ( ! acceptable ) printf("Not Acceptable !!\n");
About the Author:
No further information.
Related Online Articles:
Comments
No comment yet. Be the first to post a comment. |