Printing non-printing characters in ^ and M- notation

This is a discussion on Printing non-printing characters in ^ and M- notation within the C Programming forums, part of the General Programming Boards category; I have a project to implement the UNIX cat program with 3 flags, -v, -E and one of our choosing. ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    Printing non-printing characters in ^ and M- notation

    I have a project to implement the UNIX cat program with 3 flags, -v, -E and one of our choosing. I have everything working except for the -v flag.

    The -v flag is:
    -v, --show-nonprinting
    use ^ and M- notation, except for LFD and TAB
    I have been looking through documentation and cannot find anything about C having a print command using ^ and M- notation. Is there one?

    I've attached a file with output for all 255 values of a char with the -v flag and without it. I can see a pattern there that I could implement, it just seems like this should already be built into the language somewhere. Thank you in advance for the help.
    Attached Files Attached Files
    Last edited by sbeard22; 09-29-2008 at 08:04 PM. Reason: didn't mean to submit yet

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No. But you're allowed to write one.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    2
    Yeah, it wasn't too bad. The tricky part was dealing with the negative business.

    Code:
    void  print_nonprt(char c, FILE *ofp) {
    
       if(c < -96) {
         fprintf(ofp, "M-^%c",c+192);
       } else if(c < 0) {
         fprintf(ofp, "M-%c",c+128);
       } else if(c == 9 || c == 10) {
          putc(c, ofp);
       } else if(c < 32 ) {
          fprintf(ofp, "^%c",c+64);
       } else if(c < 127) {
          putc(c, ofp);
       } else {
         fprintf(ofp, "^?");
       }
    
    }

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    You might also consider not having to deal with negative queries if you adjust the input at the top (inside print_nonprt):
    Code:
    if (c & 128) {
        fprintf(ofp, "M-");
        c &= ~128; }
    ...
    followed by the rest
    That way you are sure to cover your special cases for c==9, c==10, and c==127 and their negative equivalents without needing to worry about whether you've made any math errors. But I'm not sure how you would handle TAB and NL when they have their bit 7 set.
    Last edited by nonoob; 09-30-2008 at 06:49 AM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    can someone clarify what the business is with the negative values?

    I thought the c can go up to 255. why does c go negative and is there any documentation anywhere that clarifies this?

    thanks

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    C considers char to be signed unless specified otherwise. That is, the bytes are interpreted to be in the numerical range -128 to +127. The most significant bit is the sign bit just as for any other integers. So when we're talking about characters in the upper end of the ASCII chart, those above +127, they appear to be negative...

    Unless you declare it as unsigned char c in the first place. Then your range checks would be written to compare to numbers in the 0 to 255 range.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > C considers char to be signed unless specified otherwise.
    Quote Originally Posted by draft c99
    The three types char, signed char, and unsigned char are collectively called
    the character types. The implementation shall define char to have the same range,
    representation, and behavior as either signed char or unsigned char.32)
    A char by itself can be either.
    So if you're relying on something specific, then you need to be specific.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

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