Overview: Fundamentals of C, Part 1: Some Simple C Programs
Overview: Fundamentals of C, Part 1: Some Simple C Programs
Overview: Fundamentals of C, part 1 What happens when you compile this program?
What happens when you execute (or run) this program?
• Some simple C programs /* Date: 18/01/2014 Version: 1.0 */
/* Program to calculate the area of a rectangle, given its length and width */
• C program syntax: identifiers, datatypes, punctuation #include <stdio.h> /* definition of printf and scanf */
int main()
• Arithmetic operators {
int length=3; /* declare and initialise length to 3 */
int width=4; /* declare and initialise width to 4 */
int area;; /* declare area,, no initialisation */
• recall: a variable in C has a name (called its identifier), a area = length * width;
memory location where it is stored,
stored a type,
type and a value:
• the interpretation of this statement is: get the current values of the
• identifier should be meaningful
e.g. length instead of data1, x, etc. variables on the right-hand side, multiply these values, and store
the result in the variable on the left-hand
left hand side.
side
• identifier, type, and value -- you decide
• memory location -- the computer decides
• general syntax of an assignment statement:
• general syntax of a variable declaration: type name; varname = expression;
• type can be int, float, char, … • varname is an already-declared variable
• expression is a piece of code that evaluates to some value
• optional (but a good idea): initialise variable at declaration
•g
general syntax:
y type
yp name = value; the computer evaluates the current value of expression and
• value can be a constant, e.g. int length = 3; stores the result in varname, overwriting the current value of
varname
• value can refer to earlier variables, e.g. could have had
int area = length * width;
5 6
• Example: write one function to get data input, one function for data int main(void)
{
output, and one or more functions to process the data. Then activate
int diameter = 3; / declare and initialise diameter to 3 *//
/*
functions in the correct order to solve the problem! float radius = diameter/2.0; /* declare and initialise radius to 1.5 */
float area = PI * radius * radius; /* declare and calculate area */
• the function main() is activated by the computer’s Operating System
(OS) when the program is run.
run /* Output the data and results to the screen */
printf("Diameter is %d\n", diameter); /* output: Diameter is 3 */
• void main(void) means: main() receives no input data from printf("Radius is %.2f\n", radius); /* output: Radius is 1.50 */
the OS before the start of program execution, and returns no output data printf("Area
printf( Area is %.4f\n",area);
%.4f\n ,area); //* output: Area is 7.0686 *//
to the OS at program termination.
return 0;
• the statements that make up a function are called the body of the function, }
and are enclosed in braces { }
9 10
Another simple C program – a more detailed look Another simple C program – importance of variables
• since radius could in general have a fractional part, it is • could have written the previous program like this:
declared as a float
int main(void)
•area must also be declared as a float, since in general it is {
gp
also a floating-point number. float radius = 3 / 2.0; /* declare and initialise radius to 1.5 */
fl t area = PI * radius
float di * radius;
di /* declare
d l and
d calculate
l l t area */
• What happens if you declare them as int instead? (rest as before)
Answer: their fractional parts are truncated (not rounded).
(i.e.
(i e dropped diameter and replaced it with its value 3). 3) This
• PI is defined as a constant, so program can’t change it. Makes results in more compact code and gives the same result as before.
the program more readable for us – computer doesn’t care! However, the above code is more difficult to understand.
• printf("Area is %.4f\n",area); means that the • variables make programs easier to read, debug, & understand.
floating-point variable area is to be written to the screen with 4
places of decimals (rounding is used if needed – as in this case). • a program can change the value off a variable i while
i the
• %.4f is a placeholder which specifies the format in program is running. The programs we’ve examined so far
which
hi h the
th corresponding
di variable
i bl area will
ill be
b output.
t t haven’t
ave t do
donee so, but we
we’ll see eexamples
a p es oof tthiss later…
ate …
11 12
Another simple C program – integer division C program syntax
• float radius=diameter/2.0; why 2.0 and not 2? • when viewed as a typed document, a natural language (e.g.
English) document consists of:
• answer: diameter and 2 are both integers, and according words
to C, the result of dividing one integer by another is also an punctuation
i
integer – even if that
h means truncating
i the
h result
l (i.e.
(i dropping
d i special symbols
the fractional part). spaces and blank lines
• if we had radius=diameter/2 , the value of radius
• when viewed as a typed document, a C program consists of:
would be 1 (3/2 = 1.5, then drop the fractional part).
keywords
• but if any of the quantities involved are floating-point, then identifiers
the entire calculation is done using floating-point arithmetic. punctuation
We can “force”
force the computer to do this by making one of the comments
quantities of type float: this is why we had 2.0 instead of 2 spaces and blank lines
13 14
15 16
Datatypes in C (contd.) Datatypes in C (contd.)
Data Type: int, long, short • floating-point can represent both integer and non-integer values
• not all
ll integers
i can be
b represented,
d due
d to finite
fi i memory constraints
i e g 1.0,
e.g. 1 0 0.05,
0 05 91.1
91 1
e.g. int: 2 bytes wide, ranging from 215 to (2151) Scientific notation: 1.0100, 5.0102, 9.11101 (mantissa power of 10)
long: 4 bytes wide, ranging from 2231 to (2311)
1) Exponential notation: 11.0e0,
0e0 5.0e
5 0e22, 99.11e1
11e1
short: 1 byte wide, ranging from 128 to 127 • number of digits the computer allows for the decimal part of the mantissa
(on some computers, int gets 4 bytes, and/or short gets 2 bytes) determines the precision of the floating-point representation.
• number of digits the computer allows for the exponent determines the
Data Type: float, double range of the floating-point representation.
• represents
t reall numbers
b
• integral part and fractional part separated by decimal point
float: for single
single-precision
precision floating point numbers
double: for double-precision floating point numbers
• float and double are datatypes for real numbers but do not
include all of them, again due to memory constraints
17 18
Strip all but 2 decimal points from a float (not rounding): 12.34567 => 12.34 Increment and decrement operators:
x++ ; is equivalent to x = x + 1;
y ; is equivalent to y = y 1;
float trimmed = ((int)(100*number))/100.0 ;
Compound
C d ((or abbreviated)
bb i t d) assignment
i t operators:
t
1234.567
x += 3; is equivalent to x = x + 3;
sum = y; is equivalent to sum = sum y;
1234
product *= z; is i equivalent
i l to product = product * z;
d /= 4.5; is equivalent to d = d / 4.5 ;
r %= 2; is equivalent to r = r % 2 ;
12.34, as required
In general: identifier operator= expression;
(this can be generalised to stripping all but N decimal points,
points for any value means identifier=identifier operator
p (
(expression);
p );
of N – details later) (Note: expression is evaluated first in these compound assignments)
29 30