Programming Notes
Programming Notes
Review of C
Week 2
A C Program
/*
* Converts distance in miles to kilometers.
*/
#include <stdio.h>
#define KMS_PER_MILE 1.609
int
main(void)
{
double miles,
kms;
*/
*/
A C Program
/*
* Converts distance in miles to kilometers.
*/
#include <stdio.h>
#define KMS_PER_MILE 1.609
int
main(void)
{
double miles,
kms;
*/
*/
Preprocessor Directives
2/3/2015
Preprocessor Directives
A C program line beginning with the #
symbol that provides an instruction to the
preprocessor.
Preprocessor a system program that modifies
a C program prior to its compilation
Two most common directives:
#include
#define
Structured Programming Using Procedural Languages I NSS 225 Ov erview of C
2/3/2015
A C Program
/*
* Converts distance in miles to kilometers.
*/
#include <stdio.h>
#define KMS_PER_MILE 1.609
int
main(void)
{
double miles,
kms;
*/
*/
A C Program
/*
* Converts distance in miles to kilometers.
*/
#include <stdio.h>
#define KMS_PER_MILE 1.609
int
main(void)
{
double miles,
kms;
*/
*/
Comments
Program Comments
Programmers can add additional descriptions
and explanations on their processes via
Comments inside of their code.
The /* and */ symbols are used to identify
the comment. Text between these symbols
is ignored by the Preprocessor & Compiler.
For example:
/* This will be ignored during the compile. */
Structured Programming Using Procedural Languages I NSS 225 Ov erview of C
2/3/2015
Comments
/*
* Programmer: Troy Zinderman
* Date Completed: September 23, 2005
* Class: INSS209
*
* Calculates final grade of students based
* on assignment weights
*
Must be included in all
*/
project source code
submissions.
A C Program
/*
* Converts distance in miles to kilometers.
*/
#include <stdio.h>
#define KMS_PER_MILE 1.609
int
main(void)
{
double miles,
kms;
*/
*/
2/3/2015
A C Program
/*
* Converts distance in miles to kilometers.
*/
Main Function
#include <stdio.h>
#define KMS_PER_MILE 1.609
int
main(void)
{
double miles,
kms;
*/
*/
Main Function
Every C program contains a main
function. This marks where the
program execution begins.
A function body is comprised of 2 parts:
Declarations
Executable Statements
Function Components
Declarations is the part of a program
that tells the compiler the name of
memory cells in the program.
Executable Statements program lines
that are converted to machine
language instructions and executed
by the computer.
2/3/2015
Reserved Words
A word that has special meaning in the
C language and can not be used for
any other purpose. Some examples:
Basic coding instructions
if, else, break, switch
Part of declarations
float, int, double
Structured Programming Using Procedural Languages I NSS 225 Ov erview of C
Standard Identifiers
A word that has special meaning but
one that can be redefined by a
programmer (not a good idea).
Common functions from libraries
printf, scanf
2/3/2015
User-Defined Identifiers
Words that programmers can assign to
memory cells to hold data & program
results.
User-Defined Identifiers make
programmers easier to construct,
read, & debug.
There are syntactical rules &
recommendations for their use.
Structured Programming Using Procedural Languages I NSS 225 Ov erview of C
2.
3.
4.
5.
Back to Declarations
Most declarations in a program are for the
assignment of variables.
Variable a name associated with a memory
cell whose value can change.
Variable Declaration a statement that
communicates to the complier the name
of a variable in the program and the kind if
information stored in the variable.
2/3/2015
Variable Declaration
Variable Identifier
(Name)
double miles,
kms;
*/
*/
Comments describing
variable purpose
Data Types
A set of values and operations that can
be performed on those values
Standard Data Type a data type that is
predefined to the C language.
Enumerated Data Type a data type
that is defined by the programmer
(to be address later in the semester).
Structured Programming Using Procedural Languages I NSS 225 Ov erview of C
2/3/2015
On to Executable Statements
Assignment Statement an instruction
that stores a value or a
computational result in a variable.
Variable Identifier
Constant Macro
2/3/2015
Assignment Statements
(continued)
Executable Statements
(continued)
Input Operation an instruction that copies data from
an input device into memory.
Output Operation an instruction that displays
information stored in memory.
In the C language, most of the common input/output
functions are supplied in the standard
input/output library (stdio.h)
Access to the library is acquired through a preprocessor directive:
#include
<stdio.h>
10
2/3/2015
Function Argument
Placeholder
11
2/3/2015
Escape Sequences
A series of characters used to change
the state of computers and their
attached peripheral devices .
An escape character is used to identify
an alternative interpretation of the
next character in the character
sequence.
12
2/3/2015
A Note on scanf
Microsoft has depreciated the use of
scanf within newer versions of Visual
Studio. For students using versions
above 2010, you will have to switch
to using scanf_s or add the following
line as the first preprocessor directive:
#define _CRT_SECURE_NO_WARNINGS
13
2/3/2015
Placeholder
scanf(%lf, &miles);
Address-of
operator
Address-of Operator
A operator (&) that precedes a variable
declared in the scanf function. Its
purpose is to store where the data is
stored in memory (not what is stored
in the variable).
Effect of scanf
14
2/3/2015
Arithmetic Expressions
Operator
Meaning
Addition
Subtraction
Multiplication
Division
Remainder
Structured Programming Using Procedural Languages I NSS 225 Ov erview of C
15
2/3/2015
16
2/3/2015
z - (a + b / 2) + w * -y
Control Structures
Control Structure a combination of
individual instructions into a single logical
unit with one entry point and one exit
point.
Compound Statement a group of
statements bracketed by { and } that
are executed sequentially.
Compound Statement
{
statement1;
statement2;
statement3;
.
.
.
.
.
statement n;
}
Structured Programming Using Procedural Languages I NSS 225 Ov erview of C
17
2/3/2015
Conditions
Condition an expression that is either
false (represented by 0) or true
(represented by 1).
rest_heart_rate > 75
Meaning
Type
<
Less than
Relational
>
Greater than
Relational
<=
Less than or
equal to
Greater than or
equal to
Relational
==
Equal to
Equality
!=
Not equal to
Equality
>=
Relational
18
2/3/2015
Logical Operators
Logical Expression an expression that
uses one or more of the logical
operators && (and), || (or), ! (not).
n >= 0 && n <= 100
salary < min || dependants > 5
Logical Complement
The complement of a condition has the
value 1 (true) when the conditions
value is false; the complement of a
condition has the value of 0 (false) when
the conditions value is nonzero (true).
!(0 <= n && n <= 100)
Operator Precedence
Operator
Precedence
Function Calls
! + - & (unary operators)
highest
* / %
+ < <= >= >
== !=
&&
||
=
Structured Programming Using Procedural Languages I NSS 225 Ov erview of C
lowest
Copyright 2009 Troy A. Zinderman
19
2/3/2015
Short-Circuit Evaluation
Stopping evaluation of a logical
expression as soon as its value can be
determined.
x = 0
x > 2
x > 2
y = 6
&&
||
y > 5
y > 5
Comparing Characters
Expression
Value
9 >= 0
1 (true)
a < e
1 (true)
B <= A
0 (false)
Z == z
0 (false)
a <= A
System dependant
1 (true) if ch is lowercase
Logical Assignment
Assigning the response of a logical
expression to a variable (used to identify
a true or false condition).
in_range = 1;
n = 99;
in_range = ( n > -10 && n < 10);
in_range = 0
Structured Programming Using Procedural Languages I NSS 225 Ov erview of C
20
2/3/2015
Selection
if statement allows the value of an
expression to select a course of action.
if (rest_heart_rate > 56)
printf(Keep up your exercise program./n);
else
printf(Your heart is in excellent health./n);
Types of Selection
One alternative:
if (condition)
statement;
Two alternatives:
if (condition)
statement;
else
statement;
Structured Programming Using Procedural Languages I NSS 225 Selection Structures
21
2/3/2015
Nested if Statements
An if statement with another if
statement as its true task or false task.
if (x > 0)
num_pos = num_pos + 1;
else
if (x < 0)
num_neg = num_neg + 1;
else
num_zero = num_zero + 1;
ifs in Sequence
if (x > 0)
num_pos = num_pos + 1;
if (x < 0)
num_neg = num_neg + 1;
if (x == 0)
num_zero = num_zero + 1;
22
2/3/2015
Repetition in Programs
Loop a control structure that
repeats a group of steps in a
program.
Loop Body the statements that
are repeated in the loop body.
Structured Programming Using Procedural Languages I NSS 225 Repetition & Loop Statements
23
2/3/2015
Kinds of Loops
Kind
When Used
Counting Loop
Sentinel- Controlled
Loop
Endfile-Controlled
Loop
Input Validation
Loop
General Conditional
Loop
Structured Programming Using Procedural Languages I NSS 225 Repetition & Loop Statements
Counting Loops
Counter-controlled loop a loop whose
required number of iterations can be
determined before loop execution begins.
Set loop control variable to an initial
value of zero
While loop control variable < final value
Statement1
Statement2
Statement3
:
Increase loop control variable by 1
Structured Programming Using Procedural Languages I NSS 225 Repetition & Loop Statements
count_emp = 0;
while (count_emp < 7)
{
printf("Hours> ");
Testing
Condition
scanf("%d", &hours);
printf("Rate> ");
scanf("%lf", &rate);
pay = hours * rate;
printf("Pay is $%6.2f\n", pay);
count_emp = count_emp + 1;
}
Increment
Control Variable
Structured Programming Using Procedural Languages I NSS 225 Repetition & Loop Statements
24
2/3/2015
Structured Programming Using Procedural Languages I NSS 225 Repetition & Loop Statements
Structured Programming Using Procedural Languages I NSS 225 Repetition & Loop Statements
25
2/3/2015
26
2/3/2015
total_pay = 0.0;
initialization
for (
count_emp = 0;
count_emp < number_emp;
loop repetition
count_emp += 1
condition test
)
{
printf("Hours> ");
loop control
variable change
scanf("%lf", &hours);
printf("Rate > $");
scanf("%lf", &rate);
pay = hours * rate;
printf("Pay is $%6.2f\n\n", pay);
total_pay = total_pay + pay;
}
printf("All employees processed\n");
printf("Total payroll is $%8.2f\n", total_pay);
Structured Programming Using Procedural Languages I NSS 225 Repetition & Loop Statements
operator variable
++counter
Side Effect a change in the value of a
variable as a result of carrying out an
operation.
Structured Programming Using Procedural Languages I NSS 225 Repetition & Loop Statements
Placement of Increment or
Decrement Operators
Where the operator is placed upon the variable set
the time of Increment or Decrement.
Prefix Increment the expressions value is calculated
(up or down) and then used.
++variable
--variable
variable++
variable--
Structured Programming Using Procedural Languages I NSS 225 Repetition & Loop Statements
27