Why are these programs not working?

  • Thread starter Thread starter dE_logics
  • Start date Start date
  • Tags Tags
    Programs
Click For Summary
SUMMARY

The discussion centers around issues with two C programs designed to count lines and categorize input characters, which are not functioning as expected when compiled with GCC. Users report that after entering input, the programs do not produce output and instead allow continuous input without termination. The solution involves using the end-of-stream character (Ctrl-D) to signal the end of input, which is necessary for the programs to execute correctly and display results.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with GCC (GNU Compiler Collection) for compiling C programs
  • Knowledge of standard input/output functions in C
  • Awareness of end-of-file (EOF) concepts in console applications
NEXT STEPS
  • Learn about handling user input in C, including EOF and getchar() function
  • Explore debugging techniques for C programs using GDB (GNU Debugger)
  • Study how to implement error handling in C to manage unexpected input
  • Investigate alternative input methods in C, such as using fgets() for reading strings
USEFUL FOR

C programmers, students learning C language fundamentals, and developers troubleshooting input/output issues in console applications.

dE_logics
Messages
742
Reaction score
0
I was wondering why are these programs, as given in an e-book not working --

Code:
 #include <stdio.h>

   /* count lines in input */
   main()
   {
       int c, nl;

       nl = 0;
       while ((c = getchar()) != EOF)
           if (c == '\n')
               ++nl;
       printf("%d\n", nl);
   }
Code:
 #include <stdio.h>

   /* count digits, white space, others */
   main()
   {
       int c, i, nwhite, nother;
       int ndigit[10];

       nwhite = nother = 0;
       for (i = 0; i < 10; ++i)
           ndigit[i] = 0;

       while ((c = getchar()) != EOF)
           if (c >= '0' && c <= '9')
               ++ndigit[c-'0'];
           else if (c == ' ' || c == '\n' || c == '\t')
               ++nwhite;
           else
               ++nother;

       printf("digits =");
       for (i = 0; i < 10; ++i)
           printf(" %d", ndigit[i]);
       printf(", white space = %d, other = %d\n",
           nwhite, nother);
   }

The programs are getting compiled, but on running the binary, I just get an empty line on which I can type...

Expected is that after entering a new line, the program should proceed, but nothing like that is happening...after entering a new line...I just enter a new line and can type in that too. :confused:

This forms a never ending loop.

I'm using gcc to compile the program.
 
Technology news on Phys.org
Enter the end-of-stream character to terminate the input. Probably ctrl-D.
 
Thanks.
 

Similar threads

Replies
14
Views
4K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K