4

I'm trying to do exercise 1-10 from the C Programming Language. The idea is to create a program where the output equals the input, however, if a tab is printed, it should print \t instead of the actual tab. It also suggests doing the same with backspace/backslash, but I am trying to get it to work with just a tab before moving forwards.

I determined the value of a tab to be 9, so I came up with this code. I am confused as to why this doesn't work - it seems like a straightforward method of solving the problem. If the character getchar receives has a value equal to 9, which a tab would, then output \t in plain text. I would love to be smacked on the head for whatever has lead me barking up the wrong tree with the following code. I have seen some people post solutions here, yet I am still confused as to what minor detail is causing this to fail.

   #include <stdio.h>

    main(){

    int c;
    while ((c = getchar()) != EOF) {
        if ((c == '\t') == 9) putchar("\t");
        else purchar(c);
    }
    }

That brings the following compilation error

tenth.c: In function 'main':
tenth.c:7:35: warning: passing argument 1 of 'putchar' makes integer from pointe
r without a cast
     if ((c == '\t') == 9) putchar("\t");
                                   ^
In file included from tenth.c:1:0:
c:\mingw\include\stdio.h:645:43: note: expected 'int' but argument is of type 'c
har *'
 __CRT_INLINE __cdecl __MINGW_NOTHROW  int putchar(int __c)
                                           ^
C:\Users\*\AppData\Local\Temp\ccC4FPSb.o:tenth.c:(.text+0x18): undefined ref
erence to `purchar'
collect2.exe: error: ld returned 1 exit status

I've also tried

main(){

int c;
while ((c = getchar()) != EOF) {
    if (c == '\t') putchar("\t");
    else purchar(c);
}
}
Community
  • 1
  • 1
cfinspan
  • 47
  • 5
  • 2
    Note that you should be coding to at least the old (C99) standard, and preferably the new (C11) standard. Neither allows `main()` (though C89/C90 did, out of necessity); both the newer standards require the return type to be specified explicitly — `int main(void)` is recommended when you are not going to use the command-line arguments. If your compiler doesn't warn you about this, add warning flags so it does — or get a better compiler. – Jonathan Leffler Aug 14 '16 at 01:44
  • THank you! I will change my code to start with proper formatting. – cfinspan Aug 14 '16 at 16:13
  • There are two valid signatures for the `main()` function Both signatures have return type of `int` in the current scenario suggest using: `int main( void )` – user3629249 Aug 15 '16 at 06:49
  • this expression: `((c == '\t') == 9)` says if 'c' is equal to at tab (which will set a true/false condition) then it says does that true/false condition equal 9. Suggest: (c == '\t') – user3629249 Aug 15 '16 at 06:52
  • the posted code would be massively easier to read/understand if it were consistently indented. Indent after every opening brace '{'. un-indent before every closing brace '}'. – user3629249 Aug 15 '16 at 06:55

3 Answers3

11

There's a difference between ' and " in C:

  • "\t" creates a C-style string of type char[2] containing the characters \t (tab) and \0 (the NUL-terminating character).
  • '\t' is a single character of type int.

putchar takes an int parameter and prints out a single character. You should use (assuming your goal is to print the message \t to the user and not a tab character):

putchar('\\');  // Print the backslash (it must be escaped)
putchar('t');   // Print the t

Note that the \ character is special and needs to be escaped with an extra \ (so '\\' is a single \ character of type int).

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
3

Note that you have also incorrectly spelled 'putchar' as 'purchar' in your else statement. You can see that the compiler is complaining about it:

C:\Users\*\AppData\Local\Temp\ccC4FPSb.o:tenth.c:(.text+0x18): undefined ref
erence to `purchar'
theQuestionMan
  • 1,270
  • 2
  • 18
  • 29
1

Putchar only accepts a single character.You have entered "\t" which is basically two characters,because double quotes means it's a string,and even a string of 1 character doesnt count as a char.So try doing putchar('\t') with single quotes (which mean a single char)

rexxar2
  • 97
  • 9
  • 2
    The problem isn't that it's two characters, because then `""` would be fine. The problem is that OP tries to pass a `char*` to a function that takes an `int`. –  Aug 14 '16 at 00:31