Solved K and R page 22 question about array program.

It says that the output of this program on itself is ........

Is there a way to run the .c textfile through the executable of the program on the command line without useing the int argc, char **argv?
 
Yes, they are reading stdin with getchar(3); executed something like ./a.out < a.out

Not the binary, the source code should be passed in -- let's call it example.p22.c:
Code:
[noparse]#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);
}[/noparse]
clang -Wno-implicit-int example.p22.c example.p22
example.p22 < example.p22.c
Code:
digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345
 
In general, there are three basic ways for a program to get input: files (this includes stdin), arguments (argv/argc), and environment variables (getenv(3)). How you use those are up to you, but environment variables tend to be for things that are specific to the machine or, ahem, environment, that don’t change from run to run. Arguments tend to be about what to do or how to do it, and often include file names, and the files themselves (stdin and out/err included) provide the data to operate on and where to put the result.

There are of course more (sockets, shared memory) but as you are starting out, those are the big three.
 
Back
Top