Find the answer to your Linux question:
Results 1 to 4 of 4
Suppose I compile the following simple program to count the number of chars in an input: #include <stdio.h> main() { long nc; nc = 0; for (nc = 0; getchar() ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    4

    bash/C issue

    Suppose I compile the following simple program to count the number of chars in an input:

    #include <stdio.h>

    main()
    {
    long nc;

    nc = 0;
    for (nc = 0; getchar() != EOF; ++nc)
    ;
    printf("%1d\n",nc);
    }

    Now, when I run this program from bash (with ./a.out), I can input, but for some reason the output never comes. The output does come, however, when I put the printf statement inside the loop. Does anyone know how to resolve this issue?

    Thanks!

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Try ending your program with Ctrl-D, which is EOF

  3. #3
    Just Joined!
    Join Date
    Feb 2008
    Posts
    4
    thanks, it works

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    That's to say when your program is running terminate it by typing Ctrl-d, this will put EOF in the input stream so when getchar retrieves it getchar() == EOF and your program will end

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...