Title: ECE 447 - Lecture 4
1ECE 447 - Lecture 4
Review of C Differences between C and C
2Arguments for using a high level language (HLL)
1. Much shorter time of the software development,
easier to write and debug 2. Standard
library functions 3. Portable code usable over a
variety of microcontroller units (MCUs) 4.
Shorter time to learn the necessary tools, e.g.,
common front end of the most
cross-compilers 5. Draft design possible in a
hospitable environment, e.g., PC 6. Concurrent
hardware/software development, build a
little, test a little approach 7. Assembler
truly required only when the HLL does not meet
the goals of functionality, speed, and code
size 8. Using assembly code produced by the
compiler as a starting point for the
optimization
3Review of C (1)
Program structure
Include header files include ltfilename.hgt
a) constants, macros, type definitions,
structure/union definitions, etc. b)
declarations of functions and external variables
Local constants, macros, type definitions
Definitions of global variables
Definitions of functions
Main function
int main() ...
4Review of C (2)
Functions
return_type function_name (argument
declaration) declarations and statements
1. Called by name with parameter list, e.g.,
A HighestNumber(B, C) 2. Any function can
be invoked or called by any other 3. Cannot be
defined inside another function 4. If no
return_type is specified, int is assumed 5. If
the function is not declared before it is
invoked, it may return the wrong type 6.
Local variables come into existence when the
function is called, and disappear when the
function is left
5Review of C (3)
Function parameters
Parameters are passed by value 1. When the
function is invoked the parameters may be
expressions 2. All expressions are evaluated
before a function is called 3. The order of
evaluation is unspecified 4. Value of the
argument may be a pointer
Functions can be called recursively if they do
not depend on global variables.
6Review of C (4)
Declarations and definitions
Definitions set aside storage and possibly
values in the executable code
You can define only once Declarations only
specify types associated with storage or
functions You
can declare as often as necessary
int Square(int x) int y yxx
return y int Square(int x) extern int
Square(int x)
int z
extern int z
7Review of C (5)
Scope of variables
global variables
Int Numb1 void Function1 (float Numb2, int
Numb3) float Intermediate ..
local variable
1. Local variables cannot be accessed from the
outside of the function in which they are
declared 2. Local variables are destroyed on
exit 3. Function called from within another
function cannot access variables defined in
the outer function unless their pointers are
passed
8Review of C (6)
Common errors (1)
1. Assignment vs. comparison
if (AB) not if
(AB) 2. Passing value when address is
required int a, b scanf (d,
d, a, b) not scanf(d, d, a, b)
9Review of C (7)
Common errors (2)
3. Omitting () in function calls int
Name1 int Name1(void) ... int
Name2 Name2 Name1() not Name2 Name1
10Review of C (8)
Common errors (3)
4. Separating indices in multidimensional arrays
by commas int ArrayOne57 int RowIndex,
ColumnIndex Numb1 ArrayOneRowIndexColumn
Index not Numb1 ArrayOneRowIndex, Column
Index 5. Treating character arrays as
character pointers char StringOne char
StringTwo20 StringOne It was the best of
times not StringTwo It was the worst of
times
11C programming conventions
1. Case sensitive identifiers 2. Constants are
all upper case 3. Keywords are all lower case 4.
No convention for names of variables and
functions, but common styles a)
Capitalized first letters of words int
GetFirstValue() b) Lower case with
underscores int get_first_value 5.
Meaningful names of variables and functions
12Differences between C and C
13Differences between C and C (1)
Declarations of variables
C
C
At the top of a function block or a block
created by a pair of braces Before any
executable statement
Anywhere in the program As close to their point
of use as possible
Example
Example
double mean(double num, int size) int i
double total for(i0 iltsize i) total
numi return total/size
double mean(double num, int size) double
total for(int i0 iltsize i) total
numi return total/size
14Differences between C and C (2)
Input/Output
C
C
Allows only standard I/O functions scanf, printf
Allows I/O operators ltlt, gtgt
Example
Example
include ltstdio.hgt main() double x, y3.14
scanf(f, x) printf( the answer is f\n,
xy)
include ltstream.hgt main() double x,
y3.14 cin gtgt x cout ltlt the answer is ltlt
xy ltlt\n
15Differences between C and C (3)
Allocation of space for dynamic variables
C
C
Allows only standard functions malloc(),
calloc(), and free()
Allows new and delete operators
Example
Example
include ltstdlib.hgt char reserve_memory()
char temp temp malloc(128) return
temp
char reserve_memory() char temp temp
new char128 return temp
16Differences between C and C (4)
Passing parameters to the function
C
C
Allows only passing parameters by value
Allows reference type of parameters
Example
Example
void double_value(int ptr_x) ptr_x
2(ptr_x) .. int value 123 double_value(v
alue)
void double_value(int x) x
2x .. int value 123 double_value(value)
17Differences between C and C (5)
General differences
C
C
Classes Derived classes Friend functions Function
overloading Operators overloading Inline
expanded functions Providing parameters with
default values
18Elements of structured programming
- Use three simple structures -
- sequence,
- decision, and
- repetition
- to write all programs
- Keep program segments small to keep them
manageable - Organize the problem solution hierarchically
(top-down) - Use single-input, single-output program flow
19Find at least 8 errors in the following code in
C. Write the corrected version of the code,
which at best approximates the probable
intentions of the author.
static int counter int count() return
counter main() int i,j char
array45 char pointer char text20
scanf(d, counter) for(j1 jlt5 j)
for(i1 ilt4 i) if(ij)
pointerij arrayi, j count text
The end\n printf(s, text)