1 / 19

ECE 447: Lecture 4

ECE 447: Lecture 4. Review of C Differences between C and C++. ECE 447: Benefits of using a High Level Language (HLL). Much shorter time of the software development, easier to write and debug Standard library functions

Download Presentation

ECE 447: Lecture 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 447: Lecture 4 Review of C Differences between C and C++

  2. ECE 447:Benefits of using a High Level Language (HLL) Much shorter time of the software development, easier to write and debug Standard library functions Portable code usable over a variety of microcontroller units (MCUs) Shorter time to learn the necessary tools, e.g., common front end of the most cross-compilers Draft design possible in a hospitable environment, e.g., PC Concurrent hardware/software development, “build a little, test a little” approach Assembler truly required only when the HLL does not meet the goals of functionality, speed, and code size

  3. ECE 447: Review of C, 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() { …... }

  4. ECE 447: Review of C, Functions return_type function_name (argument declaration) { declarations and statements } Called by name with parameter list, e.g., A=HighestNumber(B, C); Any function can be invoked or called by any other Cannot be defined inside another function If no return_type is specified, int is assumed If the function is not declared before it is invoked, it may return the wrong type Local variables come into scope when the function is called, and disappear when the function is exited

  5. ECE 447: Review of C Function Parameters Parameters are passed by value When the function is invoked the parameters may be expressions All expressions are evaluated before a function is called The order of evaluation is unspecified Value of the argument may be a pointer Recursion: Functions can be called recursively if they do not depend on global variables.

  6. ECE 447: Review of C Definitions and Declarations 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; y=x*x; return y; } int Square(int x); extern int Square(int x); int z; define declare extern int z;

  7. ECE 447: Review of C, 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

  8. ECE 447: Review of C, Common Errors 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);

  9. ECE 447: Review of C, Common Errors 3. Omitting () in function calls int Name1; int Name1(void) {…... } int Name2; Name2 = Name1(); not Name2 = Name1;

  10. ECE 447: Review of C, Common Errors 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”;

  11. ECE 447: 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

  12. ECE 447: Lecture 4 Differences between C and C++

  13. ECE 447: Declaration of Variables C++ C Anywhere in the program As close to their point of use as possible At the top of a function block or a block created by a pair of braces { } Before any executable statement Example: Example: double mean(double num[], int size) { double total; for(int i=0; i<size; i++) total += num[i]; return total/size; } double mean(double num[], int size) { int i; double total; for(i=0; i<size; i++) total += num[i]; return total/size; }

  14. ECE 447: Input/Output C++ C Allows I/O operators <<, >> Allows only standard I/O functions scanf, printf Example: Example: #include <stream.h> main() { double x, y=3.14; cin >> x; cout << “the answer is” << x*y <<“\n”; } #include <stdio.h> main() { double x, y=3.14; scanf(“%f”, &x); printf( “the answer is %f\n”, x*y); }

  15. ECE 447: Memory Allocation C++ C Allows new and delete operators Allows only standard functions malloc(), calloc(), and free() Example: Example: char * reserve_memory() { char * temp; temp = new char[128]; return temp; } #include <stdlib.h> char * reserve_memory() { char * temp; temp = malloc(128); return temp; }

  16. ECE 447: Parameter Passing C++ C Allows reference type of parameters Allows only passing parameters by value Example: Example: void double_value(int * ptr_x) { *ptr_x = 2*(*ptr_x); } ….. int value = 123; double_value(&value); void double_value(int & x) { x = 2*x; } ….. int value = 123; double_value(value);

  17. ECE 447: General Differences C C++ Classes Derived classes Friend functions Function overloading Operators overloading Inline expanded functions Providing parameters with default values

  18. ECE 447: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

  19. 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; int count() { return counter++; } main() { int i,j; char array[4][5]; char *pointer; char text[20]; scanf(“%d”, counter); for(j=1; j<=5; j++) for(i=1; i<=4; i++) { if(i=j) pointer[i+j] = array[i, j] *count; } text = “The end\n”; printf(“%s”, text); }

More Related