![]() |
Department of
Mathematics and Computer Science |
|
Statements in C are grouped together using curly braces, { and }, much the same way that BEGIN and END are used in Pascal. The pair of braces is used to limit the beginning and end of functions, and also to group sets of statements together to correspond to the syntax of the language.
The main program is itself a function with the name MAIN, with a general structure as follows:
main ()
{
...
}
Similar to Modula-2 and Ada, in C one must initially "borrow" i-o
functions from the standard i-o library. This is done by means
of an INCLUDE statement which is interpreted by the pre-processor.
This statement usually occurs at the head of any program code.
#include <stdio.h>As mentioned above, a pair of curly braces is used much as a BEGIN ... END pair is used in Pascal.
C has numerous data types, the most common being: int (integer), float (real), char (a single byte), short (short integer), long (long integer), double (double-precision floating point). Variable MUST be declared before being used in any block (or globally) in Fortran style, WITHOUT any reserved word as VAR, with the TYPE FIRST, and then the list of variables (separated by commas). C is not as strongly typed as Pascal, and therefore variables can change their type when needed.
C does NOT include the exponentiation operator.
Assignment is with a single equals sign (as in FORTRAN), e.g., x = 2; and all statements MUST end with a semi-colon (unlike Pascal where a semi-colon can be omitted before an END and MUST be omitted before an ELSE).
The standard logical operators which are different from Pascal are: == (for equals?), != (for not equals?), && (for AND), || (for OR), and ! (for NOT).
The FOR loop in C is much more versatile than its counterparts in Pascal, Modula-2 and Ada. The general form is:
for ( expr1 ; expr2 ; expr3 ) statement;which has the same effect as:/
expr1 ;
while ( expr2 )
{
statement;
expr3 ;
}
In other words, expr1 is an initialization expression and is
performed only once. expr2 is the test which is repeated each
time through the loop. expr3 is any expression which can be
evaluated right before the test is repeated. Normally, expr3
is a simple increment. It is also possible for any of the three
expressions to be absent!
Some structural differences/analogies:
--------------------------------|---------------------------------
PASCAL C
--------------------------------|---------------------------------
WHILE ... DO while (...)
BEGIN {
... ...
END; }
--------------------------------|---------------------------------
FOR i := 1 TO 100 DO for (i = 1; i <= 100; ++i)
BEGIN {
... ...
END; }
--------------------------------|---------------------------------
FOR i := 100 DOWNTO 1 DO for (i = 100; i >= 1; --i)
BEGIN {
... ...
END; }
--------------------------------|---------------------------------
IF ... THEN if ( ... )
BEGIN {
... ...
END }
ELSE else
BEGIN {
... ...
END; }
NOTE: The reserved word
THEN does not appear in C.
--------------------------------|---------------------------------
CASE i OF switch (i) {
a: action1; case a : action1; break;
b: BEGIN case b :
action2a; action2a;
action2b; action2b;
END; break;
c,d,e: action3; case c: case d: case e:
END; action3;
break;
default:
}
NOTE: Omitting the key word
BREAK will cause ALL the
actions to take place
sequentially after the
first action chosen!
--------------------------------|---------------------------------
C also has a do-while statement, similar to the REPEAT loop of
Pascal/Modula-2. The form is:
do
{
...
}
while ( ...)
In this structure, the loop is repeated WHILE the expression is
TRUE (unlike the Pascal REPEAT loop in which repetition is
continued as long as the expression is FALSE).
Comments are surrounded by the /* and */ double symbols (similar to Modula-2's (* and *) ).
When declaring a pointer variable, one uses an asterisk before the variable name, e.g. int *c declares c to be a pointer to an integer. To determine the address of a variable, one can use the ampersand before the variable, e.g. c = &dot (where dot is an int variable).
Records are called structures in C. Subsections of structures are referenced by the . field pointer, e.g. list.right (corresponds to list.right in Pascal).
Integer variables can be incremented and decremented by abbreviated statements: i++ (or ++i) and i-- (or --i). This quasi-operator can also be used in other expressions. For example, given:
i = 0; t = x[i++];t will receive the value of x[0] and i will have the value of 1 after t receives its value. On the other hand if ++i were used as the subscript, i would have been incremented FIRST and then the NEW value would have been used, so t would have received the value of x[1].
Strings are enclosed in DOUBLE quotes.
Identifiers must start with a letter of the alphabet and can use the underline (_) as a character (but NOT at the beginning).
Identifiers are usually all LOWER CASE, except for special constants which are usually all UPPER CASE.
Functions/procedures in C cannot be nested. Functions can appear in any order. The main program may precede or follow the other functions.
Array subscripts are assumed to start at ZERO (not 1). The array name used by itself, is considered to be a pointer variable to the giving the address of the first element of the array rather than the value of the first element.
My email address is
dsmolarski@math.scu.edu
This page last updated 16 March 2000.