• Cart is empty.


    Total: $0

Programming Traps and Pitfalls

Here is a list of commonly encountered programming mistakes in Micro C. You can use a Lint tool such as Splint ( freeware ) or PC-Lint from Gimpel Software to catch some of these problems automatically at compile time.

1. Assigning a larger value to variable than it can hold. For example:

Variable "i" can hold a value up to 255 but the program is trying to go up to 1000. the program will not work as expected. Value of "i" will wrap around to 0 after 255.

2. C is case-sensitive. For example:

will not link correctly because main() is uppercase.

3. C uses double quotation marks for strings, not single quotes ( that BASIC uses for comments ). For example:

This will result in a compiler error.

4. Micro C needs to know the baud rate to be able to print to the serial port.

This program will build OK but will not work when downloaded to the target board because baud rate is not specified.

Use serinit() function to initialize the serial port and specify the baud rate:

5. Be careful not to mix variable names. For example:

The variable names a1 and a2 do not mean much. Hence they are easy to mix with one another. The intended formula was

result = a2 + 3*a1;

but the user mixed the variables. Use meaningful names for variables to avoid this, especially in large programs with several thousand lines of code.

6. Do not forget the arguments to print(). This is a very common error. For example:

This will print a garbage value from stack since no argument to printf is specified.

7. Micro C does integer arithmetic. 1/2 is equal to 0, not 0.5. For example:

8. Avoid infinite loops. For example:

This program will loop forever because of the ";" at the end of while statement.

9. Make sure that a pointer is initialized before it is used. For example:

This will copy the string "abc" to an undefined memory location with unpredictable results.

10. Use strcmp() to compare 2 strings, not == . For example:

This will always print "No" since it is comparing the pointers, not the actual strings.