home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18412 < prev    next >
Encoding:
Text File  |  1992-12-15  |  2.7 KB  |  86 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!usc!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!mksol!mccall
  3. From: mccall@mksol.dseg.ti.com (fred j mccall 575-3539)
  4. Subject: Re: Problems with Case statements HELP!
  5. Message-ID: <1992Dec15.172351.21579@mksol.dseg.ti.com>
  6. Keywords: case help lost c not
  7. Organization: Texas Instruments Inc
  8. References: <Bz18KA.MCv@access.digex.com>
  9. Distribution: usa
  10. Date: Tue, 15 Dec 1992 17:23:51 GMT
  11. Lines: 73
  12.  
  13. In <Bz18KA.MCv@access.digex.com> jhurwitz@access.digex.com (j. hurwitz) writes:
  14.  
  15.  
  16. >Okay, I'm new to C so you'll have to forgive my lame question. Anyhoot,
  17. >I'm trying to write a program that will check for balanced braces in my
  18. >C source code. I have no problem when I do this:
  19.  
  20. >=========================== Begin Hack =============================
  21. >//programm to read a C source file and verify # of left to right braces
  22.  
  23. >But, when I try this:
  24.  
  25. >============================== Begin Hack =========================
  26. >//programm to read a C source file and verify # of left to right braces
  27.  
  28. >#include <stdio.h>
  29. >int main(int argc, char *argv[])
  30. >{
  31. >   FILE *fptr;
  32. >   int l=0, r=0;
  33. >   char ch;
  34.  
  35. >   fptr  = fopen("brachek3.c","r");
  36. >   while ((ch =getc(fptr)) !=EOF)      
  37. >   {
  38. >   switch (ch)
  39. >     {
  40. >     case '{':  l++; 
  41.                         <<---- fall through case.
  42. >     case '}':  r++; 
  43. >     }
  44. >   }
  45. >   printf("\nl= %d r= %d", l, r);
  46. >   if (l != r)
  47. >   printf("\nBraces do not match\n");
  48. >   else printf("\nBrace match !!!\n");
  49. >   fclose(fptr);
  50. >}
  51. >================================ End Hack ========================
  52. >I get an uneven number of braces at the end. I can't figure out what
  53. >I am doing wrong. I'm sure it is a minor oversight on my part. Any help
  54. >would be greatly appreciated
  55.  
  56. In C, each case is simply a label and will fall through to the end of
  57. the switch unless it encounters a 'break' statement to end it.  Each
  58. time you find a '{' you are incrementing both r and l.  Each time you
  59. find a '}' you are incrementing only r.  Given the code, r will exceed
  60. l by the number of '}' characters you find.  If things are balanced,
  61. it will be twice l.
  62.  
  63. Your switch should read:
  64.  
  65.     switch (ch) {
  66.        case '{':
  67.           l++;
  68.           break;
  69.        case '}':
  70.           l++;
  71.           break;
  72.        default:
  73.           /* Do nothing */
  74.           break;
  75.     }
  76.  
  77. A lot of people think that including the empty default (or putting a
  78. break on the default case, period) is a bit anal retentive, so handle
  79. it however you are comfortable.
  80.  
  81. -- 
  82. "Insisting on perfect safety is for people who don't have the balls to live
  83.  in the real world."   -- Mary Shafer, NASA Ames Dryden
  84. ------------------------------------------------------------------------------
  85. Fred.McCall@dseg.ti.com - I don't speak for others and they don't speak for me.
  86.