I don't see the use of ifdef/endif in the following code

This is a discussion on I don't see the use of ifdef/endif in the following code within the C Programming forums, part of the General Programming Boards category; Given the following stripped down version of some production code.. Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    I don't see the use of ifdef/endif in the following code

    Given the following stripped down version of some production code..


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MIN_LOGIN_LEN 2
    #define MAX_LOGIN_LEN 32
    int main(void)
    {
      char lid[] = "c";
    
      #ifdef MIN_LOGIN_LEN
      if (strlen(lid) < MIN_LOGIN_LEN)
        {
          fprintf(stderr,"Login name must be at least %d characters long.\n",
                  MIN_LOGIN_LEN);
          return 1;
        }
      #endif
        
      if (strlen(lid) > MAX_LOGIN_LEN)
        {
          fprintf(stderr,"Login name exceeds %d characters.\n",MAX_LOGIN_LEN);
          return 1;
        }
    
     return 0;
    }
    I get

    [cdalten@localhost oakland]$ gcc -g cap3.c -o cap3
    [cdalten@localhost oakland]$ ./cap3
    Login name must be at least 2 characters long.
    [cdalten@localhost oakland]$


    Now, when I remove both MIN_LOGIN_LEN and define MAX_LOGIN_LEN in the following..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MIN_LOGIN_LEN 2
    #define MAX_LOGIN_LEN 32
    int main(void)
    {
      char lid[] = "c";
    
      if (strlen(lid) < MIN_LOGIN_LEN)
        {
          fprintf(stderr,"Login name must be at least %d characters long.\n",
                  MIN_LOGIN_LEN);
          return 1;
        }
        
      if (strlen(lid) > MAX_LOGIN_LEN)
        {
          fprintf(stderr,"Login name exceeds %d characters.\n",MAX_LOGIN_LEN);
          return 1;
        }
    
     return 0;
    }
    [cdalten@localhost oakland]$ gcc -g cap3.c -o cap3
    [cdalten@localhost oakland]$ ./cap3
    Login name must be at least 2 characters long.

    I still get the same results. What is the point of the ifdef/endif in this case.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by Overworked_PhD
    What is the point of the ifdef/endif in this case.
    Well, there is no difference because MIN_LOGIN_LEN is defined in the first example, so the preprocessed source is effectively the same as in the second example. If you want a difference, don't define it, e.g.,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAX_LOGIN_LEN 32
    int main(void)
    {
      char lid[] = "c";
    
      #ifdef MIN_LOGIN_LEN
      if (strlen(lid) < MIN_LOGIN_LEN)
        {
          fprintf(stderr,"Login name must be at least %d characters long.\n",
                  MIN_LOGIN_LEN);
          return 1;
        }
      #endif
        
      if (strlen(lid) > MAX_LOGIN_LEN)
        {
          fprintf(stderr,"Login name exceeds %d characters.\n",MAX_LOGIN_LEN);
          return 1;
        }
    
     return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Okay, here is the snipped of code in question

    Code:
    #ifdef MIN_LOGIN_LEN
        if (strlen(lid) < MIN_LOGIN_LEN)
        {
            sprintf(msg,"Login name must be at least %d characters long.\n",
                MIN_LOGIN_LEN);
            return 1;
        }
    #endif
        
        if (strlen(lid) > MAX_LOGIN_LEN)
        {
            sprintf(msg,"Login name exceeds %d characters.\n",MAX_LOGIN_LEN);
            return 1;
        }
    
        for (c= lid; *c != '\0'; c++)
    #ifdef MIXED_CASE_LOGINS
            if (!isascii(*c) || !(isalpha(*c) || (c != lid && isdigit(*c))))
            {
                strcpy(msg,"Logins may contain only letters and digits, "
                    "and begin with a letter.\n");
                return 1;
            }
    #else
            if (!isascii(*c) || !(islower(*c) || (c != lid && isdigit(*c))))
            {
                strcpy(msg,"Logins may contain only lower-case letters and digits, "
                    "and begin with a letter.\n");
                return 1;
            }
    #endif /* !MIXED_CASE_LOGINS */
    If the ifdef/endif don't matter, then why would have Dr. Weiss, the author of the program in question, go through the hassle of having them in the first place.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by Overworked_PhD
    If the ifdef/endif don't matter, then why would have Dr. Weiss, the author of the program in question, go through the hassle of having them in the first place.
    Your premise is wrong: the #ifdef/#endif directives do matter. You are only not seeing it because the identifiers in question are defined.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    I think the point is that the #ifdef is a conditional inclusion. It seems odd that the MIN_LOGIN_LEN 2 would be #defined in the code itself. It would seem to be more reasonable that the #define would be done on the command line or in the makefile or whatever - that way you could either build the software with the min login length, or not, depending on what you wanted. By taking the #ifdef out in your second example, you are taking away the 'option' - the code is built with a minimum login length functionality. By including the #define in the first example, you are also negating the 'option,' because then the #ifdef is always true.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 04:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21