home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / std / c / 3062 < prev    next >
Encoding:
Text File  |  1992-11-24  |  1.3 KB  |  43 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!csn!zeppo.uccs.edu!sanluis!dbbergac
  2. From: dbbergac@sanluis (Dave Bergacker)
  3. Newsgroups: comp.std.c
  4. Subject: Return values from recursive function
  5. Date: 23 Nov 1992 23:52:36 GMT
  6. Organization: University of Colorado at Colorado Springs
  7. Lines: 31
  8. Message-ID: <1erqs4INN7aa@zeppo.uccs.edu>
  9. NNTP-Posting-Host: sanluis.uccs.edu
  10. X-Newsreader: TIN [version 1.1 PL6]
  11.  
  12.  
  13. Hi, I teach a 'C' class, and in a recent assignment, a student turned in
  14. code that looks like:
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. int main(void)
  20. {
  21.   char *s1 = "mom", *s2 = "dad";
  22.   int streq(const char *s1, const char *s2);
  23.  
  24.   printf("Comparing strings %s and %s\n",s1,s2);
  25.   printf("The result was %d\n",streq(s1,s2));
  26.   printf("The C strcmp function returns %d\n",strcmp(s1,s2));
  27. }
  28.  
  29. int streq(const char *s1, const char *s2)
  30. {
  31.   if ( !*s1 || ( *s1 != *s2 )) return *s1 - *s2;
  32.   else streq(++s1,++s2);
  33. }
  34.  
  35. Notice that there was no explicit return from the else branch, yet this
  36. code worked on Borlands TC (version unknown) and gcc version 2.2.2.  Does
  37. ANSI specify anything about this behavior? Regretably I do NOT have a
  38. copy of the standard.
  39. --
  40. Happy Holidays,
  41.     Dave Bergacker
  42. The opinions expressed are not those of UCCS, nor should they be.
  43.