ASCII control characters

This is a discussion on ASCII control characters within the C Programming forums, part of the General Programming Boards category; How do you print the ASCII control characters?...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    ASCII control characters

    How do you print the ASCII control characters?

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    To print any ASCII characters, I think, it's the same thing.
    Code:
    printf("%c",ASCII_character);
    By the way I dont know what's a control character.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Here is the code that prints all the ASCII characters
    Code:
    #include<stdio.h>
    int main(void)
    {
    	int i;
    	for(i=0;i<=255;i++)
    		printf("\n%c",i);
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    1
    what is the a control character?

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Control characters are characters that represent some formatting action, rather than a printable letter. So instead of 'a', 'A', etc... Formatting characters are numbers that are interpreted as "new-line", "carriage-return", "backspace", etc... I guess tabs could also be considered control characters in a sense.

    ASCII provides a rather limited set of control characters. Libraries like ncurses provide access to more sequences that are recognized by terminals and terminal emulators, allowing you to even construct windowed apps from the command-line.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    If fgetc comes across one of these control characters, is it possible to print them? with printf?

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It probably depends on your shell / terminal emulator. I know it was back in the days of real DOS, because I did an exercise with that in Turbo C. However some of that functionality might have been disabled in newer versions of Windows. So to answer your question, I know it did in DOS, but now I'm not so sure. Remember also that not all the control characters are for visual formatting. ^C for instance signals the end of the text, but that doesn't necessarily have any meaning for the screen, and so you'll actually see "^C" get printed to the screen should you press that combination.

    edit: http://www.csgnetwork.com/asciiset.html is a quick reference if you need one

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    Why doesn't this print ^I where i have a tab?


    Code:
    if(iscntrl(c)){
      c+64;
      printf("^%c", c);
    }

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by erasm View Post
    Why doesn't this print ^I where i have a tab?
    That's not how it works.

    Contra BEN10, there are 128 ASCII characters, not 255. "ctrl" is not one of them.

    Table of ASCII Characters

    If you want "see" what the actual character is, you can get this number:

    print("%d", c);
    for values 0-127, or

    print("%x", c);
    for hex values 00-7f

    The hex values are handy if you want to dump a bunch of text, because they are all two digits:
    Code:
    void hextext (char *text) {
    	int i, len = strlen(text);
    	for (i=0; i<len; i++) {
    		printf("%x ", text[i]);
    		if (!(i%25)) printf("\n");
    	}
    }
    Last edited by MK27; 08-10-2009 at 11:13 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by erasm View Post
    Why doesn't this print ^I where i have a tab?


    Code:
    if(iscntrl(c)){
      c+64;
      printf("^%c", c);
    }
    The two characters "^I" are not the same as the single character '^I'. The '^' has no special meaning -- that's just how most programs render a control character. Printing a caret followed by a character does not print a control character.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    Hmmm i think i need some clarification of the unix cat command.

    Using the unix command: cat -v file.txt gives no evidence of any control characters. Shouldn't there be newline control characters at the end of each line?

    file.txt:
    here is a line.
    more line.



    after some blanks.
    the end.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    The cat manual page states that "-v" shows all control characters EXCEPT newline and tab
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    BEN10 & coolskyboy: take a look at this ASCII chart of all characters:
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    The first 32 are usually referred to as 'control characters'. They may cause unpredictable activities when thrown out to a display or printer. Such as advance the paper, pause data flow, start a new line, etc. Rather than the rest which are 'printable' - that is, those that make ink flow and advance the cursor nicely.

    It sure is strange that people don't know these things instinctively. We all should have lived through the mechanical Teletype days. That would straighten everyone out!

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    Why is this code resulting in 2 I's being printed?

    here is line1.
    ^IIafter a tab.
    ^II^IItwo tabs!
    the end.

    Code:
    if(iscntrl(c) && tflag && c != '\n'){
      c += 64;
      printf("^%c", c);
    }

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Let me guess: after this, you have something like
    Code:
    if (isalpha(c)) {
        printf("%c", c);
    }
    which will print that I again (instead of putting it in an else, so that only one or the other print would happen).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Using fgetc() to read control characters
    By Bnchs in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 05:23 AM
  3. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  4. All the ASCII characters
    By Shadow12345 in forum C++ Programming
    Replies: 5
    Last Post: 05-20-2002, 01:20 PM
  5. ascii characters
    By Denethor2000 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2002, 04:26 AM

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