120 likes | 144 Views
Learn basic C programming concepts, syntax, and structure. Includes sample program and project overview for hands-on learning. Essential for beginners and teams to grasp C fundamentals effectively.
E N D
Basic Overview of C andOverview of Project 1 Matthew Murach
Sample Program /* A sample program */ void main() { int a = 5; /* Variable a */ while(1) /* Infinite Loop */ { while(!start_button()); /* Program stalls until start button is pressed */ while(start_button()); /* Program stalls until start button is released */ printf(“The value of a is %d”, a); a++; if (a > 10) fd(0); else bk(0); } }
Fundamentals of C • Comments in C should use /* and corresponding */ to mark comment regions in code • // is not allowed in C • Proper usage of comments is essential for your understanding as well as fellow team members working on your code
Fundamentals of C (cont) • printf is C’s equivalent to cout in C++ • There are a number of special characters for the printf function. • %d indicates that an unsigned decimal integer will follow. • \n denotes a new line should be called • For example printf(“\n Hello %d”, 5); will print Hello 5 at the prompt
Fundamentals of C (cont.) • Basic loop operations are the same as C++ • For instance, while(a == b) {do something here} • As a shortcut while and for loops that have no functional elements can be written as follows while(a == b); /* Same as while(a == b){} */
Fundamentals of C • If/Else If/Else are the same in C as in C++ • Note that Case/Switch statements are NOT supported in Interactive C • For Example /* Assign Greater of A or B to C */ if(a < b) c=b; else c=a; • Note that if you only execute one instruction you may omit the {} and use a ; instead.
Structure of C • Note that C REQUIRES that variables be declared before the start of the body for instance, void main() void main() { { int a = 5; int a = 5; printf(\n %i); int b; /* correct */ int b; /* ERROR! */ printf(\n %i); } }
Handyboard Caveats to C • Scanf is a disallowed feature in interactive C • Single char declarations are illegal but char strings are okay. Example char a; /* wrong */ char a[1]; /* okay */ • As mentioned before case and switch is not allowed. • Octal notation is not supported
Notes on IC • IC requires DOS old style naming conventions for files i.e. 8 plus 3 for extension. load test_comb.c /* error unscore */ load testconverter.c /* error too long */ load comlock.c /* Correct */ • When Working with IC, Verify that the libraries have been properly loaded.
Project 1 overview • Essentially you are designing a very combinational lock that accepts one bit at a time (serial input) • At each iteration or state the program should run a compare operation between the actual combination and the user input.
How does the user input values? • A bank of digital inputs 7-15 is located on the handyboard. • A short between the top most pin and bottom most pin indicates a logic ‘1’ • An open port denotes a logic ‘0’ • For example, a = digital(7); /* grabs value from port 7 */
State Machine 1 1 1 1 1 1 1 0 0 1 1 0 1 0 0 0 0 0 0 0 Test Passed Test Failed