Help with basic concept in C - loops

This is a discussion on Help with basic concept in C - loops within the C Programming forums, part of the General Programming Boards category; Hi guys, No need to say this but I'm new to C. So far I've been working with high level ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    Help with basic concept in C - loops

    Hi guys,
    No need to say this but I'm new to C. So far I've been working with high level languages and from the start I can see that C is in a league of it's own. I have no problems understanding code or following it. I am learning C with "C the programming language" as you could probably imagine.So here is my question:
    When you make a simple loop with getchar() and putchar(), such as
    Code:
    #include <stdio.h>
    
    int main(void){
    int c;
    
    c=getchar();
    while ((c = getchar()) != EOF){
    putchar(c);
    c=getchar();
    }
    }
    And when you compile and run the program I have those scenarios:
    i hit "g" and then enter i will obviously get "g". When I simply hit enter I get an extra new line. Fine. The question is what happens when I introduce a word or a string such as "ggg" and hit enter. I obviously get ggg back. But the question is-does the program enter the loop 3 times for each of the 3 g's or does it make a single loop with the 3 g's in it. I fear I'm somewhat missing the concept here.

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    Oops nevermind. I figured I could answer that question myself. Anyway in case someone is wondering the same:
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
            int c, b;
            b = 0;
            c = getchar();
            while (c != EOF) {
                    putchar(c);
                    c = getchar();
                    ++b;
                    printf("\n%d\n", b);
            }
    }
    That pretty much answers the question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic problem using for loops w/ functions. help :(
    By rensan in forum C Programming
    Replies: 8
    Last Post: 04-22-2012, 11:05 AM
  2. Replies: 3
    Last Post: 06-01-2011, 05:19 PM
  3. linked list - basic concept.
    By Vber in forum C Programming
    Replies: 4
    Last Post: 04-11-2003, 03:30 PM
  4. basic doubt in pointer concept
    By sanju in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 12:35 AM
  5. Basic Concept
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21