Programming Lecture 1 2009
Programming Lecture 1 2009
What is a Program?
- A program is an organized list of instructions that, when executed, causes the computer to
behave in a predetermined manner. Without programs, computers are useless.
What is Programming?
Programming is the process of instructing the computer to accomplish a particular task. It is done
with the help of a programming language. When the program is executed, the instructions will be
converted into machine language, which is the only language that the computer understands.
1
Identifiers
Manipulation of data is a basic feature of a computer’s abilities. This data must be present in the
memory (RAM) of the computer for operations to be done on it. The computer accesses this data
via memory addresses. Identifiers are required to keep track of these memory addresses
(receptacles, data containers). Identifiers allow us humans to give these ‘data containers’ more
meaningful names. This makes the task of manipulation of data more manageable.
Memory Address Perspectives:
Computer sees - AH010 D4327
2007 We see - YEAR
Internally the computer sees a HEXIDECIMAL label representing the memory location, we see
familiar titles / labels such as YEAR, FNAME, AGE, which we are able to put into some
context.
Variables
are identifiers whose value can be changed within a program
have the following attributes
• a logical name which is descriptive and useful (e.g. total_males, count,
StartDate. Poor examples are total_number_of_males_in_school (too
long) and n1 (too short and not useful).
• Names must not contain certain restricted characters/symbols.
Unacceptable names are Gross Pay (no space allowed) and 1Age
(should not begin with a digit). Case is important based on the
programming language being used.
• a data type (real, integer, string, character, boolean, date)
• a size or length
Constants
are identifiers whose value cannot be changed within the program
EXAMPLE:
Let CIRC 2 * PI * Rad
CIRC is a variable
2 is a literal
PI is a constant
Rad is a variable
2
i.e. ‘A’, ‘B’, ‘c’, ‘#’, ‘5’, ‘\’, ‘&’
String - a combination of one or more characters enclosed in
quotes, i.e. “123”, “Tony”, “lj*&^#$$”
Examples:
Age, gross_pay, NetPay (Identifiers)
120, 3.141, “Hello” (literal constants)
Pi * radius * radius, 12 + 5 * 60 / 10 – x (combination of
operands and operators)
3
The C Programming Language
Character Sets
C does not use nor require the use of every character. It uses the following:
C Form
C is considered to be a function language, thus all C programs will consist of at least one
function. It uses only 32 key words, which combine with the formal syntax to form the
programming language.
Layout of Programs
Preprocessor directives
Function prototypes
Global declarations
/* comments */
main()
{
f1()
{
Local variables to function_f1;
Statements associated with function_f1;
}
f2()
{
Local variables to function_f2;
Statements associated with function_f2;
}
.
.
.etc.
Parts of a Program
() {}
Use of the bracket set () in conjunction with function name and {} are used to delimit the
C statements that are associated with that function. { must begin the body of every
function and }must end.
Main
This is a must in every C program. It is where the program actually begins.
4
Comments
This is a note to a programmer that is placed in the source code. It is always ignored by
the compiler. This does not affect the program. Comments are used primarily to
document the meaning and purpose of the source code, so that the programmer can
remember later how it functions and how to use it. They can be placed anywhere except
in the middle of any C keyword, function name or variable name. There are 2 types of
comments:
i. /* … */
This is used for comments extending over more than one line. /* signals the
beginning of the comment and */ indicates the end.
ii. //
This is used for a comment extending over only one line. There is no signal
ending the comment. The comment ends automatically at the end of the line. //
indicates the beginning of the comment.
The #include
This causes the copy of a specified file to be included in place of the directive. There are
2 forms:
i. #include <filename>
<> indicates that the preprocessor searches for the file on the system disk. It is
used to add the standard library files to a program.
#define
This creates symbolic constants and macros. The format is:
When this line appears in a program, all subsequent occurrences of the identifier will be
automatically replaced by the replacement text before the program is compiled.
Standard Library
C has a rich collection of existing functions called the C Standard Library. These
functions are grouped together based on what they do in what are called header files.
These are included in a program using the #include. The most common header files are:
Data Types
There are 5 basic data types:
5
Variables
Format
Format
<constant data type> <variable name> = <value>
Eg.
const float PI = 3.14159;
#define PI = 3.14159
Arithmetic in C
The operators used in C are all binary operators as they each have 2 operands. Eg. a+b.
Basic Operators
The basic operators are:
Add +
Subtract -
Multiply *
Divide /
6
Assignment Operator
The assignment operator (=) places the values found at the right side of it in the variable
found on the left side. For eg. a = 10+2; means that a is holding the value 12.
Format Specifiers
These indicate the data type of the variable to be inputted or outputted.
Conversion Specifiers
Integers: d, i, o, u, x, X, l, h
Floating points: e, E, f, g, G, l, L
Characters & strings: c, s
Scan set: [scan characters], (input only)
Miscellaneous: p, n, %
Input Function
C uses many types of input functions of which scanf is the most commonly used.
Format
scanf(<formatting control string>, <other components>);
scanf processes the control string from left to right and each time it reaches a
specifier it tries to interpret what has been typed as a value. If you input multiple
values then these are assumed to be separated by white space – ie. spaces, new line or
tabs. Thus, 3 4 5 or 3
4
5
does the same.
Therefore, for scanf(“%d %d %d”, &a, &b, &c);
A scan set is a set of characters enclosed in [ ]. The set then scans the inputted
stream looking for those characters that match characters contained in the set.
Output Function
Precise output formatting is accomplished with printf. Each contains a control string that
consists of conversion specifiers, field widths, precisions and literal characters.
7
Format
printf(<control string>, <other arguments>);
Precision is the number of digits to appear after the decimal point. When G and g
are used the precision is the maximum numbers of significant digits to be printed. S
gives the maximum number of characters in the string. To use precision, place a (.)
followed by an integer representing the precision between % and the specifier.
Eg. printf(“%*.*f”,7,2,98.736);
≡ 98.74
Using Flags
Five flags are available:
- Left justify
+ Always displays sign
Space Displays space if there is no sign
# Use alternate form of specifier
0 Pad with leading zeros
printf(“%#x\n”, 1427);
≡ 0x593
8
Literals and Escape Sequences
There are certain characters that cannot be printed so easily. They must be represented
by escape sequences which is represented by a back slash (\) and a particular escape
character. These are:
Eg. printf(“\n”);
≡ new line