Like Tree2Likes
  • 1 Post By AndiPersti
  • 1 Post By Barney McGrew

Concerns about '
Like Tree2Likes
  • 1 Post By AndiPersti
  • 1 Post By Barney McGrew

Thread: Concerns about '\0' and '\n' inside a loop

' and '\n' inside a loop

This is a discussion on Concerns about '

Like Tree2Likes
  • 1 Post By AndiPersti
  • 1 Post By Barney McGrew

Thread: Concerns about '\0' and '\n' inside a loop

' and '\n' inside a loop
within the C Programming forums, part of the General Programming Boards category; Good afternoon fellas. I'm concerned about the usage of a function I wrote to discover if a string is a ...

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Concerns about '\0' and '\n' inside a loop

    Good afternoon fellas.

    I'm concerned about the usage of a function I wrote to discover if a string is a number in addition with atoi. I thought I could do this:

    Code:
    /* if it reached the null terminator, then all the chars where numbers. */
    int isanumber(char* s) { 
      int i; 
      for(i = 0; isdigit(s[i]) && s[i] != '\0'; i++) 
      ; 
      if(s[i] == '\0')
        return 1; 
      return 0;            
    }
    instead of, the only thing that worked was:

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <ctype.h> 
    
    #ifndef MAXSIZE 
      #define MAXSIZE 100
    #endif  
    
    int Atoi(char s[]);
    int isanumber(char* s);
    
    int Atoi(char s[]) 
    { 
      int i, n, sign;
      for(i = 0; isspace(s[i]); i++) 
      ; 
      sign = (s[i] == '-') ? -1 : 1; 
      if(s[i] == '+' || s[i] == '-') 
         i++;
      for(n = 0; isdigit(s[i]); i++) 
         n = 10 * n + (s[i] - '0');
      return sign * n; 
    }
    
    /* if it reached the line feed, then all the chars where numbers. */
    
    int isanumber(char* s) { 
      int i; 
      for(i = 0; isdigit(s[i]) && s[i] != '\n'; i++) 
      ; 
      if(s[i] == '\n')
        return 1; 
      return 0;            
    }     
    
    int main(void) 
    { 
      char* strnum = malloc(MAXSIZE * sizeof(char));
      int num;
      
      do { 
      printf("Enter a number: ");
      if(fgets(strnum, MAXSIZE, stdin))
       num = Atoi(strnum); 
      } while(!(isanumber(strnum))); 
       
      printf("You entered: %d\n", num);
      
      free(strnum);
      return 0;    
    }
    how's that?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What will happen if i input a negative number, for example -5, in your isnumber function?

  3. #3
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    I wonder if I fixed it ... I tested with a negative number and used valgrind.

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <ctype.h> 
    
    #ifndef MAXSIZE 
      #define MAXSIZE 100
    #endif  
    
    int Atoi(char s[]);
    int isanumber(char* s);
    
    int Atoi(char s[]) 
    { 
      int i, n, sign;
      for(i = 0; isspace(s[i]); i++) 
      ; 
      sign = (s[i] == '-') ? -1 : 1; 
      if(s[i] == '+' || s[i] == '-') 
         i++;
      for(n = 0; isdigit(s[i]); i++) 
         n = 10 * n + (s[i] - '0');
      return sign * n; 
    }
    
    /* if it reached the line feed, then all the chars where numbers. */
    
    int isanumber(char* s) { 
      
      int i = 0; 
      
      while(s[i] != '\n') 
      { 
         if(isalpha(s[i]) || isblank(s[i]) )
           break;
         
         i++;    
      }        
      
      if(s[i] == '\n')
        return 1; 
      return 0;            
    }     
    
    int main(void) 
    { 
      char* strnum = malloc(MAXSIZE * sizeof(char));
      int num;
      
      do { 
      printf("Enter a number: ");
      if(fgets(strnum, MAXSIZE, stdin))
       num = Atoi(strnum); 
      } while(!(isanumber(strnum))); 
       
      printf("You entered: %d\n", num);
      
      free(strnum);
      return 0;    
    }

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    It seems ok.Did you test it and find any problem?

    The only thing that comes to mind, is that suppose that a user inputs an integer, he can overflow you.In the limits.h there are defined the maximum and minimum numbers an integer can get.

  5. #5
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    he can overflow you
    this has been worrying me a bit... do I have to use the constants from limits.h every time I want to protect my program from overflows ?

    I did this:

    Code:
    #ifndef MAXSIZE
        #define MAXSIZE UCHAR_MAX
    #endif
    is that right?
    Last edited by thames; 11-12-2012 at 11:49 AM.

  6. #6
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    I don't know how to properly guard that code from overflow.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by thames View Post
    this has been worrying me a bit... do I have to use the constants from limits.h every time I want to protect my program from overflows ?

    I did this:

    Code:
    #ifndef MAXSIZE
        #define MAXSIZE UCHAR_MAX
    #endif
    is that right?
    UCHAR_MAX is the maximum value of an unsigned char (usually 255). So why do you want to limit the size of your string to it?

    If you are concerned about overflow, just use the standard function strtol() (or strtoll() since C99) which handles overflow when converting numbers (strtol() returns LONG_MAX and sets errno to ERANGE if there is an overflow).

    Bye, Andreas
    thames likes this.

  8. #8
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    many thanks Andreas

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <ctype.h> 
    
    #ifndef MAXSIZE 
       #define MAXSIZE 1000
    #endif
    
    int Atoi(char s[]);
    int isanumber(char* s);
    
    int Atoi(char s[]) 
    { 
      int i, n, sign;
      for(i = 0; isspace(s[i]); i++) 
      ; 
      sign = (s[i] == '-') ? -1 : 1; 
      if(s[i] == '+' || s[i] == '-') 
         i++;
      for(n = 0; isdigit(s[i]); i++) 
         n = 10 * n + (s[i] - '0');
      return sign * n; 
    }
    
    /* if it reached the line feed, then all the chars where numbers. */
    
    int isanumber(char* s) { 
      
      int i = 0; 
      
      while(s[i] != '\n') 
      { 
         if(isalpha(s[i]) || isblank(s[i]) )
           break;
         
         i++;    
      }        
      
      if(s[i] == '\n')
        return 1; 
      return 0;            
    }     
    
    int main(void) 
    { 
      char* strnum = malloc(MAXSIZE * sizeof(char));
      long long int num;
      
      do { 
      printf("Enter a number: ");
      if(fgets(strnum, MAXSIZE, stdin))
       num = strtoll(strnum, NULL, 10); 
      } while(!(isanumber(strnum))); 
       
      printf("You entered: %lld\n", num);
      
      free(strnum);
      return 0;    
    }

  9. #9
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Since char may be either signed or unsigned, you should always cast to unsigned char when passing one to the is* and to* functions, like so:

    Code:
    isspace((unsigned char) s[i])
    thames likes this.

  10. #10
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    like this ?

    edit:

    what are the problems a signed char can cause?

    Code:
     
    
    #include <stdio.h> 
    #include <stdlib.h>
    #include <ctype.h> 
    
    #ifndef MAXSIZE 
       #define MAXSIZE 1000
    #endif
    
    int Atoi(char s[]);
    int isanumber(char* s);
    
    int Atoi(char s[]) 
    { 
      int i, n, sign;
      for(i = 0; isspace(s[i]); i++) 
      ; 
      sign = (s[i] == '-') ? -1 : 1; 
      if(s[i] == '+' || s[i] == '-') 
         i++;
      for(n = 0; isdigit(s[i]); i++) 
         n = 10 * n + (s[i] - '0');
      return sign * n; 
    }
    
    /* if it reached the line feed, then all the chars where numbers. */
    
    int isanumber(char* s) { 
      
      int i = 0; 
      
      while(s[i] != '\n') 
      { 
         if(isalpha((unsigned char) s[i]) || isblank( (unsigned char) s[i]) )
           break;
         
         i++;    
      }        
      
      if(s[i] == '\n')
        return 1; 
      return 0;            
    }     
    
    int main(void) 
    { 
      char* strnum = malloc(MAXSIZE * sizeof(char));
      long long int num;
      
      do { 
      printf("Enter a number: ");
      if(fgets(strnum, MAXSIZE, stdin))
       num = strtoll(strnum, NULL, 10); 
      } while(!(isanumber(strnum))); 
       
      printf("You entered: %lld\n", num);
      
      free(strnum);
      return 0;    
    }
    Last edited by thames; 11-12-2012 at 07:22 PM.

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Do you have to write your own number checking function?

    Here are two examples your function accepts as a number:
    Code:
    Enter a number: 42-56
    You entered: 42
    Enter a number: #42
    You entered: 0
    Bye, Andreas

  12. #12
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Do you have to write your own number checking function?
    I don't have to, but I was curious about it. Thanks Andreas.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mkstemp inside a loop
    By jsh in forum C Programming
    Replies: 2
    Last Post: 12-27-2011, 01:45 PM
  2. ok to return inside a for loop?
    By sneakyimp in forum C Programming
    Replies: 3
    Last Post: 11-07-2011, 04:22 PM
  3. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 09:38 PM
  4. change var inside loop and run loop again
    By d387420489 in forum C Programming
    Replies: 5
    Last Post: 07-29-2011, 02:19 AM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 02:46 AM

Tags for this Thread


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