how to check for the “backspace” character in C

后端 未结 5 520
天涯浪人
天涯浪人 2020-11-30 08:01

I\'d like to know how to check if a user types the \"backspace\" character.

I\'m using the getch() function i.e. \"key = getch()\" in my C program and i

5条回答
  •  星月不相逢
    2020-11-30 08:13

    Try this:

    #include       /* printf   */
    #include       /* isalpha isdigit isspace etc      */
    
    #define FALSE 0
    #define TRUE  1
    
    /* function declarations */
    int char_type(char);
    
    main()
    {
     char ch;
    
     ch = 127;
     char_type(ch);
    
     ch = '\b';
     char_type(ch);
    
     return 0;
    }
    
    int char_type(char ch)
    {
     if ( iscntrl(ch) != FALSE)
       printf("%c is a control character\n", ch); 
    }
    

    This is a complete program but it only tests for control characters. You could use principles of it, your choice. Just learning too!

    See : http://www.tutorialspoint.com/c_standard_library/ctype_h.htm or lookup the functions for the ctype.h header file of the C Standard Library.

    It's good that you're getting input. Thanks all for the info. I was just looking up backspace code and found this question.

    BTW try '\0' before any char. Not sure what that does but it stops all code after it. Is that like the return 0; line?

提交回复
热议问题