Professional Documents
Culture Documents
C-Notes Unit 1,2,3
C-Notes Unit 1,2,3
C-Notes Unit 1,2,3
Bit: - - - - - - - -
Value: 128 64 32 16 8 4 2 1
Example: 1 0 1 1 0 1 1 0
00000000
00010101
00010110
CLR A 00000000
ADD A 00010101
ADD B 00010110
CISC 105 – Topic 1 10
Computer Programming –
High-Level Languages
■ In order to get around these obstacles, high-
level languages were introduced.
■ High-level languages provide a one-to-many
mapping between one high-level statement
and multiple assembly language instructions.
■ High-level language is converted to assembly
instructions by a compiler, which creates
object files. Modern compilers perform the
assembler function as well.
CISC 105 – Topic 1 11
Modern Software
Development
■ Programmers often use library functions,
which are pre-written functions provided as
part of the compiler/development toolset.
■ A library is an object file.
■ After the source code is compiled into
machine code, a linker resolves cross-
references among these object files, including
libraries. The object files are linked into an
executable file.
CISC 105 – Topic 1 12
Modern Software
Development
Source Code File Compiler
Object File
Other Object
Files
(perhaps
Linker Executable File
libraries)
Loader
#include <stdio.h>
#define KMS_PER_MILE 1.609
int main(void)
{
double miles, /* distance in miles */
kms; /* equiv. distance in kms */
/* Get the distance in miles */
printf(“Enter the distance in miles>”);
scanf (“%lf”, &miles);
return(0);
}
CISC 105 – Topic 1 21
Preprocessor Directives
#include directive –
/* Converts distances from miles to kilometers */
return(0);
}
CISC 105 – Topic 1 22
Preprocessor Directives
#include directive –
/* Converts distances from miles to kilometers */
return(0);
}
CISC 105 – Topic 1 23
What is a valid identifier?
■ Identifiers can only be composed of
letters, digits, and underscores.
■ Identifiers cannot begin with a digit.
■ Reserved words cannot be identifiers.
■ Identifiers can be as long as you want.
■ Upper and lower case letters are
different (i.e. kms and Kms are not
considered to be the same identifier).
This_is_a_long_identifier_but_is_it_valid
?8timesTheRadius
miles
Phil’s variable
kilometers_per_hou
xr
“radius”
double mile;
int counter;
#define STUDENTS_PER_SECTION 22
int num_students;
double GPA, class_GPA;
char letter_grade;
CISC 105 – Topic 1 30
Assignment Statements
■ An assignment statement is one type of
executable statement.
■ An assignment statement uses the “=“
operator, and follows the form:
variable =
expression;
■ This statement first evaluates the
expression on the right and stores the
result in the variable on the left.
CISC 105 – Topic 1 31
Assignment Statements
■ Here are some examples of assignment
statements:
■ x = 12;
■ negative_x = -x;
■ x = y + 12 + z * 5;
■ result = y;
■ Note that any variables in the right-side
expression are not modified by an assignment
statement.
function function
name arguments
format print
string list
function
arguments
CISC 105 – Topic 1 35
The Format String
and Print List
■ The format string is the text that is to be
displayed on the screen.
■ The “%” characters are called placeholders.
They indicate the display position for
variables whose values are to be displayed.
■ The variable names to be displayed are
specified in the print list and appear in the
same order as their placeholders.
format input
string list
function
arguments
CISC 105 – Topic 1 42
The Format String
■ The format string is the set of placeholders
which specify what type of data is being
input.
■ The same placeholders are used as for printf,
except for when inputting a floating-point
number. A float type still uses the “%f”,
however the double type uses the “%lf”
placeholder.
a+b (a + b) / (c * c * 4 *
d)
--------
c2 * 4d
a * (4x % y2) a * (4 * x % (y * y))
x + y(x2 + y) x + y * (x * x + y)
Increment Decrement
CISC 105 – Topic 1 63
Another Special Case:
Increment and Decrement
■ Notice that the increment operator expression
x++ is the same as saying: x += 1;
■ So, why are there two forms of each operator?
■ The answer lies in when the increment (or
decrement) operation is actually performed.
■ This distinction only occurs when the ++ (or --)
operator is used on a variable in the same
expression in which the value of the variable is
used.
y = x++; y = ++x;
printf(“x=%d, y=%d\ printf(“x=%d, y=%d\
n”,x,y); n”,x,y);
y = x; x += 1;
x += 1; y = x;
printf(“x=%d, y=%d\ printf(“x=%d, y=%d\
n”,x,y); n”,x,y);
CISC 105 – Topic 1 65
Another Special Case:
Increment and Decrement
■ The difference lies in when the expression
(x++ or ++x) gets evaluated in relation to
when the operator (++) gets performed.
■ The x++ expression is equal to the value of x
and the ++ operator is performed after the
evaluation is over.
■ The ++x expression indicates that the ++
operator is performed first (before ++x is
evaluated) and thus, is equal to the value of x
+ 1.
CISC 105 – Topic 1 66
Review
■ What is the output of the following
program fragment?
int x, y, z;
z = x++ * y – 2;
z += ++x;
y = x + z;
y = sqrt(x);
w = y; y=2.0, w=2, z=2, q=1
z = x – w;
q = sqrt(3);
printf(“y=%f,w=%d,z=%d,q=%d”,y,w,z,q);
printf(“c=%.2f,d=%.2f”,c,d);
CISC 105 – Topic 1 72
Topic 3 – The General Form
of a C Program
The main Function Header
■ Thus, a general C program looks like:
Preprocessor directives
int main()
{
variable declarations
executable statements
return(0);
}
default:
. . .
. . .
break;
}
CISC 105 – Topic 1 99
The switch Statement
switch(control variable)
■ When the switch {
statement is case (value1):
encountered, the . . .
control variable is . . .
break;
evaluated. Then, if
that evaluated value is case (value2):
equal to any of the . . .
values specified in a . . .
case clause, the break;
statements default:
immediately following . . .
the colon (“:”) begin to . . .
run. break;
}
CISC 105 – Topic 1 100
The switch Statement
switch(control variable)
{
■ These statements case (value1):
then continue to . . .
. . .
run until a break break;
statement is
encountered. case (value2):
. . .
Control then flows . . .
to the statement break;
immediately default:
following the . . .
closing brace (“}”). . . .
break;
}
CISC 105 – Topic 1 101
The switch Statement
switch(control variable)
{
■ It is important to case (value1):
remember that . . .
. . .
control will pass break;
into the next case case (value2):
clause if a break . . .
. . .
statement is not break;
encountered.
default:
. . .
. . .
break;
}
CISC 105 – Topic 1 102
The switch Statement
switch(control variable)
{
■ So, what happens if case (value1):
the control variable . . .
. . .
is not equal to any break;
of the values case (value2):
specified in the . . .
. . .
case clauses? break;
■ The default case default:
clause runs. . . .
. . .
break;
}
CISC 105 – Topic 1 103
switch Statement Example
statement1;
switch(x)
{ Which statements run
case (3):
statement2;
if:
statement3; x = 1?
case (27): x = 2?
case (1):
statement4;
x = 3?
case (2): x = 10?
statement5; x = 27?
break;
default:
statement6:
statement7:
break;
}
statement8;
CISC 105 – Topic 1 104
Topic 6 –
Repetition and Loops
The while Statement
■ In order to create loops in the C
language, the while statement can be
used. This statement follows the form:
while (condition)
{
statement1;
statement2;
. . .
}
statement.
evaluated.
CISC 105 – Topic 1 121
The do-while Statement
Loop
Yes,#1 Loop
the loops do the same #2
thing.
Is one better than the other? Why?
CISC 105 – Topic 1 124
Common Loop Errors
■ Find the error(s) in the following code:
#include <stdio.h>
int main()
{
int count, num_lines;
printf (“How many lines would you like?”);
scanf(“%d”,&num_lines);
for (count = 0; count <= num_lines; count++)
printf (“********************\n”);
return 0;
} CISC 105 – Topic 1 125
Common Loop Errors
■ Find the error(s) in the following code:
#include <stdio.h>
/* This program asks for numbers and keeps a running
sum. It terminates when –99 is entered. */
int main()
{
int sum, number;
do {
printf(“Enter a number (-99 to quit)>”);
scanf(“%d”,&number);
sum += number;
} while (number != -99);
printf (“The sum is %d.\n”,sum);
return 0;
}
CISC 105 – Topic 1 126