Week 2 - Introduction and Programming Construct - Programming Construct
Week 2 - Introduction and Programming Construct - Programming Construct
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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;
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 */
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 */
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 */
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 */
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
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
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
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);
miles
The scanf Function
scanf(“%lf”, &miles);
miles
The scanf Function
scanf(“%lf”, &miles);
miles
30.5
The scanf Function
scanf(“%lf”, &miles);
scanf(“%lf”, &miles);