1 / 31

ECT 358

ECT 358. Lecture 21 C Review. It is better to be silent and be considered a fool than to speak and remove all doubt. Yea also, when he that is a fool walketh by the way, his wisdom faileth him, and he saith to every one that he is a fool. Ecclesiastes 10:3. #include Directive.

Download Presentation

ECT 358

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. ECT 358 Lecture 21 C Review

  2. It is better to be silent and be considered a fool than to speak and remove all doubt. Yea also, when he that is a fool walketh by the way, his wisdom faileth him, and he saith to every one that he is a fool. Ecclesiastes 10:3

  3. #include Directive • The compiler needs to know where to find certain common macros and functions used in your program. For example, stdio.h is the standard input-output header file used by the C compiler. • Other include directives would be: #include <math.h> #include <stdlib.h> #include <stddef.h> #include <string.h> #include <ctype.h> #include <math.h> #include <dos.h> #include “myheader.h” Looks in current folder first.

  4. #define Directive • The compiler uses the define directive to replace every instance of an object with a value or string. For example, #define TEMP 2 replaces every occurrence of TEMP with 2. • Other define directives would be: #define D_A 0 #define A_D 1 #define START_CONVERSION 2 #define A_D_CONTROL 4 #define STATUS_BYTE 5 #define PPI_A 8

  5. main() function • The execution of your code always starts in the main function. It can be placed anywhere in the program. The end of execution of your program also ends in main(), not a function. • main() { //statement block initialize(); //comment here my_other_function(); /*comment as well */ return 0; // exit(0) }

  6. Void functiontype • The addition of void in front of a function indicates that the function does not return a value. For example void main() indicates that our program does not return a value when finished. void main() { initialize(); //comment here my_other_function(); /*comment as well */ return 0; // exit(0); }

  7. Datatypes • There are several datatypes that are commonly used. These are: char character ASCII character int integer 32 bits float 16 bits floating point double 32 bits floating point

  8. Arithmetic Operators • There are several operators that are commonly used. These are: + Addition 6+4=10 - Subtraction 6-4=2 * Multiplication 6*4=24 / Division 6/4=1 if not float % Modulus 6%4=2 (remainder)

  9. Arithmetic Assignment Operators x += y; x = x + y x -= y; x = x - y x *= y; x = x * y x /= y; x = x / y x %= y; x = x % y Preincrement: ++x; x = x + 1 --x; x = x – 1 Postincrement: y = x++; y = original x value, then x is incremented y = x--;

  10. Relational Operators x == y; equal to x != y; not equal to x > y; greater than x < y; less than x >= y; greater than or equal to x <= y; less than or equal to each returns 0 or 1 only

  11. Cast Operators x = 7, y = 5 x / y = 1 (float)x / y = 1.4

  12. Statements • for • while • do-while

  13. For Statement for(x=0;x<10;x++) PDOUT = x; for(x=0;x<10;x++) { PDOUT = x; PEOUT = 10 – x; } for(x=0;x<10;x++); //null statement !watch out with the semi-colon!

  14. Multiple Expression For Statement for(x=0, y=10; x<9, y>=0; x++, y--) { PDOUT = x; PEOUT = 10 – y; } !watch out with the commas and semi-colon!

  15. Other For Statements for( ; ; ) //infinite loop { } for(c = 0; c != 0xFF; ) { c = PDIN; }

  16. While Statement while(c != 0xFF) { c = PDIN; } while(1) //infinite loop

  17. Do-While Statement do { c = PDIN; } while(c != 0xFF); Initial statements get run at least once, unlike in if and while statements.

  18. Nested Loops for(x=0;x<10;x++) { for(y=0;y<10;y++) { PDOUT = x+y; } }

  19. Other operators x = sizeof(y) //if y is an integer, x = 4 (bytes) if (x == 3 && y == 4) //logical AND if (x >0 || y > 0) //logical OR if (x == !y) //logical negation z = (x>5) ? 1 : 0 //conditional operator

  20. Bit manipulation operators & bitwise AND | bitwise OR ^ bitwise XOR ~ bitwise complement >> right shift z = y >> 5 << left shift z = x << 2

  21. Signed and Unsigned Modifiers signed int b; unsigned char a; Used to make sure a number is treated in the signed or unsigned fashion. signed range (-128 – 127) unsigned range (0 – 255)

  22. Short and Long Modifiers short int b; //16 bits instead of 32 short b; (short int b) unsigned short int b; //16 bits instead of 32 long int a; //32 bits long c; (long int c) default = signed short int

  23. Available Header Files assert.h ctype.h errno.h ez8.h float.h format.h limits.h math.h setjmp.h sio.h stdarg.h stddef.h stdio.h stdlib.h string.h strings.h

  24. Math Functions #include <math.h> sin(x) sinh(x) asin(x) cos(x) cosh(x) acos(x) tan(x) tanh(x) atan(x) pow(x,y) //x^y sqrt(x)

  25. If - Else Statement if(x > 0) { } if((i == 0) && (j >= 34)) { } else else if (i>3) { }

  26. Switch Statement switch(x) { case ‘0’: y=1; break; case ‘1’: y=2; break; case ‘2’: y=3; break; default: y=0; break; } Break statement allows you to exit the switch construct after the statements have been run. The break statement also stops infinite loops.

  27. Continue Statement for(x=0;x<10;x++) { if(x == 4) continue; //jumps back to top sum += x; }

  28. Goto Statement x=3; Label: y=3; w=2; j += 1; if(j<23) goto Label; //function scope w=1;

  29. Arrays int x[3]; //x[0] x[1] x[2] for(y=0;y<3;y++) x[y]=0; int z[3][3]; //multidimensional array

  30. Scope of Variables int z; //global variable with program scope int test() { //local variable with block scope int x; //temporary duration static int w; //permanent duration { int x; } }

  31. Specifiers and Modifiers register int i; //reserves a register for i extern int y; //program B, declared as global in program A. const int x = 34; //can’t change in program volatile int z; //changed by another program or interrupt

More Related