CS 2213 Advanced Programming
Types, Operators, and Expressions in C


Read Chapter 2 of the text.
Previous Topic: Introduction to Program Development in C
Next Topic: Control Flow in C

Variable Names

Like in Java, variable names consists of letters and digits, the first being a letter.


Data Types and Sizes

We already covered this material.

Constants

We covered most of this already.

A byte-sized constant can be specified by giving its value in octal (base 8) or hexadecimal (base 16).

C also has an enumerated data type, which Java does not have.

For example:

enum months {JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
             SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER};
Something of this type can be assigned to one of the 12 constants given in the braces. (These actually have numeric values.)

Declarations

All variables must be declared before they are used.

You can use the const qualifier to indicate that a variable may not ne changed:

const double pi = 3.14159265358979;
const char msg[] = "This cannot be changed\n";
You can also use const for an array parameter:
int strlen(const char[]);
but it is up the the implementation to determine whether this will be enforced.

Arithmetic Operators

These are similar to Java

Relational and Logical Operators

These are similar to Java, but there is no boolean type.

Integer types are used with 0 being false and anything else being true.

Recall that logical expressions are evaluated left to right and evaluation stops as soon as the truth or falsehood of the result is known.

   for (i=0;i<lim-1 && (c=getchar()) != '\n' && c != EOF; i++)
      s[i] = c;
If the test i<lim-1 fails, the character will not be read in.

Type Conversion

Automatic conversion takes place in expressions.

For binary operator like =, -, * and /, fi the operands are of different type, the smaller one gets converted into the type of the larger one.

Function parameters are considered expressions, so if a function is declared to take a double parameter, you can use a char or and int and it will be converted.

Note that C does not allow more than one function with a given name.

You cannot have one version which takes an int parameter and another with the same name that takes a double parameter.

This assumes that the compiler has a prototype for the function available so that it knows to do the conversions.


Increment and Decrement Operators

These are just like in Java.

int x,n;
n = 5;
x = n++;
Will make n=6 and x=5 while
int x,n;
n = 5;
x = ++n;
Will make n=6 and x=6.

Note that these cannot be applied to an expression: (i+j)++ is illegal.


Bitwise Operators

These are essentially the same as in Java, but you may not have used them. Here are the operations:
   &    bitwise AND
   |    bitwise inclusive OR
   ^    bitwise exclusive OR
   <<   left shift
   >>   right shift
   ~    one's complement (unary)
The first three of these are important for systems programming.
We will not cover these operations in this course.

Assignment Operators and Expressions

In addition to the ordinary assignment operator, =, most binary operators have a corresponding assignment operator:
   +=
   -=
   *=
   /=
   %=
   <<=
   >>=
   &=
   ^=
   |=
Recall that an assignment operator has a value, and the value is the value assigned.

Conditional Expressions

The ? is an operator which takes 3 arguments.
The following is called a conditional expression:
    expr1 ? expr2 : expr3;
First expr1 is evaluated. If it is true (non-zero) then expt2 is evaluated and that is the value of the conditional expression. Otherwise expr3 is evaluated and its value is the value of the conditional expression.
For example:
z = (a > b) ? a : b;
set z to be the value of the conditional expression.
Note that the assignment operators have the lowest precedence, meaning they are evaluated last.
This sets z to the larger of a and b.

Precedence and Order of Evaluation

Associativity refers to the order used when the operators have the same precedence.
Usually it is left to right except for the unary operators, ? and assignment.

Some of these operators we have not yet discussed.