PAGEFLOP
FORWARD
SELECTOR
SELAGAIN
WRONGSEL
YOUR SELECTION IS NOT IN THE 0 - #SELIMIT RANGE Please try again
SELRTRN
AANVANG
Copyright CourseWare Technologies Inc., 1985-88
Lesson - 2
THE C BUILDING BLOCKS
MENU
Building Blocks|Topics to Learn|2-0|15,48
# Topic
--- -------
1 - Semicolons
2 - Identifiers
3 - Braces, Standard C Library Functions
4 - Data Types
5 - Variables
6 - First Review
7 - Keywords
8 - Constants
9 - Operators and Operations
10 - Formatted Text I/O
11 - Second Review
0 - Return to the Main Menu
P1
Building Blocks|Semicolons|2-1.1|6,40
A C statement is an assignment, an
expression, or a function call.
The semicolon ( ; ) marks the end of
a C program statement.
P2
Building Blocks|Semicolons|2-1.2|8,57
The following example demonstrates the use of semicolons.
main()
{
printf("This example demonstrates\n");
printf("the use of semicolons\n");
printf("to end a statement.\n");
}
P3
Building Blocks|Identifiers|2-2.1|16,52
An identifier is a sequence of letters,
digits, or an underscore:
0 through 9, a through z, A through Z, _
The first character of an identifier must be
a letter or an underscore.
Only the first eight (8) characters of an
identifier are significant.
In an identifier, upper and lower case
characters are different.
P4
Building Blocks|Identifiers|2-2.2|12,45
Legal identifiers include:
_sys_variable
_ground_speed
file_21
Illegal identifiers include:
21_century;
new_mode } these two are
new_model } not distinct !
P5
Building Blocks|Braces|2-3.1|7,46
A compound statement is a series of
statements which are syntactically equivalent
to a single statement.
Curly braces, { and } , delimit a
function body or a compound statement.
P6
Building Blocks|Braces|2-3.2|13,56
This example shows a compound statement used to delimit
the statements within a for loop. The outermost pair
of braces delimits the function body.
main()
{ (opening brace for the function)
:
for ( ... ) { (compound statement)
:
x = x + 1;
y = y + 1;
} (end compound statement)
} (function end)
P7
Building Blocks|The Standard C Library|2-3.3|11,54
Examine the statement:
printf("This is my first \"C\" program.\n");
printf() is a function name, the code of which is already
written and is a part of the Standard C Library.
The linking phase of the compilation process takes
your source files and translates them into executable
object files. During this process, the Standard C
Library is automatically searched for any functions
which are not part of your source code.
P8
Building Blocks|Data Types|2-4.1|14,50
A data type defines the type of data that
will be represented by a variable.
There are four basic data types in C:
char a character
int an integer
float a floating point number
double a double precision float
All data types must be declared prior to
being used.
P9
Building Blocks|Data Types|2-4.2|11,50
A bit (for binary digit) is the smallest unit
of storage in the computer.
The internal representation of a data type
is machine dependent. The values may be:
char 8 bits
int 16 or 32 bits
float 32 or 64 bits
double 64 or 128 bits
P10
Building Blocks|Data Types|2-4.3|16,57
In addition to the four basic data types, there
are the following data type extensions:
short [int] normally half the memory requirement
of an int
long [int] designed to use twice the memory of int
unsigned [int] shifts the range of numbers that can be
stored (i.e., 0 to 65535 instead of
-32768 to +32767)
long [float] same as double
NOTE: [int] is optional.
P11
Building Blocks|Variables|2-5.1|10,60
A variable is a quantity that may assume any one of a
set of values. Below are several variable declaration
examples.
char c, name[10]; a variable of type char
and an array of 10 characters
int size, index, line_no; three variables of type int
float debit, torque; two variables of type float
double lb_per_in, irrat_num; two double precision numbers
P12
Building Blocks|Keywords|2-7.1|11,54
Certain identifiers are keywords and may not
be used as variable names. Keywords in C include:
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
P13
Building Blocks|Constants|2-8.1|8,38
Five types of constants may be
distinguished in C:
* integer constants
* explicit long constants
* character constants
* floating point constants
* character string constants
P14
Building Blocks|Constants|2-8.2|12,54
An octal integer constant is preceded by 0
(zero). Octal digits include the digits 0 - 7.
A hexadecimal integer constant is preceded
by 0X or 0x. Hexadecimal digits include the digits
0 - 9, the letters a - f, and the letters A - F.
For example,
237 is a decimal constant
023 is an octal constant, the same as 19 decimal
0XA9 is a hexadecimal constant, the same as 169 decimal
P15
Building Blocks|Constants|2-8.3|4,46
An integer constant immediately followed
by L or l is a long constant, e.g.,
2349L
P16
Building Blocks|Constants|2-8.4|17,59
A character constant is a character enclosed in
single quotes, e.g., 'K'. The value of every character
can be represented by an integer according to the ASCII
code. ASCII values range from 0 to 127. A, for example,
is represented by 65, a by 97, 0 by 48.
Some special characters need to be preceded by a
backslash as shown below:
new line character (nl or lf) \n
horizontal tab \t
backspace (bs) \b
carriage return (cr) \r
form feed (ff) \f
backslash (\) \\
single quote (') \'
a bit pattern (ddd) \ddd (octal)
P17
Building Blocks|Constants|2-8.5|15,50
A floating point constant consists of an
integer, a decimal point, a fraction, an e or E,
and an optionally signed integer exponent. For
example,
127
0.236
2.E-16
2.37e+23
Every floating point constant is represented
internally as a double precision number. A double
precision number uses twice the number of bits as
a float.
P18
Building Blocks|Constants|2-8.6|9,54
A character string constant is a sequence of
characters enclosed by a pair of double quotes,
i.e. " ... ". The compiler adds the NULL character
'\0' at the end of each string.
NOTE: Since a " delimits a string, if you want
to use the double quote character itself as part of
the string, you must preceded it with \, that is, \"
P19
Building Blocks|Constants|2-8.7|8,54
The following example shows character string constant
declarations and their internal or machine representations.
String Internal Representation
------ -----------------------
"class" |c|l|a|s|s|\0|
"\c\"_class" |"|c|"|_|c|l|a|s|s|\0|
P20
Building Blocks|Operators|2-9.1|17,66
C operators may be classified into three categories:
arithmetic, relational, and logical.
The arithmetic operators are:
+ add
- subtract
* multiply
/ divide
% modulo (yields remainder of integer division)
The relational operators are:
> greater than
< less than
P21
Building Blocks|Operators|2-9.2|13,50
The logical operators are:
&& logical AND
|| logical OR
<< logical shift left
>> logical shift right
! logical negation
& logical bitwise AND
| logical bitwise OR
^ logical bitwise exclusive OR
- unary minus
(Note: bitwise operations act on bits)
P22
Building Blocks|Operations|2-9.3|16,57
The following example demonstrates the use of arithmetic
operators and their results. Note that all the operands
are declared as integers; therefore, all the fractional
values will be truncated.
Arithmetic Operations
int a, b, c;
a = 4; b = 7; /* initial values */
Operation Result
--------- ------
c = a + b; c = 11
c = a - b; c = -3
c = a / b; c = 0
c = a % b; c = 4
P23
Building Blocks|Operations|2-9.4|17,58
This example demonstrates the use of relational operators
and their results. Note that result 0 is interpreted in
C as FALSE and FALSE as 0. Note that any non-zero result
is considered as TRUE and vice versa. Here we use the
value of 1 to represent a TRUE condition.
Relational Operations
int a, b, c;
a = 4; b = 7; /* initial values */
Operation Result
------------- -------------
c = (a > b); c = FALSE = 0
c = (a < b); c = TRUE = 1
c = (a == b); c = FALSE = 0
c = (a != b); c = TRUE = 1
P24
Building Blocks|Operations|2-9.5|12,54
Like the relational operators, all but the
bitwise logical operators produce either a TRUE or a
FALSE result. The following examples demonstrate the
use of logical operators and their results as well as
the use of bitwise logical operators on an unsigned
integer a.
NOTE: Using signed integers in logical bit
shifts would produce garbage results. The examples
show the syntax of the operation and the results
produced.
P25
Building Blocks|Operations|2-9.6|15,54
int a = 1, b = 0, c; /* initial values */
c = (a && b); c = FALSE = 0
c = (a || b); c = TRUE = 1
c = (!a); c = FALSE = 0
-------------------------------------------------------
unsigned a = 7; (using 16 bits) 0000 0000 0000 0111
c = a << 2; c = 28 = 0000 0000 0001 1100
c = a >> 2; c = 1 = 0000 0000 0000 0001
-------------------------------------------------------
int a = 7; /* initial value */
c = ~a; c = -8 = 1111 1111 1111 1000
c = a^255; c = 248 = 0000 0000 1111 1000
c = -a; c = -7 = 1111 1111 1111 1001
OPERATION RESULT
P26
Building Blocks|Formatted Text I/O|2-10.1|11,42
Format specifiers include:
%d decimal (base 10)
%o octal (base 8)
%x hexadecimal (base 16)
%u unsigned decimal
%e scientific notation
%f decimal (double or float)
%g %e or %f, select shortest
%c single character
%s string of characters
P27
Building Blocks|Formatted Text I/O|2-10.2|16,53
The following example demonstrates the formatting
capabilities of the printf() function used with
various format specifiers.
The field length specifiers following % represent
the minimum number of fields that will be used to
represent the variable.
The number following the decimal point with the %f
specifier indicates the maximum number of fields to be
used to represent the fractional part of the result, while
the number preceding the decimal point indicates the
minimum number of right justified fields to be used to
represent the object's value.
P28
Building Blocks|Formatted Text I/O|2-10.3|13,70
char name[] = "c_class"; /* compiler will determine string length */
main()
{
int a = 27; /* compiler initialization */
float b = 137.6;
char c = 'K';
printf("a_d = %5d, a_o = %7o, a_h = %5x, a_u = %u,\n",
a,a,a,a);
printf("b_exp = %10e, b_float = %10.4f, b_gen =%g,\n",
b,b,b);
printf("c = %5c, name = %15s\n", c, name);
}
P29
Building Blocks|Formatted Text I/O|2-10.4|12,56
The output of our example:
a_d = 27, a_o = 33, a_h = 1b, a_u = 27,
b_exp = 1.376000e+02, b_float = 137.6000, b_gen =137.6,
c = K, name = c_class
Make sure you understand each of these formats before
going on to the next screen! Remember you can page
back with - <CR>