I m using devc++.Code:#include <stdio.h> int main() { char c = ' '; /* I typed tab here */ printf("a%ca\n", c); return 0; }
The output of the program is : "a a". There is one space between the alphas. Why isn't there a tab?
I m using devc++.Code:#include <stdio.h> int main() { char c = ' '; /* I typed tab here */ printf("a%ca\n", c); return 0; }
The output of the program is : "a a". There is one space between the alphas. Why isn't there a tab?
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
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.
orCode:#include <stdio.h> int main(void) { char *c = " "; printf("a%sa\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".Code:#include <stdio.h> int main(void) { char c = '\t'; printf("a%ca\n", c); return 0; }
FAQ Entry
Edit: Beaten
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.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.
The error is with the OP's text editor used to create the source file.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
Last edited by itsme86; 03-11-2005 at 09:04 AM.
If you understand what you're doing, you're not learning anything.
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.Originally Posted by modec
If you understand what you're doing, you're not learning anything.
i didn't catch your post itsme86. It is probably the devc++ editor's fault. is the newline considered a single character?
Code:c = ' ';
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();
My mistake, thanks for the infoOriginally Posted by itsme86
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?
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.Code:char c = ' ';
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.
i m using flex to write the lexical analyser and i have the following prob:
this code
when i input a tab characterCode://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; }givesCode:' '
this codeCode:token = 39, lexeme = "' '"
when i input a tab character givesCode://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; }
the single thing i change is the putchar('a')Code:atoken = 39, lexeme = "' '"
does anyone have an idea why that happens?
i used gdb to debug.
how is that possible? the value of yytext is "'\t'" but printf prints "' '".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 = "' '"
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.
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.