Vous êtes sur la page 1sur 10

ECE 447 - Lecture 4

Review of C
Differences between
C and C++

Arguments 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

1
Text editor
Project
C source .c file
{name}.code
Cross-compiler
Cc11 Startup
Assembly code
Assembly source {name}start.s
source .s11 .s11
Cross-assembler Cross-assembler Cross-assembler
As11 As11 As11

Relocatable Object Relocatable .o11 Object


object .o11 listing .lst object .lst
listing
Vendor
libraries Relocatable Object
{lib_name}.a11 Linker object listing
Linker
Ild11 .o11 .lst
command file
User libraries
.ld
{lib_name}.a11 Non-relocatable
executable program
{name}.e11

Review of C (1)
Program structure
Include header files #include <filename.h>
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()
{
…...
}

2
Review 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. Auto variables come into existence when the function is called,
and disappear when the function is left

Review 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.

3
Review 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; int z;
y=x*x;
return y;
}

int Square(int x);


extern int Square(int x); extern int z;

Review of C (5)
Scope of variables
Int Numb1; global variables

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

4
Review of C (6)
Common errors (1)

1. Assignment vs. comparison


= ==

if (A==B) {}
not if (A=B) {}

2. Passing value when address is required

int a, b;
scanf (%d, %d”, &a, &b);
not scanf(“%d, %d”, a, b);

Review of C (7)
Common errors (2)

3. Omitting () in function calls

int Name1;

int Name1(void)
{…...
}

int Name2;

Name2 = Name1();
not Name2 = Name1;

5
Review of C (8)
Common errors (3)

4. Separating indices in multidimensional arrays by commas

int ArrayOne[5][7];
int RowIndex, ColumnIndex;

Numb1 = ArrayOne[RowIndex][Column Index];


not Numb1 = ArrayOne[RowIndex, Column Index];

5. Treating character arrays as character pointers

char *StringOne;
char StringTwo[20];

StringOne = “It was the best of times”;


not StringTwo = “It was the worst of times”;

C 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

6
Differences between
C and C++

Differences between C and C++ (1)


Declarations of variables

C++ C
At the top of a function block
Anywhere in the program
or a block created by a pair of
As close to their point of use as
braces { }
possible
Before any executable statement

Example: Example:
double mean(double num[], int size) double mean(double num[], int size)
{ {
double total; int i;
double total;
for(int i=0; i<size; i++)
total += num[i]; for(i=0; i<size; i++)
return total/size; total += num[i];
} return total/size;
}

7
Differences between C and C++ (2)
Input/Output

C++ C
Allows I/O operators <<, >> Allows only standard I/O functions
scanf, printf

Example: Example:

#include <stream.h> #include <stdio.h>

main() main()
{ {
double x, y=3.14; double x, y=3.14;

cin >> x; scanf(“%f”, &x);


cout << “the answer is” << x*y <<“\n”; printf( “the answer is %f\n”, x*y);
} }

Differences between C and C++ (3)


Allocation of space for dynamic variables

C++ C
Allows new and delete operators Allows only standard functions
malloc(), calloc(), and free()

Example: Example:
#include <stdlib.h>

char * reserve_memory() char * reserve_memory()


{ {
char * temp; char * temp;

temp = new char[128]; temp = malloc(128);


return temp; return temp;
} }

8
Differences between C and C++ (4)
Passing parameters to the function

C++ C
Allows reference type of Allows only passing parameters
parameters by value

Example: Example:

void double_value(int & x) void double_value(int * ptr_x)


{ {
x = 2*x; *ptr_x = 2*(*ptr_x);
} }
….. …..
int value = 123; int value = 123;
double_value(value); double_value(&value);

Differences 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

9
Elements 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

Find 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; scanf(“%d”, counter);

int count() for(j=1; j<=5; j++)


{ for(i=1; i<=4; i++)
return counter++; {
} if(i=j)
pointer[i+j] = array[i, j] *count;
main() }
{ text = “The end\n”;
int i,j; printf(“%s”, text);
char array[4][5]; }
char *pointer;
char text[20];

10

Vous aimerez peut-être aussi