Domanda

I'm completely new to C, and I am attempting to run this example code on xcode, but it says build failed. I got the code exact like it is in the book, but it still won't run.

#include <stdio.h>
/* count characters in input; 2nd version */
int main()
{
    double nc;
    for (nc =0; getchar() != EOF; ++nc) {
        ;
    }
    printf("%.0f\n", nc);
}

Sorry if it's a noob question. All help is appreciated!

È stato utile?

Soluzione

The question code lacks a 'return {int value}' statement. I added this missing line, and ended up with the following code:

#include <stdio.h>
/* count characters in input; 2nd version */
int main()
{
   double nc;
   for (nc =0; getchar() != EOF; ++nc) {
       ;
      }
   printf("%.0f\n", nc);

   return(0);
}

The above compiled without errors or warnings using:

gcc -Wall -o test test.c

To run it requires that input to the program comes from a file. I made a file named 'junk.txt' with the following content (not including the braces):

[ adddsadsd 33334343434243] 

Then I executed the program using the following command:

./test < junk.txt

which generated the output:

25
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top