PAGEFLOP
FORWARD
SELECTOR
SELAGAIN
WRONGSEL
YOUR SELECTION IS NOT IN THE 0 - #SELIMIT RANGE Please try again
AANVANG
Copyright CourseWare Technologies Inc., 1985-88
Lesson - 6
THE C PROGRAMMING LANGUAGE
MENU
C Programming|Topics to Learn|6-0|16,54
# Topic
--- -------
1 - Characteristics of the C Language
2 - Data Types, Operators, Keywords, and Comments
3 - Control Constructs
4 - Programs and Functions
5 - Formatted I/O
6 - Arrays
7 - Pointers
8 - First Review
9 - C Structures
10 - File I/O
11 - UNIX System Interface
12 - The lint utility
13 - Second Review
0 - Return to the Main Menu
P1
C Programming|Characteristics of C|6-1.1|11,54
The C programming language:
* is very concise,
* has a free format,
* is modern,
* is a general-purpose language, and
* is a middle level language.
This lesson is just an overview of C. Please see
the CTI tutorial Programming in C for more details.
P2
C Programming|Characteristics of C|6-1.2|15,54
Features of the C programming language include:
* code portability
* compiler portability
* efficient object programs
* fast execution
* easy access to facilities UNIX
* rich set of operators
* powerful and straightforward data structures
* small size
* allows separate compilation
P3
C Programming|Characteristics of C|6-1.3|18,54
Let's compare C with other languages.
* C has structured constructs which are not in all
versions of Fortran and Basic.
* C has standard system programming functions which
are only extensions in other languages.
* C has machine level type instructions, which typically
require a function call in other languages.
* C has parallel processing features, which are
available in few other languages.
* C is extremely powerful, yet readable, unlike
some languages.
* UNIX is written in C.
P4
C Programming|Data Types|6-2.1|11,60
C data types include the following:
char ASCII character
int machine integer
short 1/2 storage required for int
long twice the storage required for int
unsigned has extra bit permitting 2 x magnitude of int
float floating point number
double double precision floating point number
enum specifies values that variable can assume
void undeclared type, (integer)
P5
C Programming|Operators|6-2.2|15,54
C operators may be classified into three categories:
* arithmetic
* relational
* logical
Arithmetic operators include:
+ - add
- - subtract
* - multiply
/ - divide
% - modulo (yields remainder
of integer division)
P6
C Programming|Operators|6-2.3|16,45
Relational operators include:
> - greater than
< - less than
Logical operators include:
&& - logical AND
|| - logical OR
<< - logical shift left
>> - logical shift right
! - logical negation
& - logical bitwise AND
| - logical bitwise OR
^ - logical bitwise exclusive OR
- - unary minus
P7
C Programming|Keywords and Comments|6-2.4|15,54
Following is a list of C identifiers which
are reserved for use as keywords and may not be
used otherwise:
auto double if static
break else int struct
case entry long switch
char extern register typedef
continue float return union
default for sizeof unsigned
do goto short while
Comments in a C program must be enclosed by:
/* and */
P8
C Programming|Control Constructs|6-3.1|7,54
There are three types of control constructs in C:
* simple,
* compound: delimited by { and }
* Conditional: if-then-else and switch
P9
C Programming|Control Constructs|6-3.2|16,28
if-else and switch syntax
-------------------------
if (expression)
statement_1;
[else]
statement_2;
switch (expression) {
case C1: ........;
case C2: ........;
:
case Cn: ........;
[default:] ........;
}
P10
C Programming|Control Constructs|6-3.3|11,65
continue - causes the next iteration of the enclosing
loop (for, while, do-while) to begin
break - permits/causes the program control to exit
the innermost enclosing loop (or switch) immediately,
i.e. before the terminal value is reached
goto error - causes the program control to transfer to the
statement ERROR. For example:
error: Statement
P11
C Programming|Control Constructs|6-3.4|15,50
while for do-while syntax
-------------------------------
while (expression)
statement;
for (expression_1; expression_2; expression_3)
statement;
do
statement
:
while (expression)
P12
C Programming|Control Constructs|6-3.5|17,32
----- while example -----
/* read until new line */
c = getchar();
while (x == TRUE && c != EOF) {
if (c == '\n')
x = FALSE;
else
c = getchar();
}
----- do-while example -----
do {
x = x + y;
z = x * y;
} while ((c = z * 2 ) < 54);
P13
C Programming|Programs and Functions|6-4.1|12,50
A C program is an independent, executable unit.
The basic form of a C program is as follows:
main()
{
printf("This is a C Program");
}
NOTE: only 1 main is allowed per program.
P14
C Programming|Programs and Functions|6-4.2|16,54
A C function is a collection of C statements
to express a specific idea, similar to a procedure
in PASCAL or a subroutine in FORTRAN.
The same function may be called in several programs.
The general form of a C function is:
[result_type] function_name ([arguments])
[argument declaration]
{
Function body
[return [expression];
}
P15
C Programming|Programs and Functions|6-4.3|11,54
C functions may be compiled only as follows:
cc -c funct_name.c (-c means no linking)
This will produce an object file called funct_name.o.
The default executable image a.out is produced
when you specify:
cc funct_name.c
P16
C Programming|Programs and Functions|6-4.4|7,54
To produce the executable image converter from
two source files, func1.c and func2.c, and one
object file func3.o enter:
cc -o converter func1.c func2.c func3.o
NOTE: The -o flag must precede the executable file name.
P17
C Programming|Programs and Functions|6-4.5|11,54
The following example illustrates a program
made up of functions other than just main(). This
program performs two tasks:
* it reads a word and
* it verifies the word.
The reading is performed by calling the function
read_word() and the writing is performed by calling
the function put_word().
P18
C Programming|Programs and Functions|6-4.6|9,54
Note that the word may be delimited by an EOF
character, a tab, a new-line, a blank or by nothing
if the character string has more than 20 characters.
Also note that when passing an array as an
argument one need not specify the array bounds,
because in any case by default it is the address
of the array, not its values that are being passed.
P19
C Programming|Programs and Functions|6-4.7|17,48
#include <stdio.h>
main() /* READ/VERIFY WORD */
{
short word_len;
char word[20];
word_len = read_word(word);
put_word(word, word_len);
}
read_word(word)
char word[];
{
short i = 0, c;
while ((c = getchar()) != EOF && c != '\t'
&& c != '\n' && c != ' ' && i < 20)
word[i++] = c;
return(i);
} . . . continued on next screen
P20
C Programming|Programs and Functions|6-4.8|8,48
put_word(word, len)
char word[];
short len;
{
int i;
for (i = 0; i < len; i++)
putchar(word[i]);
}
P21
C Programming|Formatted I/O|6-5.1|16,54
The most commonly used function for formatted
input is scanf() and for formatted output is printf().
The format specifier is indicated by the %
character, which produces right-justified results.
Some conversion sequences to specify format are:
%d - decimal
%o - octal integer
%x - hexadecimal integer
%u - unsigned integer
%f - floating point number
%c - single character
%s - string
P22
C Programming|Formatted I/O|6-5.2|16,54
The following example illustrates the formatted
output facilities available with the library functions
scanf() and printf().
Note the type of the variables used and that some
of the variable were initialized in the declaration.
Those variables that were not initialized have acquired
their values through the function scanf() call.
Also note the field length specifiers following
the "%" character in front of the format specifiers. The
field length tells how many fields or columns will be
used for formatted ASCII representation of the result.
The arguments to the function scanf() must
contain pointers.
P23
C Programming|Formatted I/O|6-5.3|14,57
main()
{
int num_1 = 47, num_2= 29,
num_3 = 250, num_4;
float float_num;
char c, s[16];
scanf("%u %f %c %15s", &num_4, &float_num,
&c, s);
printf("num_1 = %5d, num_2 = %10o\n", num_1, num_2);
printf("num_3 = %5x, num_4 = %10u\n", num_3, num_4);
printf("c = %10c, s = %20s\n",c,c);
}
P24
C Programming|Formatted I/O|6-5.4|9,45
If you provide the following input to the program:
47 39.5 Z test
it will produced this output:
num_1 = 47, num_2 = 35
num_3 = fa, num_4 = 47
c = Z, s = test
P25
C Programming|Arrays|6-6.1|8,54
The following are examples of arrays:
char char_ar[10]; /* Holds 10 Characters */
int days_per_mon[12]; /* Holds 12 integers */
float forces[4]; /* 4 floating point numbers
that represent side forces */
P26
C Programming|Arrays|6-6.2|15,54
The following example shows one method for
initializing all entries of a three dimensional
array "ar_int[3][4][5]" to zero.
Note that for every change in "i", "j" changes
4 times, and for every change in "j", "k" changes
5 times.
In C, the array index ranges from 0 to n-1, where
n is the dimension. Therefore, references like the
following are incorrect.
ar_int[3][2][4] or ar_int[2][0][5]
P27
C Programming|Arrays|6-6.3|11,49
main() /* initialize an array */
{
int i,j,k;
int ar_int[3][4][5]; /* array of integers */
for ( i=0; i < 3; i++)
for (j=0; j < 4; j++)
for (k=0; k < 5; k++)
ar_int[i][j][k] = 0;
}
P28
C Programming|Pointers|6-7.1|14,47
A pointer is a variable that holds the
address of another variable.
Pointers are type bound, for instance:
int *i_ptr;
char *c_ptr;
float *f_ptr;
char *names[44];
You can obtain the address of an object by
preceding it with an & .
P29
C Programming|Pointers|6-7.2|13,54
In the following example the function exchange()
is used to exchange the values pointed to by "x" and "y".
If explicit values rather than pointers to the
values were passed to the function exchange(), the
calling function would not see the effect of the exchange.
Therefore, you must pass the pointer/address of the
variable, if the calling function is to see the new values
of the variables in the argument list.
P30
C Programming|Pointers|6-7.3|10,54
The example illustrates the string copying
function, strcpy() which is a part of the standard
C library.
Note that a "for" loop is used for copying. It
is assumed that the addresses of a string (the same as
that of the 1st character) was passed. The terminating
condition is the NULL value in the source string "str2",
i.e. the condition will evaluate to zero.
P31
C Programming|Pointers|6-7.4|17,28
exchange(x,y)
int *x, *y;
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
is called with:
exchange(&v1, &v2);
copy string pointer version
---------------------------
void strcpy(str1, str2)
{
char *str1, *str2;
}