Thread: C octal/hexa constants

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    C octal/hexa constants

    So i was looking at octal/hexa constants in C, for example

    char a='\100';
    unsigned char b='\xff';

    The standard says their max value (of the octal/hexa escape sequences) is equal to a max value of a unsigned character, so that means 255.

    What im writing is a syntactic checker (debuger) which will report an error/warning if the constant is not written correctly... and im stuck on this part.

    Code:
    while((c=getchar())!=EOF)
    {
    
    ...
    
    else if(state==squote_escape) /* after it reads a '\ */
    { 
          
           ....
    }
    
    }

    Basicaly, i dont know how to add the octal constant check.

    If anyone can come up with a pseudocode it would be nice.

    Code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_SIZE 1024
    
    /* push, pop, check function */
    
    char stack[MAX_SIZE];
    int stack_counter=0;
    
    int push(char c) /* pushes character to top of the stack */
    {
        if(stack_counter==1024) return 0;
        
        stack[stack_counter++]=c;
        return 1;
    }
    
    int pop() /* decrements stack_counter */
    {
        if(stack_counter<0) return 0;
        
        stack_counter--;
        return 1;
    }
    
    int check (char c)
    {
        char tmp;
        
        switch(c)
        {
           case '(':
           case '[':
           case '{':
                if((tmp=push(c)==0)) return 0;
                break;
           
           case ')':
                tmp=pop();
                if(tmp==0) return 0;
                if(stack[stack_counter]!='(') return 0;
                break;
                
           case ']':
                tmp=pop();
                if(tmp==0) return 0;
                if(stack[stack_counter]!='[') return 0;
                break;
                
           case '}':
                tmp=pop();
                if(tmp==0) return 0;
                if(stack[stack_counter]!='{') return 0;
                break;
                
           }
           return 1;
    }
                
    
    int main(int argc, char *argv[])
    {
          
          int c;
          enum states { normal,
                        comment_entry, comment, comment_exit,
                        squote, squote2, squote3, squote_escape,
                        dquote, dquote_escape };
                        
          int line=1;
          int state=normal;
          int x=0;
          int valid=1;
          char tmp=0;
          
        
          while((c=getchar())!=EOF)
          {
              if(state==normal && c=='/') { state = comment_entry; }
              else if(state==normal && c=='"') { state = dquote; }
              else if(state==normal && c=='\'') { state = squote; }
              
              else if(state==normal) { if((check(c))==0) {printf("Line %d: Mismatching %c\n", line, c); valid=0;} } 
              
              else if(state==comment_entry && c=='*') { state = comment; }
              else if(state==comment_entry && c=='/') { }
              else if(state==comment_entry) { if(c=='"') { state = dquote; } else if(c=='\'') { state = squote; } else {state = normal; if((check(c))==0) {printf("Line %d: Mismatching %c\n", line, c); valid=0; } }  }  /* neccesary to check braces here aswell */
              
              else if(state==comment && c=='*') { state = comment_exit; }
              else if(state==comment) {}
              
              else if(state==comment_exit && c=='*') {}
              else if(state==comment_exit && c=='/') { state = normal; }
              else if(state==comment_exit) { state = comment; }
              
              else if(state==dquote && c=='"') { state = normal; }
              else if(state==dquote && c=='\n') { state = normal; printf("Line %d: Double quotes not properly closed.\n", line); valid=0; }
              else if(state==dquote && c=='\\') { state = dquote_escape; }
              else if(state==dquote) { }
              
              else if(state==dquote_escape) { state = dquote; }
              
              else if(state==squote && (c=='\'' || c=='\n')) { state = normal; printf("Line %d: Constant not properly closed.\n", line); valid=0; }
              else if(state==squote && c=='\\') { state = squote_escape; }
              else if(state==squote) { state = squote2; }
              
              else if(state==squote_escape && (c=='\'' || c=='"' || c=='?' || c=='\\' || c=='a' || c=='b' || c=='f' || c=='n' || c=='r' || c=='t' || c=='v')) { state = squote2; }
              else if(state==squote_escape) { printf("[Warning]: Line %d: Unknown escape sequence.\n", line); state = squote2; }
              
              else if(state==squote2 && c=='\'') { state = normal; }
              else if(state==squote2) { state = squote3; valid=0; printf("Line %d: Constant not properly closed.\n", line);  }
              else if(state==squote3 && c=='\\') { tmp=1; }
              else if(state==squote3 && tmp==1) { tmp = 0; }
              else if(state==squote3 && (c=='\'' || c=='\n')) { state = normal; }
              
              if(c=='\n') line++;
          }
      
                /* promijenjeno squote stanje */
         
              for(x=0; x<stack_counter; x++)
              printf("Mismatching %c\n", stack[x]);
              
              switch(state)
              {  
                 case comment:
                 case comment_exit:
                      printf("Code ends inside a comment.\n"); valid=0; break;
                 
                 case squote:
                 case squote2:
                 case squote3:
                 case squote_escape:
                      printf("Code ends inside '.\n"); valid=0; break;
                      
              }
              
              if(valid==1)
              printf("No syntactic errors.\n");
              
          
          
      printf("Press any key to continue");
      getchar();	
      return 0;
    }
    Last edited by Tool; 11-19-2009 at 12:38 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    int val = 0;
    int ch;
    for( ch = NextChar(); val <= 255 && ch >= '0' && ch <= '7'; ch = NextChar() )
        val = 8 * val + ch - '0';
    Unget( ch );
    if( val > 255 )
        Error( "Octal character out of range" );
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Thanks, that was really helpful!

    Code done & working, ill do the same for hexa.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    One question though; how'd u come up with val formula, val = 8 * val + ch - '0';
    and how would it look for calculating hexa?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Well, just as you would multiply a base-10 number by 10 to shift it over, you are multiplying by 8 for base 8 numbers. The (ch - '0') part - I would have it in parenthesis to make it clearer, is the conversion from ASCII character to number.

    For the HEX, you'd multiply by 16... but the conversion 0-9, A-F would need a different approach since those groups of characters aren't contiguous in the ASCII chart.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with variables and constants.
    By dave1339 in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 09:18 PM
  2. Replies: 7
    Last Post: 11-12-2008, 11:00 AM
  3. COnstants
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 09-01-2007, 04:59 AM
  4. where are the constants?
    By Raven Arkadon in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2005, 02:07 PM
  5. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM