Thread: C programming questions (newbie)

  1. #1
    Registered User vitaliypisnya's Avatar
    Join Date
    Dec 2014
    Location
    Kyiv, Ukraine
    Posts
    3

    C programming questions (newbie)

    I'm reading Kernighan & Ritchie C programming book now and have a few questions about 'getchar' and 'putchar'.
    1. The question is below the code example:
    Code:
    #include <stdio.h>
    int main(void) {
        int c;
        c = getchar();
        while(c != EOF) {
            putchar(c);
            c = getchar();
        }
        return 0;
    }
    When I run this code in the terminal and enter a symbol (for example 'i') and than press Enter it prints entered symbol:
    i // entered symbol
    i // printed symbol
    After that it invites me to enter another symbol. If I enter another symbol the procedure repeats. If I press Ctrl-D (EOF) instead of entering another symbol, the program terminates. The first question is why when I run this code and after entering a symbol ('i') I don't press Enter but I press Ctrl-D, it prints entered symbol and prints it in that way:
    ii // the first symbol is entered, the second is printed after Ctrl-D
    So to terminate the program I need to press Ctrl-D again.
    2. Why in the code above I can enter a word and it prints a word and in code below it prints only one symbol even if I entered a word? The code:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int c;
    
        printf("Enter a character:\n");
        c = getchar();
        printf("Entered character is:\n");
        putchar(c);
        printf("\n");
    
        return 0;
    }
    Will be grateful for any help.
    Last edited by vitaliypisnya; 12-01-2014 at 03:56 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by vitaliypisnya
    The first question is why when I run this code and after entering a symbol ('i') and I don't press Enter but I press Ctrl-D, it prints entered symbol and prints it in that way:
    ii // the first symbol is entered, the second is printed after Ctrl-D
    So to terminate the program I need to press Ctrl-D again.
    You have a loop, so you keep reading and printing characters until getchar returns EOF.

    Quote Originally Posted by vitaliypisnya
    Why in the code above I can enter a word and it prints a word and in code below it prints only one symbol even if I entered a word?
    You don't have a loop, so you only read and print one character even if the user enters a dozen characters.
    Last edited by laserlight; 12-01-2014 at 10:00 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User vitaliypisnya's Avatar
    Join Date
    Dec 2014
    Location
    Kyiv, Ukraine
    Posts
    3
    laserlight, can you explain why when I run this code:
    Code:
    while((c = getchar()), c != EOF) {
            if(c >= 0)
                putchar(c + 1);
    I enter for example "hello". And after pressing 'Enter' button it prints what I expect, but the invitation to new entry starts with spaces equal to symbols was printed before:

    hello
    ifmmp
    // the new input starts here.

    Why is that? And that happens if the value in the line 'if(c >= 0)' is from 0 to 10. If it's more than 10 the result is different:

    hello
    ifmmp // the new input starts at the end of this line.

    Will be very grateful for the answer.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You are probably observing the effect of printing the newline character + 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    You add 1 to the value of that character.
    By pressing 'return' (or 'enter') you send an 'line feed' (value 10).
    Than you add 1 and the result is 11 and put it back to the console.
    The corresponding character is 'vertical tab' (value 11).

  6. #6
    Registered User vitaliypisnya's Avatar
    Join Date
    Dec 2014
    Location
    Kyiv, Ukraine
    Posts
    3
    Tnanks, WoodSTokk! Now I get it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 08-23-2012, 08:16 AM
  2. Newbie C programming questions
    By moussa in forum C Programming
    Replies: 4
    Last Post: 04-10-2012, 02:17 AM
  3. Replies: 10
    Last Post: 11-09-2011, 03:48 PM
  4. newbie questions
    By jp37 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2003, 09:31 PM
  5. Newbie IO questions
    By gustavosserra in forum C Programming
    Replies: 5
    Last Post: 08-29-2003, 08:09 AM

Tags for this Thread