Guides:C/C Crash Course/Finding syntax errors

From CoderGuide

Jump to: navigation, search

Back to TOC

Finding syntax errors

If the compiler finds a syntax error, it will give you hints on finding those errors, but sometimes the compiler can get confused and not be able to give you any help. All it will know is that you made an error somewhere. If you're using a text editor that has syntax highlighting, such as jEdit (www.jedit.org) or Vim (www.vim.org), then it can make finding some syntax errors faster (if the colors don't look right, then you know you made a type-o).

Syntax highlighting is automatic in these programs. Here is the difference between syntax highlighting and no syntax highlighting:

Without:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#include "endoftheworld.h"

int main(){

   float a;
   char *s="Hello Word!";

   printf("I have an important message for you! %s!\n",s);
   
   fprintf(stdout,"\tHeed it well\n");

   puts("Beginning end of the world test.");
   
   while(!eotw()){
      /* Are there predictions? */
      
      if( (eotwpredict()==time(NULL)) && !eotw()){
         printf("Woah! looks like they got the date wrong again! %s and still here!\n", 
            localtime(time(NULL)));
      }/*end if(eotwpredict() ... */

      sleep(4); /*sleep for 4 seconds*/

   }/*end while(!eotw())*/

}/*end main()*/
/*****************************************************/

And with syntax highlighting:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
 
#include "endoftheworld.h"
 
int main(){
 
   float a;
   char *s="Hello Word!";
 
   printf("I have an important message for you! %s!\n",s);
 
   fprintf(stdout,"\tHeed it well\n");
 
   puts("Beginning end of the world test.");
 
   while(!eotw()){
      /* Are there predictions? */
 
      if( (eotwpredict()==time(NULL)) && !eotw()){
         printf("Woah! looks like they got the date wrong again! %s and still here!\n", 
            localtime(time(NULL)));
      }/*end if(eotwpredict() ... */
 
      sleep(4); /*sleep for 4 seconds*/
 
   }/*end while(!eotw())*/
 
}/*end main()*/
/*****************************************************/

As you can see, syntax highlighting makes it easer to read source code-- but it is no substitute for writing clean, easy to read, and fully commented code.

Here is a few hints on finding those kinds of syntax errors:

  1. Check to make sure all strings have a matching end quotation mark.
  2. Check that you have a matching number of open and closed parenthesis in your statements. Add one for every '(' you encounter, subtract one from every ')' you run into. Minimize the number of parentheses where possible. Separate groups with white spaces to make them easier to read.
  3. Count the number of open and closed braces in your functions. Add one for every '{' and subtract one for every '}' you run into. Whenever you put a '{' in your code, add a '}' right afterwards. Add a comment at the end of of '}' if the code block goes on for more than a few lines so you know who that end belongs too.
  4. If you're about ready to pull your hair out, or you haven't been able to find your error for over an hour, your brain is fried. Go do something else for 10-20 minutes, then go look at your code a again.

Most hard to find errors are from failure to close a statement correctly, and that's what really confuses a compiler. Vim will highlight mismatched parenthesis and braces in red to help you find those errors while programming. It can't tell you where the error is, only that one exists.

Aside from syntax errors, there are "logical" errors. Those are program bugs that result from a mistake in reason. No compiler can figure those out for you-- if they could, then they would be able to write programs for you :-).

One thing you can do to find logical errors, is to put printf() or puts() statements in your code to let you know what stage your program is in before it barfed. It can help a lot, especially in loops and conditional statements.

Personal tools