Week 2 - Introduction and Programming Construct - Programming Construct

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 95

PROGRAMMING

CONSTRUCT
Pemrograman Komputer
Teknik Biomedis ITERA
C LANGUAGE ELEMENTS AND
GENERAL FORM OF C PROGRAM
(CHAPTER 2.1 & 2.4)
C Language Elements
The C program has two parts:
• Preprocessor directives
 are commands that give instructions to the C preprocessor
 begins with a number symbol (#) as its first non-blank character
 example: #include and #define

• Main function
int
main(void)
{
fu
nc
ti
on
bo
dy
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
main function
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
scanf(“%lf”, &miles);

/* Convert the distance to kilometers. */


kms = KMS_PER_MILE * miles;

/* Display the distance in kilometers. */


printf(“That equals %f kilometers.\n”,
kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int preprocessor directive
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
preprocessor directive a C
scanf(“%lf”, &miles);
program line beginning
/* Convert the distance to kilometers. */ with # that provides an
kms = KMS_PER_MILE * miles; instruction to the
preprocessor
/* Display the distance in kilometers. */
printf(“That equals %f kilometers.\n”, kms); preprocessor a system
program that modifies a C
return (0);
}
program prior to its
compilation
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */ #include directive gives


printf(“Enter the distance in miles> ”); a
scanf(“%lf”, &miles);
program access to a library
library a collection of useful
/* Convert the distance to kilometers. */
kms = KMS_PER_MILE * miles; functions and symbols that
may be accessed by a
/* Display the distance in kilometers. */ program
printf(“That equals %f kilometers.\n”, kms);
#include in this case gives
return (0);
} a program access to
standard header file
<stdio.h>
/*
* Converts distances from miles to kilometers.
*/
standard header file
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
scanf(“%lf”, &miles);
each library has a standard
header file whose name ends
/* Convert the distance to kilometers. */ with the symbols .h
kms = KMS_PER_MILE * miles;

/* Display the distance in kilometers. */


printf(“That equals %f kilometers.\n”, kms);

return (0);
}
the directive notifies the preprocessor that some names used in the
program are found in the standard header file <stdio.h>
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */ #define associates a constant


printf(“Enter the distance in miles> ”); macro with a certain value
scanf(“%lf”, &miles);
constant macro a name that is
/* Convert the distance to kilometers. */
replaced by a particular
kms = KMS_PER_MILE * miles;
constant value before the
/* Display the distance in kilometers. */ program is sent to the compiler
printf(“That equals %f kilometers.\n”, kms);

return (0);
}
#define in this case instructs the preprocessor to replace each
occurence of KMS_PER_MILE by 1.609 before compilation
begins
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int constant
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
scanf(“%lf”, &miles);

/* Convert the distance to kilometers. */


kms = KMS_PER_MILE * miles;

/* Display the distance in kilometers. */


printf(“That equals %f kilometers.\n”,
kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
main function
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
scanf(“%lf”, &miles);

/* Convert the distance to kilometers. */


kms = KMS_PER_MILE * miles;

/* Display the distance in kilometers. */


printf(“That equals %f kilometers.\n”,
kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
declarations
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
declarations the part of a
scanf(“%lf”, &miles);
program that tells the
/* Convert the distance to kilometers. */ compiler the names of
kms = KMS_PER_MILE * miles; memory cells in a program

/* Display the distance in kilometers. */


printf(“That equals %f kilometers.\n”, kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
scanf(“%lf”, &miles); executable statements
program lines that are
/* Convert the distance to kilometers. */
kms = KMS_PER_MILE * miles;
converted to machine
language instructions and
/* Display the distance in kilometers. */ executed by the computer
printf(“That equals %f kilometers.\n”, kms);

return (0);
}
executable
statements
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
scanf(“%lf”, &miles); reserved words a word that
reserved
has special meaning in C
word
/* Convert the distance to kilometers. */
kms = KMS_PER_MILE * miles; reserved words cannot be
used for other purposes
/* Display the distance in kilometers. */
printf(“That equals %f kilometers.\n”, kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void) variable declarations
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */ variable declarations statements


printf(“Enter the distance in miles> ”);
that communicate to the
scanf(“%lf”, &miles);
compiler the names of variables
/* Convert the distance to kilometers. */ in the program and the kind of
kms = KMS_PER_MILE * miles; information stored in each
variable
/* Display the distance in kilometers. */
printf(“That equals %f kilometers.\n”, kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void) variable
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”); variable a name associated with
scanf(“%lf”, &miles); a memory cell whose value can
change
/* Convert the distance to kilometers. */
kms = KMS_PER_MILE * miles;

/* Display the distance in kilometers. */


printf(“That equals %f kilometers.\n”,
kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void) data type
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”); data type a set of values and
scanf(“%lf”, &miles); operations that can be
performed on those values
/* Convert the distance to kilometers. */
kms = KMS_PER_MILE * miles;

/* Display the distance in kilometers. */


printf(“That equals %f kilometers.\n”,
kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
scanf(“%lf”, &miles);

standard /* Convert the distance to kilometers. */ standard identifier a word


identifier kms = KMS_PER_MILE * miles; having special meaning but
one that a programmer may
/* Display the distance in kilometers. */ redefine (but redefinition is
printf(“That equals %f kilometers.\n”, kms); not recommended)
return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */
input/output
function /* Get the distance in miles. */
printf(“Enter the distance in miles> ”); input/output function a C
scanf(“%lf”, &miles);
function that performs an
/* Convert the distance to kilometers. */ input or output operation
kms = KMS_PER_MILE * miles;

/* Display the distance in kilometers. */


printf(“That equals %f kilometers.\n”, kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */
input
operation /* Get the distance in miles. */
printf(“Enter the distance in miles> ”); input operation an
scanf(“%lf”, &miles); instruction that copies data
from an input device into
/* Convert the distance to kilometers. */ memory
kms = KMS_PER_MILE * miles;

/* Display the distance in kilometers. */


printf(“That equals %f kilometers.\n”, kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”); output function an
scanf(“%lf”, &miles); instruction that
displays information
/* Convert the distance to kilometers. */
output kms = KMS_PER_MILE * miles;
stored in memory
function
/* Display the distance in kilometers. */
printf(“That equals %f kilometers.\n”, kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */ assignment statement an


assignment printf(“Enter the distance in miles> ”); instruction that stores a
scanf(“%lf”, &miles); value or a computational
statements
/* Convert the distance to kilometers. */
result in a variable, and is
kms = KMS_PER_MILE * miles; used to perform most
arithmetic operations in a
/* Display the distance in kilometers. */ program
printf(“That equals %f kilometers.\n”, kms);

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”); we can also choose our own
scanf(“%lf”, &miles); identifiers (user-defined
identifier) to name memory
/* Convert the distance to kilometers. */ cells that will hold data and
kms = KMS_PER_MILE * miles; program results and to
name operations that we
/* Display the distance in kilometers. */ define (more on this in
printf(“That equals %f kilometers.\n”, kms);
Chapter 3)
return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
scanf(“%lf”, &miles);

/* Convert the distance to kilometers. */


kms = KMS_PER_MILE * miles;

/* Display the distance in kilometers. */


printf(“That equals %f kilometers.\n”,
kms);
return (0); user-defined identifier syntax rules:
} 1. must consist letters, digits, and underscores only
2. cannot begin with a digit
3. C reserved word cannot be used as an identifier
4. an identifier defined in a C librar should not be redefined
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
scanf(“%lf”, &miles);

/* Convert the distance to kilometers. */


kms = KMS_PER_MILE * miles;
special
/* Display the distance in kilometers. */
printf(“That equals %f kilometers.\n”, kms);
symbol
return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */


printf(“Enter the distance in miles> ”);
scanf(“%lf”, &miles);

/* Convert the distance to kilometers. */


kms = KMS_PER_MILE * miles;

/* Display the distance in kilometers. */


printf(“That equals %f kilometers.\n”,
kms);
return (0);
} punctuation
/*
* Converts distances from miles to kilometers. comment
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */ comment


printf(“Enter the distance in miles> ”);
scanf(“%lf”, &miles);
comment text beginning with
/* Convert the distance to kilometers. */ /* and ending with */ that
kms = KMS_PER_MILE * miles; provides supplementary
information but is ignored by
/* Display the distance in kilometers. */ the preprocessor and
printf(“That equals %f kilometers.\n”, kms); compiler

return (0);
}
/*
* Converts distances from miles to kilometers.
*/
#include <stdio.h> /* printf, scanf defitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */

/* Get the distance in miles. */ the value in parentheses, 0, is


printf(“Enter the distance in miles> ”); considered the result of
scanf(“%lf”, &miles);
function main’s execution,
/* Convert the distance to kilometers. */ and it indicates that your
kms = KMS_PER_MILE * miles; program executed without
error
return /* Display the distance in kilometers. */
statement printf(“That equals %f kilometers.\n”, kms);
return (0);
}
Uppercase and Lowercase Letters
• Case sensitive
• Names Rate, rate, and RATE are viewed by the compiler as
different identifiers.
Programs in Memory
Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program
VARIABLES AND DATA
TYPES (CHAPTER 2.2)
Variable
• Variables are the memory cells used for storing a
program’s input data and its computational results.
• The values stored in variables can change as the program
executes.
Variable Declarations
• The variable declarations in a C program
communicate to the C compiler
– the names of all variables used in a program,
– what kind of information will be stored in each variable, and
– how that information will be represented in memory.
• A variable declaration begins with an identifier (for example,
double) that tells the C compiler the type of data (such as a
real number) stored in a particular variable.
Syntax Display for Declarations
• SYNTAX
int variable_list;
double variable_list;
char variable_list;
• EXAMPLES
int count, large;
double x, y, z;
char first_initial;
char ans;
Data Types
• A data type is a set of values and operations that can be
performed on those values.
• A standard data type in C is a data type that is predefined,
such as char, double, and int.
• The standard data types double and int are used as
abstractions for the real numbers and integers (in the
mathematical sense).
Data Types
• Objects of a data type can be variables or constants.
• A positive numeric constant in a C program can be written
with or without a + sign.
• A numeric constant cannot contain a comma.
• Numeric constants in C are considered nonnegative numbers.
C views the minus sign as the negation operator rather than
as a part of the constant.
Data Types int
• The int data type is used to represent integers in C.
• Because of the finite size of a memory cell, not all integers can
be represented by type int.
• ANSI C specifies that the range of data type int must
include at least the values -32767 through 32767.
Data Types int
• We can perform common arithmetic operations (add,
subtract, multiply, divide) and compare two integers.
• Some values that can be stored in a type int
variable are
-10500 435 +15 -25 32767
Data Type double
• In C, the data type double is used to represent real numbers
(for example, 3.14159, 0.0005, 150.0).
• We can perform the common arithmetic operations (add,
subtract, multiply, and divide) and compare the numbers.
• Real numbers can be represented by scientific notation, for
example
123000 = 1.23x105 ≡ 1.23e5
0.000034 = 0.34x10-4 ≡ 0.34e-4
Data Type double
The storage area occupied by the number is divided into 3
sections:
1. Sign:
• 0 for positive numbers
• 1 for negative numbers
2. Mantissa (a binary fraction between 0.5 and 1.0)
3. Exponent (an integer)
Differences Between Numeric Types
int double(floating-point format)
• Operations are faster • Operations are slower
• Less storage space is • More storage space is needed to
needed to store the value store the value
• Operations with integers • Some loss of accuracy or round-off
are always precise error may occur when dealing
with double numbers
Differences Between Numeric Types
• The differences result from the way numbers are represented in
computer’s memory

binary number sign type int


exponent
format mantissa type double format

• Positive integers are represented by standard binary numbers, e.g. the


integer 13 is represented as 01101
• The format of type double values is analogous to scientific
notation,
real number = mantissa x 2exponent
Integer Types in C
Type Range in Typical Microprosessor
Implementation
short -32,767 ... 32,767
unsigned short 0 ... 65,535
int -2,147,483,647 ... 2,147, 483,647
unsigned 0 ... 4,294,967,295
long -2,147,483,647 ... 2,147,483,647
unsigned long 0 ... 4,294,967,295
Floating-Point Types in C
Type Approximate Range* Significant Digits*
float 10-37 ... 1038 6
Double 10-307 ... 10308 15

long double 10-4931 ... 104932 19


Data Type char
• Data type char represents an individual character value—a
letter, a digit, or a special symbol.
• Each type char value is enclosed in apostrophes (single
quotes) as shown
‘A’ ‘z’ ‘2’ ‘9’ ‘*’ ‘:’ ‘”’ ‘ ‘
The ASCII Code
• ASCII Code is a particular code that specifies the integer
representing each char value.
• The table below shows the ASCII code values for several
characters.
Character ASCII Code
‘ ’ 32
‘*’ 42
‘A’ 65
‘B’ 66
‘z’ 90
‘a’ 97
The ASCII Code
• In ASCII, the printable characters have codes from 32
(code for a blank or space) to 126 (code for the symbol -).
• The other codes represent nonprintable
control characters.
ARITHMETIC EXPRESSIONS
(CHAPTER 2.5)
Arithmetic Expressions

• To solve most programming problems, arithmetic


expressions are needed to manipulate type int and
double data.
• This section describes the operators used in arithmetic
expressions, and rules for writing and evaluating the
expressions.
Arithmetic Operators
• Each operator manipulates two operands, which may be
constants, variables, or other arithmetic expressions.
• The operators +, -, *, and / may be used with type int or
double operands.
Arithmetic Operators
Arithmetic Meaning Examples
Operator
+ addition 5 + 2 is 7
5.0 + 2.0 is 7.0
- substraction 5 – 2 is 3
5.0 – 2.0 is 3.0
* multiplication 5 * 2 is 10
5.0 * 2.0 is 10.0
/ division 5.0 / 2.0 is 2.5
5/2 is 2
% remainder 5 % 2 is 1
Division Operator (/)
• The division operator (/) computes the integral part of the
result of dividing its first operand by its second.
Results of Integer Division
3 / 15 = 0 18 / 3 = 6
15 / 3 = 5 16 / -3 varies
16 / 3 = 5 0 / 4 = 0
17 / 3 = 5 4 / 0 is undefined
Remainder Operator (%)
• The remainder operator (%) returns the integer remainder of
the result of dividing its first operand by its second.

Results of % Operation
3 % 5 = 3 5 % 3 = 2
4 % 5 = 4 5 % 4 = 1
5 % 5 = 0 15 % 5 = 0
6 % 5 = 1 15 % 6 = 3
7 % 5 = 2 15 % -7 varies
8 % 5 = 3 15 % 0 is undefined
Example 2.4
If you have p pieces of candy and c children and want to
distribute the candy equally,
• The expression p / c tells you how many pieces to give
each child. For example, if p is 18 and c is 4, give each
child 4 pieces.
• The expression p % c tells you how many pieces would
be left over (18 % 4 is 2).
Data Type of an Expression
• The data type of each variable must be specified in its
declaration. The data type of an expression depends on the
type(s) of its operands.
• For example
ace + bandage
is type int if both ace and bandage are type int;
otherwise, it is type double.
Data Type of an Expression
• In general, an expression of the form
ace arithmetic_operator bandage
is of type int if both ace and bandage are type int;
otherwise, it is type double.
• An expression that has operands of both type int and
double is a mixed-type expression. The data type of such
a mixed-type expression will be double.
Mixed-Type Assignment Statement
• Mixed-type expression is an expression with operands of
different types.
• The expression being evaluated and the variable to which it
is assigned have different data types.
Mixed-Type Assignment Statement
• Example 1
m = 3;
n = 2;
p = 2.0;
m is type int
x and p are type double
x = m/p;
thus
x = 3/2.0 = 1.5
Mixed-Type Assignment Statement
• Example 2
m = 3;
n = 2;
p = 2.0;
m and n are type int
y is type double
y = m/n;
thus
y = 3/2 = 1.0
Mixed-Type Assignment Statement
• Example 3
x is of type double
x = 9 * 0.5;
thus
x = 4.5
Mixed-Type Assignment Statement
• Example 4

n is of type int
n = 9 * 0.5;
thus
x = 4
Type Conversion through Cast
• C allows the programmer to convert the type of an
expression by placing the desired type in parentheses
before the expression, an operation called a type cast.
• Example
n = (int)(9 * 0.5);
Avoiding integer division when computing an average and rounding
a type double value by adding 0.5 and converting the result to
int.
Characters as Integers
• C permits conversion of type char to type
int and vise versa.
qmark_code = (int)’?’;
printf(“Code for ? = %d\n”, qmark_code);
• Arithmetic operations on characters are also allowed.
The expression ‘A’ + 1 adds 1 to the code for ‘A’ and its
value is the next character after ‘A’ which is ‘B’ in ASCII.
Expressions with Multiple Operators
• Unary operators take only one operand.
x = -y;
p = +x * y;
• Binary operators require two operands.
x = y + z;
z = y – x;
Expressions with Multiple Operators

• Rules for Evaluating Expressions


o Parantheses rule: All expressions in parentheses must be
evaluated separately.
o Operator precendence rule: Operators in the same
expression are evaluated in the following order:

unary +,- first


*, /, % next
binary +, - last
Expressions with Multiple Operators

• Rules for Evaluating Expressions


o Associativity rule:
Unary operators in the same subexpression and at the
same precedence level (such as + and -) are evaluated
right to left (right associativity).
Binary operators in the same subexpression and at the
same precendence level (such + and -) are evaluated left
to right (light associativity).
Expressions with Multiple Operators

• Rules for Evaluating Expressions Example


The expression
x * y * z + a / b – c * d
can be written in a more readable form using parentheses:
(x * y * z) + (a / b) – (c * d)
Numerical Inaccuracies
• Representational error (sometimes called round-off error) is an error due to
coding a real number as a finite number of binary digits.
The fraction 1/3 is 0.333333... which cannot be represented
exactly as binary numbers in the mantissa of the type double format.
• Cancellation error is an error resulting from applying an arithmetic operation
to operands of vastly different magnitudes, effect of smaller operand is lost.
If x is much larger than y, then x+y may have the same value as x (for
example, 1000.0 + 0.0000001234 is equal to 1000.0 on some
computer)
Numerical Inaccuracies
• If two very small numbers are multiplied, the result may be
too small to be represented accurately, so it will be
represented as zero. This phenomenon is called arithmetic
underflow.
• Meanwhile, if two very large numbers are multiplied, the
result may be too large to be represented. This
phenomenon is called arithmetic overflow.
ASSIGNMENTS AND SIMPLE
I/O (CHAPTER 2.3 & 2.6)
Assignment Statements
• An assignment statement stores a value or a computational result
in a variable, and is used to perform most arithmetic operations in
a program.
Example: kms = KMS_PER_MILE * miles;
• In C the symbol = is the assignment operator.
Read it as “becomes”, “gets”, or “takes the value of” rather than
“equals” because it is not equivalent to the equal sign of
mathematics.
Assignment Statements
• Effect of kms = KMS_PER_MILE * miles;
Before assignment
KMS_PER_MILE miles
kms 1.609 10.00 ?

After assignment
KMS_PER_MILE miles kms
1.609 10.00 16.090
Assignment to a char Variable
• The char variable next_letter is assigned the character
value ‘A’ by the assignment statement
next_letter = ‘A’;
• A single character variable or value may appear on the right-
hand side of a character assignment statement.
Input/Output Operations and Functions
• Data can be stored in memory in two different ways, by
assignment to a variable or by copying the data from an input
device into a variable using a function like scanf.
• The data transfer from the outside world into memory is called
an input operation.
Input/Output Operations and Functions
• As it executes, a program performs computations and stores
the results in memory. These program results can be
displayed to the program user by an output operation.
• All input/output operations in C are performed by special
program units called input/output functions.
Input/Output Operations and Functions
• The most common input/output functions are supplied as
part of the C standard input/output library stdio.h
• We gain access to the library through the
preprocessor directive
#include <stdio.h>
Input/Output Operations and Functions
• In C a function call is used to call or active a function.
• Calling a function is analogous to asking a friend to perform
an urgent task. You tell your friend what to do (but not how
to do it) and wait for your friend to report back that the task
is finished. After hearing from your friend, you can go on and
do something else.
The printf Function
• To see the result of a program execution, we must have a
way to specify what variable values should be displayed.
• The statement
printf(“That equals % f kilometers.\n”, kms);
calls function printf to display a line of program
output.
The printf Function
function name function arguments

printf(“That equals % f kilometers.\n”, kms);

format string

• A function call consists of twoprint list the function name and the function
parts:
arguments, enclosed in parentheses.
• The arguments for printf consist of a format string (in quotes) and a
print list (the variable kms).
• The function call above display the line
That equals 16.090000 kilometers.
The printf Function
function name function arguments

printf(“That equals % f kilometers.\n”, kms);

format string

print list
• A placeholder always begins with the symbol % in a format string indicating
where to display the output value.
• Including \n, which is called newline escape sequence, at the end of the
format string terminates the current output line.
The printf Function
• Placeholders in Format Strings
Placeholder Variable Type Function Use
%c char printf/scanf
%d int print/scanf
%f double printf
%lf double scanf
The scanf Function
• The statement
scanf(“%lf”, &miles);
calls function scanf to copy data from the standard input
device into the variable miles.
• In most cases, the standard input device is the keyboard.
The scanf Function

scanf(“%lf”, &miles);

number entered :

miles
The scanf Function

scanf(“%lf”, &miles);

number entered : 30.5

miles
The scanf Function

scanf(“%lf”, &miles);

number entered : 30.5

miles
The scanf Function

scanf(“%lf”, &miles);

number entered : 30.5

miles

30.5
The scanf Function

scanf(“%lf”, &miles);

• The format string “%lf” consists of a single placeholder that tells


scanf what kind of data to copy into the variable miles.
• Because the place holder is %lf, the input operation will proceed
without error only if the program user types in a number.
The scanf Function

scanf(“%lf”, &miles);

• In a call to scanf, the name of each variable that is to be given a


value is preceded by the ampersand character (&).
• The & is the C address-of operator
• The & operator tells the scanf function where to find each
variable into which it is to store a new value.
The scanf Function
• The number of input characters consumed by the scanf function depends on
the current format placeholder, which should reflect the type of the variable in
which the data will be stored.
• Placeholders in Format Strings

Placeholder Variable Type Function Use


%c char printf/scanf
%d int print/scanf
%f double printf
%lf double scanf
The return Statement
• The return statement transfers control from a function back
to the activator of the function.
• For the function main, control is transferred back to the
operating system.
• The value of expression is returned as the result of the
function execution.
The return Statement
• The last line in the main function
return (0);
transfers control from your program to the operating system.

The value in parentheses, 0, is considered the result of


function main’s execution, and it indicates that your program
executed without error.
Formatting Numbers in Program Output
• Specifying the format of numbers displayed by a C program
can be done by specifying the field width—the number of
columns to use for the display of the value.
• Field width is added between the % and the character
indicating which placeholder is used. Example:
%3d %4d %2d
Formatting Numbers in Program Output
• Displaying 234 and -234 Using Different Placeholders

Value Format Displayed Value Format Displayed


Output Output
234 %4d ▐234 -234 %4d -234
234 %5d ▐▐234 -234 %5d ▐-234
234 %6d ▐▐▐234 -234 %6d ▐▐-234
234 %1d 234 -234 %7d -234
Formatting Numbers in Program Output
• To describe the format specification for a type double value, we
must indicate both the total field width needed and the number
of decimal places desired.
• The form of the format string placeholder is
%n.mf where n is a number representing the total field
width, and m is the desired number of decimal places.
Formatting Numbers in Program Output
• Formatting Type double Values
Value Format Displayed Value Format Displayed
Output Output
3.14159 %5.2f ▐3.14 3.14159 %4.2f 3.14
3.14159 %3.2f 3.14 3.14159 %5.1f ▐▐3.1
3.14159 %5.3f 3.142 3.14159 %8.5f ▐3.14159
.1234 %4.2f 0.12 -.006 %4.2f -0.01
-.006 %8.3f ▐▐-0.006 -.006 %8.5f -0.00600
-.006 %.3f -0.006 -3.14159 %.4f -3.1416

You might also like