1 / 67

C from A to Z

C from A to Z. Robert Rinker and Richard Wall University of Idaho. Characteristics of C. Structured and very modular language Versatile data types and structures Comprehensive set of operators Cryptic – “programmers shorthand”

grant-henry
Télécharger la présentation

C from A to Z

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. C from A to Z Robert Rinker and Richard Wall University of Idaho University of Idaho

  2. Characteristics of C • Structured and very modular language • Versatile data types and structures • Comprehensive set of operators • Cryptic – “programmers shorthand” • Not optimizing itself but contains features that allow optimization • Originally developed for small computers • Simultaneously crude and elegant University of Idaho

  3. General Programming Rules • All C Statements are free-form • Can begin and end on any line and in any column • C statements are always terminated with a semicolon “;”. • Blank lines are ignored • Following discussion is targeted for IAR compilers University of Idaho

  4. General Programming Rules • White space (blanks, tabs and newlines) must separate keywords from other things • Comments – • All text enclosed within “/* ----- */” • Text on the same line following “//” • Examples: // This is a comment /* So is this. */ University of Idaho

  5. Key words in C University of Idaho

  6. C Data Types • int standard integer (16 bits / 2 bytes) • short short integer (8 bits / 1 byte) 1 • long long integer (32 bits / 4 bytes) 1 • unsigned unsigned integer • char character (8 bits / 1 byte) • float real number 1 • double double precision real 1 Note 1: platform dependent University of Idaho

  7. C Data Typesint constant forms • 123 standard integer constant • 123L long constant (also any integer too larger to fit in standard integer size • 123Q integer constant expressed in octal • 0X123 integer constant expressed in hexadecimal • $123 integer constant expressed in hexadecimal • 101B byte constant expressed in binary University of Idaho

  8. Variable Declarations in C • All variables must be declared before using them in a program • Variable declarations can also be used to initialize the variable. (not good practice) • int lower, upper, step; • float fahr, celsius; • int i=0; • char backslash = ‘\\’ University of Idaho

  9. Symbolic Constants in C • Constants can be defined using the #define construct • #define LOWER 0 • #define UPPER 300 • #define STEP 20 • Convention – constants in upper case, variables in lower case University of Idaho

  10. Register class in C • The register class informs the compiler that it should try to allocate this variable in a CPU register rather in memory. • The actual allocation of register variables is system dependent. If the compiler cannot comply with the register designation for whatever reason, it is ignored. • Example: register int a; University of Idaho

  11. Register class in IAR C • IAR C does not use the register designation but rather the sfr designation. • Examples • sfrw ax = 0x40; • sfrb al = 0x40; • sfrl axx = 0x40; University of Idaho

  12. Operators in C • Arithmetic operators • + Addition • - Subtraction • * Multiplication • / Division • % Modulus (remainder) University of Idaho

  13. Operators in C • Relational Operators • > Greater than • >= Greater than or equal to • < Less than • <= Less than or equal to • == Identically equal to • != Not identically equal University of Idaho

  14. Operators in C • Connective operators • && Connective AND • || Connective OR University of Idaho

  15. Boolean Values in C • There is no separate Boolean data types in C • Any variable can be treated as a Boolean value • Rules of evaluation • A numerical value of 0 (zero) represents FALSE • A numerical value other than zero represents TRUE • When a Boolean expression is evaluated, it returns: • 0 for FALSE • 1 for TRUE University of Idaho

  16. Operators in C • Bit wise logical operators • & bit wise AND • | bit wise inclusive OR • ^ bit wise exclusive OR • << left shift • >> right shift • ~ one’s complement University of Idaho

  17. Operators in C • Increment and decrement • ++n pre-increment • n++ post increment • --n pre-decrement • n-- post decrement University of Idaho

  18. Operators in C • Assignment operations • True for the following operators + - * / % << >> & ^ | • Espression of the form: e1 op = e2 is equivalent to e1 = e1 op e2 • Examples • j += 4; is the same as j = j + 4; • x *= y+1; is equivalent to x = x*(y+1) University of Idaho

  19. Quiz • What will the value of x be after executing the following code? a = 5; b = 6 if (a = b) x = 1; else x=2; University of Idaho

  20. Rules of Associatively University of Idaho

  21. The COMMA Expression The comma expression evaluates exp1 first, then exp2 second. The value of the expression is the value of the last evaluated. Example: int i=4; int j=2 j = (i++, i-j) //j = 5 - 2 University of Idaho

  22. Statements in C • Any expression can be used as a program statement by following the expression with a ‘;’ (semicolon). • Examples • fahr = celsius*9/5 + 32; • rpm = 1234*tach; omega = rpm*360/60; University of Idaho

  23. C Control Structure Sequence • Compound Statements University of Idaho

  24. C Control Structure Decision University of Idaho

  25. C Control Structure Looping University of Idaho

  26. The if – else Statement • Structure if (expression) statement_1 else statement_2 • The else part is optional • The expression is evaluated: if expression is TRUE (I.e. non zero) then statement_1. If expression is FALSE (i.e. zero) then statement_1 is executed if present. For multiple if’s, the else goes with the closest if without an else condition. University of Idaho

  27. The if – else Statement- Examples University of Idaho

  28. The switch-case Statement • Structure switch (expression) { case I1: statements; case I2: statements; case I3: statements; case In: statements; default: statements; // optional } University of Idaho

  29. The switch-case Statement con’t • Operation: the expression is evaluated; then execution continues at the statements following the case statement that matches the result or after the label default if there are no matches. If the default case does not exist, then execution continues after the last case statement. University of Idaho

  30. The switch-case Statement con’t • Execution continues through remaining cases in the switch structure unless the break instruction is encountered. If a break is encountered, then execution continues after the present switch-case instance. University of Idaho

  31. The while Statement • Structure while(expression) statement; or while(expression) { statement_1; statement_2; } University of Idaho

  32. The while Statement con’t • Operation: expression is evaluated and if TRUE then statement (or statement_1 and statement_2) is executed. The evaluation and executions sequence is repeated until the expression evaluates to be FALSE. If the expression is initially FALSE then statement is not executed at all. University of Idaho

  33. The while Statement Example while(a <= b) { a *= 4; // a = a*4; b -= 2; // b = b-2 } University of Idaho

  34. The do-while Statement • Structure do { statement; } while(expression); • Operation: Similar to the while control except that statement is executed before the expression is evaluated. This guarantees that statement is always executed at least one time even if expression is FALSE. University of Idaho

  35. The for Statement • Structure: for(expr1; expr2; expr3) { statement; } • Operation: Identical to the following sequence: expr1; while(expr2) { statement; expr3 } University of Idaho

  36. Auxiliary Control Statements • Break – Causes the innermost control structure to be exited immediately. Execution continues with the statement immediately following the control structure just exited. • Continue – Causes immediate execution of the next loop to begin. University of Idaho

  37. Anatomy of a C Array • int a[10]; Arrays always start with subscript 0. Name of array used without a subscript is a pointer-constant to the first element of the array. (What is a pointer? Hint: what is a label in assembler?) University of Idaho

  38. Functions in C – ANSI • Every function in C has the form type name (argument list, // if any) { declarations, statements, and/or expressions } University of Idaho

  39. Subroutine Arguments in C • Single variables are always passed by value • Makes a copy of the variable and passes that copy • Changing the value of the copy does not result in a changing the value of the original variable. • Example: x = get_square(y); // “y” remains unchanged int get_square(int num) { num *= num; return(num); } University of Idaho

  40. Subroutine Arguments in C • Arrays are always passed by reference • References == address of element [0]. • Example: int a[10] x = get_square(a); // or x = get_square(&a[0]); void get_square(int *num) { return(*num * *num); } University of Idaho

  41. Data Attributes in C • Scope • Internal (local) variables are only known within the module where they are declared. • External (global) variables known by by several modules (functions). University of Idaho

  42. Data Attributes in C • Longevity • automatic – internal (local) variables only – variable is “created” (instantiated) when the routine (function) is invoked and disappears when the routine is finished. • static – permanent storage allocation University of Idaho

  43. Rules of Scope • 1. Automatic variables – only exist as long as the function where they are declared is executing • 2. Arguments – same as automatic over the function to which they are passed • 3. External variables – scope extends from point within the file where they are declared to end of file. University of Idaho

  44. Function Prototypes (CS perspective) • Function prototypes allow compiler to “help” us by specifying what functions calls should look like when they are called. The compiler can then compare the prototype with the actual function call and complain if they don’t match. University of Idaho

  45. Function Prototype (COE perspective) • Prototypes tell the compiler how to arrange the variables passed to the function and how to extract data passed back from the function. Prototypes are not required if the function appears in the code before the function is called the first time. University of Idaho

  46. Function Prototypes: Example int func(int, char, int) void main(void) { : i = func(10,’a’,27) : University of Idaho

  47. C Program Structure #include < stdio.h > // default directory #include <my_headder> // project directory // Global Data Variables // Function prototypes void main(void) { // local variables University of Idaho

  48. C Program Structure con’t statement_1; statement_2; : statement_n } // end of main function // other functions University of Idaho

  49. Pointers in C • Declaration: int *ptr; // ptr is a pointer to an integer int x, y; ptr = &x; // & ... say address of x y = *ptr; // * ... say value pointed to by above two statements same as x = y; University of Idaho

  50. Pointer Variables in C • A pointer variable is a variable whoes value is the address of another variable • A pointer points to another variable • The pointer concept in C is like but more general than the concept of pointers in Pascal • A pointer is not an int; it is a completely separate data type. University of Idaho

More Related