Character Input and Output help

Discussion in 'Mac Programming' started by Vaxuun, Feb 6, 2010.

  1. Vaxuun macrumors member

    Joined:
    Feb 6, 2010
    Location:
    Denmark
    #1
    Hey MacRumors! :)

    I am still learning the program language C with the help of: The C Programming Language, 2nd Edition by Brian and Kernighan.

    I like the book very much and I think it is great you have to do some small exercises, to be sure you understand what you just wrote and what does what.

    But in one of the chapters, with the headline "1.5 Character Input and Output "1.5.1 File Copying" ", they are talking about getchar() and putchar(). I am still not sure, still after the chapter what it is about, so if someone could explain me or give a link, that would be awesome :)

    Code:
    #include <stdio.h>
    
    /* copy input to output; 2nd version */
    main() {
    
    int c;
    while ((c = getchar()) != EOF) 
    putchar(c);
    
    }
    
    that's the code we want to compile. The exercises are
    Code:
    Exercsise 1-6. Verify that the expression getchar() != EOF is 0 or 1.
    
    Exercise 1-7. Write a program to print the value of EOF.
    I use Xcode to write and compile (that's the best I know) as I can't get
    Code:
    gcc filename.c -o filename
    to work with Terminal...

    But when I run and compile with xcode, my logs only says
    Code:
    Debugger Console
    
    
    [Session started at 2010-02-06 22:52:42 +0100.]
    GNU gdb 6.3.50-20050815 (Apple version gdb-1346) (Fri Sep 18 20:40:51 UTC 2009)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "x86_64-apple-darwin".
    tty /dev/ttys000
    Loading program into debugger…
    Program loaded.
    run
    [Switching to process 70706]
    Running…
    
    as you can see, the exercise is about to get the value of c, either 0 or 1. But it doesn't print out? :(

    How do you get Xcode to work? How do I know if it 0 or 1? How can I write a program to tell me to print out (1-7)?

    I tried to do:
    Code:
    #include <stdio.h>
    
    /* copy input to output; 2nd version */
     main() {
    
    int c;
    while ((c = getchar()) != EOF)
     putchar(c);
    printf("c = %d", c);
    }
    
    but that didn't work either..

    Help please! :D

    EDIT:
    I also have another problem..

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int c, nl;
    	nl = 0; while ((c = getchar()) != EOF)
    		if (c == '\n') ++nl;
    	printf("%d\n", nl);
    	
    	return 0;
    }
    
    doesn't print anything in Consol?

    Code:
    Exercise 1-8. Write a program to count blanks, tabs, and newlines. 
    Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or
    more blanks by a single blank.
    Exercise 1-10. Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way.
    Is it me doing something wrong or is my Xcode ****ing up? :/
     
  2. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #2
    You're over-complicating the exercise. All you need to do is ask for a character and determine whether or not the statement getchar() != EOF is true or false. You don't need any while statements.
     
  3. Vaxuun thread starter macrumors member

    Joined:
    Feb 6, 2010
    Location:
    Denmark
    #3
    I just copied and pasted what was in the book, so it is funny they included while if it not necessary :/

    But how do I see if it is true or not? And how do I make a program for it?
    I am totally lost in Input and Output, I have no ideas what's it about..

    Isn't there some sources I can read some more about and get a better understanding for what it is?

    But thanks for quick answer
     
  4. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #4
    Yes, I realize you copy/pasted it. This still does not answer nor gets to the point of the exercise. What you copied will print out the value of the character you entered unless it is the EOF character.

    Do you have gcc installed? You have to install it (it's included with the XCode download) for it to work.

    You really just have to think about what the exercise is asking. It simply asks you to verify that the statement getchar() != EOF is true or false, i.e. that this expression evaluates to 1 or 0. If you're just inputting any character, it's always going to evaluate to either 1 or 0, it doesn't matter which one.

    I suggest you write a small program that

    -asks for a character
    -runs the character through "getchar() != EOF"
    -outputs the result

    It's two lines with a return 0. Really, really simple. Hint: all you need are printf statements.

    This is not good. I find K&R's chapter on I/O to be fairly good...

    The internet. Just google "input output in C" and I'm sure you'll find many different explanations.
     
  5. Vaxuun thread starter macrumors member

    Joined:
    Feb 6, 2010
    Location:
    Denmark
    #5
    So it is false or 0 because it does not end? Or?

    It's not a chapter I want to skip as I think it's gonna be useful later. But right now, I am still lost in what Input and Output is, how I can use it...

    Input reads a variable or a letter and Output prints it? But what does it print, as printf("c - %d", c); doesn't work?

    Is it just me that have a hard time learning I/O or is more advanced? Even Google didn't help me out
     
  6. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #6
    Basically.

    If you honestly cannot think of how you can use input and output in anything you program, you've got much larger problems than just this simple exercise.

    ??? What's the point of this statement? You're subtracting "c" from itself...

    I/O is pretty simple conceptually; you input something and your program acts on it, then outputs the result. I think it's just you.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    I see no subtracting here.
    Code:
    printf("c - %d", c); 
     
  8. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #8
    oh....right. Oops :p
     
  9. mdeh macrumors 6502

    Joined:
    Jan 3, 2009
    #9
    I think this is an incredibly mean thing to say. Programming is difficult, and there are many *kinder* books to learn from.
    May I suggest you look at Steven Kochan. He has a book dedicated to C, as well as objective C. He has literally started thousands of people off on this difficult journey.
     
  10. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #10
    I never said programming wasn't difficult, and if the above is "incredibly mean" I'm sorry to say I'm not going to apologize. K&R is a well-written, time-tested introduction to C. If you can't understand K&R, I have serious doubts that anything else is going to help you.

    Furthermore, the OP isn't saying he's having difficulty specifically with K&R, the concept of I/O itself seems to escape them. This is not something that a different book is likely to solve.
     
  11. skochan macrumors regular

    Joined:
    Apr 1, 2006
    Location:
    California
    #11
    Actually, I owe my writing career to K&R! When I needed to learn C, it was the only text available. Sorry, but I was a programmer at the time, with a degree in CS, and I did find the book a little tough going. That's why I decided to write Programming in C, which was the second book on C programming.

    K&R is certainly a well-written, classic book on C. However, for some people, it's not the easiest of reads. To say that if you don't learn C from K&R then nothing else is going to help you is absurd. That's not said with any sort of motivation to promote my own book (which I certainly don't need to do any more having written it over 25 years ago) but is based on the response I've had from literally thousands of readers who have learned how to write C programs using my text. And sometimes they turned to my text when K&R just seemed a little too difficult to digest. My book is not better, it's just different. Different books take different approaches to teaching. And people learn differently. That's all there is to it.

    Cheers,

    Steve
     

Share This Page