Before we embark on a brief tour of C's basic syntax and structure we
offer a brief history of C and consider the characteristics of the C
language.

In the remainder of the Chapter we will look at the basic aspects of C
programs such as C program structure, the declaration of variables, data
types and operators. We will assume knowledge of a high level language,
such as PASCAL.

It is our intention to provide a quick guide through similar C principles to most
high level languages. Here the syntax may be slightly different but the
concepts exactly the same.

C does have a few surprises:

Many High level languages, like PASCAL, are highly disciplined and
structured.

However beware -- C is much more flexible and free-wheeling.
This freedom gives C much more power that experienced users can
employ. The above example below (mystery.c) illustrates how bad
things could really get.

We briefly list some of C's characteristics that define the language and also
have lead to its popularity as a programming language. Naturally we will be
studying many of these aspects throughout the course.

Small size

Extensive use of function calls

Loose typing -- unlike PASCAL

Structured language

Low level (BitWise) programming readily available

Pointer implementation - extensive use of pointers for memory, array,
structures and functions.

C has now become a widely used professional language for various reasons.

It has high-level constructs.

It can handle low-level activities.

It produces efficient programs.

It can be compiled on a variety of computers.

Its main drawback is that it has poor error detection which can make it
off putting to the beginner. However diligence in this matter can pay off
handsomely since having learned the rules of C we can break them. Not many
languages allow this. This if done properly and carefully leads to the power of
C programming.

As an extreme example the following C code (mystery.c) is actually
legal C code.

Clearly nobody ever writes code like or at least should never. This piece
of code actually one an international Obfuscated C Code Contest
http://reality.sgi.com/csp/iocc
The standard for C programs was originally the features set by Brian
Kernighan. In order to make the language more internationally
acceptable, an international standard was developed, ANSI C (American
National Standards Institute).

As well as the standard arithmetic operators (+ - * /) found in most
languages, C provides some more operators. There are some notable
differences with other languages, such as Pascal.

Assignment is = i.e.i = 4; ch = `y';

Increment ++, Decrement -- which are more
efficient than their long hand equivalents, for example:-- x++ is faster
than x=x+1.

The ++ and -- operators can be either in post-fixed or pre-fixed.
With pre-fixed the value is computed before the expression is evaluated whereas
with post-fixed the value is computed after the expression is evaluated.

In the example below, ++z is pre-fixed and the w-- is post-fixed:

int x,y,w;
main()
{
x=((++z)-(w--)) % 100;
}

This would be equivalent to:

int x,y,w;
main()
{
z++;
x=(z-w) % 100;
w--;
}

The % (modulus) operator only works with integers.

Division / is for both integer and float division. So be careful.

The answer to: x = 3 / 2 is 1 even if x is declared a float!!

RULE: If both arguments of / are integer then do integer division.

So make sure you do this. The correct (for division) answer to the above is x
= 3.0 / 2 or x= 3 / 2.0 or (better) x = 3.0 / 2.0.

There is also a convenient shorthand way to express computations in C.

It is very common to have expressions like: i = i + 3 or x = x*(y + 2)

A warning: Beware of using ``='' instead of ``=='', such as writing
accidentally

if ( i = j ) .....

This is a perfectly LEGAL C statement (syntactically speaking) which copies
the value in "j" into "i", and delivers this value, which will then be
interpreted as TRUE if j is non-zero. This is called
assignment by value -- a key feature of C.

Not equals is: !=

Other operators < (less than) , > (grater than), <= (less than or equals),
>= (greater than or equals) are as usual.

Input two numbers and work out their sum, average and sum of the
squares of the numbers.

Exercise 12271

Input and output your name, address and age to an appropriate
structure.

Exercise 12272

Write a program that works out the largest and smallest values
from a set of 10 inputted numbers.

Exercise 12273

Write a program to read a "float" representing a number of
degrees Celsius, and print as a "float" the equivalent temperature in degrees
Fahrenheit. Print your results in a form such as

100.0 degrees Celsius converts to 212.0 degrees Fahrenheit.

Exercise 12274

Write a program to print several lines (such as your name and
address). You may use either several printf instructions, each with a newline
character in it, or one printf with several newlines in the string.

Exercise 12275

Write a program to read a positive integer at least equal to 3,
and print out all possible permutations of three positive integers less or equal
to than this value.

Exercise 12276

Write a program to read a number of units of length (a float)
and print out the area of a circle of that radius. Assume that the value of pi
is 3.14159 (an appropriate declaration will be given you by ceilidh - select
setup).

Your output should take the form:
The area of a circle of radius ... units is .... units.

If you want to be clever, and have looked ahead in the notes, print the message
Error: Negative values not permitted. if the input value is negative.

Exercise 12277

Given as input a floating (real) number of centimeters, print
out the equivalent number of feet (integer) and inches (floating, 1 decimal),
with the inches given to an accuracy of one decimal place.

Assume 2.54 centimeters per inch, and 12 inches per foot.

If the input value is 333.3, the output format should be:

333.3 centimeters is 10 feet 11.2 inches.

Exercise 12278

Given as input an integer number of seconds, print as output
the equivalent time in
hours, minutes and seconds. Recommended output format is something like

7322 seconds is equivalent to 2 hours 2 minutes 2 seconds.

Exercise 12279

Write a program to read two integers with the following
significance.

The first integer value represents a time of day on a 24 hour clock, so that
1245
represents quarter to one mid-day, for example.

The second integer represents a time duration in a similar way, so that 345
represents three hours and 45 minutes.

This duration is to be added to the first time, and the result printed out in
the same notation, in this case 1630 which is the time 3 hours and 45 minutes
after 12.45.

Typical output might be
Start time is 1415.
Duration is 50.
End time is 1505.