310 likes | 464 Views
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.
E N D
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 • 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.
#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
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) }
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); }
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
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)
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--;
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
Cast Operators x = 7, y = 5 x / y = 1 (float)x / y = 1.4
Statements • for • while • do-while
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!
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!
Other For Statements for( ; ; ) //infinite loop { } for(c = 0; c != 0xFF; ) { c = PDIN; }
While Statement while(c != 0xFF) { c = PDIN; } while(1) //infinite loop
Do-While Statement do { c = PDIN; } while(c != 0xFF); Initial statements get run at least once, unlike in if and while statements.
Nested Loops for(x=0;x<10;x++) { for(y=0;y<10;y++) { PDOUT = x+y; } }
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
Bit manipulation operators & bitwise AND | bitwise OR ^ bitwise XOR ~ bitwise complement >> right shift z = y >> 5 << left shift z = x << 2
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)
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
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
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)
If - Else Statement if(x > 0) { } if((i == 0) && (j >= 34)) { } else else if (i>3) { }
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.
Continue Statement for(x=0;x<10;x++) { if(x == 4) continue; //jumps back to top sum += x; }
Goto Statement x=3; Label: y=3; w=2; j += 1; if(j<23) goto Label; //function scope w=1;
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
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; } }
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