UNIX C Programming

Chapter 5: Operators, Expressions, and Statements

 

Objectives:

This chapter introduces students to the arithmetic operators, the concept of operator precedence, and the meaning of the terms expressions and statements. The objectives important to this chapter include:

  • Discuss the types and use of arithmetic operators
  • Explain operator precedence
  • Clarify the difference between an expression and a statement
  • Show how the while loop is constructed and discuss how it is used
  • Explain the rules for type conversion
  • Discuss the use of type casts
  • Explain the passing of arguments between functions
Concepts:

This chapter introduces the while loop. The structure of this loop is as follows:

	while (test condition is true)
	{
		do the following statements
	}

If the test condition is false to begin with, the entire while loop is skipped. The statements placed inside the braces of the while loop represent a block (as in a block of code).

C uses operators to represent arithmetic operations. The symbols assigned to operators are the following:

	= 	assignment
	+ 	addition
	- 	subtraction
	* 	multiplication
	/ 	division

There is an operator precedence in C. Operators in order of descending precedence are:

	()
	+ - (when used as unary operators)
	* /
	+ - (when used as binary operators)
	=

C features data objects, lvalues, rvalues, and operands. A data object is a general term for a region of data storage that can be used to hold values. The lvalue (for left value) is a name or expression that identifies a particular object. The lvalue is found on the left side of the assignment operator. The rvalue refers to values that can be assigned to modifiable lvalues. This means that rvalues are the values that get assigned to the lvalues. The rvalue is found on the right side of the assignment operator. Finally, operands are what operators operate on.

There are binary and unary operators. Binary operators require two operands, as found in addition and subtraction. Unary operators require a single operand. A unary operator is used to change the algebraic sign of a value.

Besides the five operators listed above, other C operators include:

  • sizeof() Size of operator. Determines the size in bytes of the operand
  • % Modulus operator. Gives the remainder in integer arithmetic
  • ++ Increment operator. Increase the value of the operand by 1.
  • -- Decrement operator. Decreases the value of the operand by 1.

The increment and decrement operators have a very high precedence of association. Only parentheses are higher. There are two forms of these two operators. These are:

  1. Prefix form ++ count -- count
  2. Postfix form count ++ count --

With prefix, the variable is incremented (or decremented) before an operation takes place. With postfix, the variable is incremented (or decremented) after an operation takes place.

Students tend to be confused between statements and expressions. An expression consists of a combination of operators and operands, whereas a statement is a complete C instruction with necessary punctuation.

Three types of statements are the assignment statement, the function statement, and the while statement. The while statement is also known as a structured statement.

C produces side effects, which are the modifications of a data object or file. An example is the assignment statement:

	states = 50;

A compound statement is two or more statements grouped together, by enclosing them in braces. This group of statements is known as a block.

Type conversions can be handled automatically or by the C programmer. The automatic rules are (in short form):

  1. Signed and unsigned char and short are automatically converted to type int, or if necessary to unsigned int.
  2. In any operation involving two different types, both values are converted to the higher ranking of the two types.
  3. The ranking of types is long double, double, float, unsigned long, long, unsigned int, and int.
  4. In an assignment statement, the result of the calculations is converted to the type of the variable that is being assigned a value. This can lead to promotion or demotion.
  5. When passed to functions, char and short are converted to int and float is converted to double.

The C programmer can cast a variable to a designated type by using a cast operator. The syntax is:

	(data_type) variable_name

Function statements can include those with no arguments and those with arguments. When arguments are passed to a function, the function heading must include a formal parameter, as in:

	void pound(int n) 
/* Where n is the name of the parameter */
/* and int represents the type */

In addition to the function heading, the function prototype should include a type. The prototype for the function above is:

	void pound(int n);

Before leaving this chapter, students should understand the difference between a function prototype, a function call, and a called function.

A prototype is a pattern for the function that is handed to the compiler early in the program.

A call is when you use the function's name to get something done.

A called function is one that runs when it is called by something else (usually another function.)