Thread: printing a tab

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    36

    printing a tab

    Code:
    #include <stdio.h>
    
    int main()
    {
        char c = '  '; /* I typed tab here */
        printf("a%ca\n", c);
        return 0;
    }
    I m using devc++.
    The output of the program is : "a a". There is one space between the alphas. Why isn't there a tab?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <stdio.h>
    
    int main()
    {
        char c = '\t';
        printf("a%ca\n", c);
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    you're assigning a character constant to the variable that you're printing. As ' ' represents a single whitespace, regardless of just how much white space you throw in there, and so it will only print a single whitespace.

    You can either use a string to represent the tab, or the escape sequence that represents the tab.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *c = "		"; 
        printf("a%sa\n", c);
        return 0;
    }
    or
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char c = '\t';
        printf("a%ca\n", c);
        return 0;
    }
    Also, just so you know, you really should use void if there are no arguments to main. Without the void, it translates as "there is an unknown amount of arguments".

    FAQ Entry

    Edit: Beaten

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    As ' ' represents a single whitespace, regardless of just how much white space you throw in there, and so it will only print a single whitespace.
    That's true, but not wholly the case. A tab is a single character, so you just need to make sure that you're using a text editor that actually inserts a tab when you press the TAB key instead of inserting a number of spaces.
    Code:
    itsme@dreams:~/C$ cat tab.c
    #include <stdio.h>
    
    int main(void)
    {
      char c = '    ';
    
      printf("a%ca\n", c);
      return 0;
    }
    itsme@dreams:~/C$ gcc -Wall -ansi -pedantic tab.c -o tab
    itsme@dreams:~/C$ ./tab
    a       a
    The error is with the OP's text editor used to create the source file.
    Last edited by itsme86; 03-11-2005 at 09:04 AM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    thanks for the replys. i tried the same code (the one i posted) in cygwin (gcc) and the output was :
    "a a" (a [tab] a). can anyone explain why this difference?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by modec
    thanks for the replys. i tried the same code (the one i posted) in cygwin (gcc) and the output was :
    "a a" (a [tab] a). can anyone explain why this difference?
    I posted just before you, but I believe it contains your answer.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    i didn't catch your post itsme86. It is probably the devc++ editor's fault. is the newline considered a single character?
    Code:
    c = '
    ';

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    What you got was 2 white spaces giving you the effect of a tab. As has been mentioned before, to get a tab you simply use the escape character \ followed by t that is the tab character.
    Heres some more examples.

    Code:
    \b backspace character, moves cursor to the left, one character
    \f form feed character, goes to the top of a new page
    \r return character, returns to the beginning of the current line
    \t tab character, advances to the next tab stop
    \n  the new line character.
    When no one helps you out. Call google();

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    Quote Originally Posted by itsme86
    That's true, but not wholly the case.
    My mistake, thanks for the info

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    ok, i know about the escape sequences \n \t \r etc. But i m asking: considering that newline is a single character is the following acceptable?
    Code:
    char c = '
    ';
    the reason i m asking is because i m writing a lexical analyser and i want to know whether the analyser should accept it or not.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why don't you just try it?
    Code:
    itsme@dreams:~/C$ cat newline.c
    #include <stdio.h>
    
    int main(void)
    {
      char c = '
    ';
    
      printf("a%ca\n", c);
      return 0;
    }
    itsme@dreams:~/C$ gcc -Wall newline.c -o newline
    newline.c:5: unterminated character constant
    newline.c:6: unterminated character constant
    itsme@dreams:~/C$
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    i m using flex to write the lexical analyser and i have the following prob:
    this code
    Code:
    //other stuff for flex
    
    int main ()
    {
    	int token;
    	
    	do { 
    		token = yylex();
    		printf( "token = %d, lexeme = \"%s\"\n", token, yytext );
    	} while ( token != T_eof );
    	
    	return 0;
    }
    when i input a tab character
    Code:
    '         '
    gives
    Code:
    token = 39, lexeme = "' '"
    this code
    Code:
    //exactly the same other stuff for flex
    
    int main ()
    {
    	int token;
    	
    	do { 
    		token = yylex();
    		putchar('a');
    		printf( "token = %d, lexeme = \"%s\"\n", token, yytext );
    	} while ( token != T_eof );
    	
    	return 0;
    }
    when i input a tab character gives
    Code:
    atoken = 39, lexeme = "'        '"
    the single thing i change is the putchar('a')
    does anyone have an idea why that happens?

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    i used gdb to debug.

    Code:
    Hardware access (read/write) watchpoint 3: yytext
    
    Value = 0x93c70 "'\t'"
    0x00003c38 in main () at lexin.c:107
    107                        printf( "token = %d, lexeme = \"%s\"\n", token, yytex
    t );
    (gdb) print yytext
    $2 = 0x93c70 "'\t'"
    (gdb) c
    Continuing.
    token = 39, lexeme = "' '"
    how is that possible? the value of yytext is "'\t'" but printf prints "' '".

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What do you mean how is it possible? The tab character is represented by an escaped t. Printf translates it into an actual tab. What's the problem?

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    ok i got it. I was thinking of a tab as many spaces. But printf doesn't print many spaces, it moves to the next tab stop. So i was thinking that printf makes a mistake when it prints a single space,but it was simply because the next tab stop was a single space away. thanx for the replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Tab Ordering of GUI Controls
    By cloudy in forum Windows Programming
    Replies: 2
    Last Post: 04-22-2006, 09:13 AM
  3. Tab order in Tab Control
    By Halloko in forum Windows Programming
    Replies: 2
    Last Post: 05-08-2005, 11:08 PM
  4. Visual C++
    By Golffor1 in forum C++ Programming
    Replies: 1
    Last Post: 08-04-2003, 04:30 PM
  5. Disable a tab in control tab?
    By Iron Mike in forum Windows Programming
    Replies: 1
    Last Post: 07-23-2003, 10:50 AM