--- title: "wc Word Count (C)" id: 77540 type: "computer_media" slug: "wc-word-count-c" url: "http://localhost/computer_media/wc-word-count-c/" markdown_url: "http://localhost/computer_media/wc-word-count-c.md" published_at: "2026-11-15T18:38:10+00:00" modified_at: "2026-11-22T09:55:55+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2022/04/20230809-041332.jpg" excerpt: "A C version of the Unix wc utility for the QL: counts lines, words and characters in a file." post_tag: - name: "QL" slug: "ql" taxonomy: "post_tag" url: "http://localhost/tag/ql/" model: - name: "Sinclair QL" slug: "sinclair-ql" taxonomy: "model" url: "http://localhost/model/sinclair-ql/" indiv: - name: "Tim Swenson" slug: "tim-swenson" taxonomy: "indiv" url: "http://localhost/indiv/tim-swenson/" genre: - name: "Programming" slug: "programming" taxonomy: "genre" url: "http://localhost/type/programming/" media_type: "Program" programmers: - name: "Tim Swenson" slug: "tim-swenson" taxonomy: "indiv" url: "http://localhost/indiv/tim-swenson/" mediadate: "1991" article_media: - id: 23352 title: "The C Page" type: "article" url: "http://localhost/article/the-c-page-4/" media_type_tags: "Programming" license: false --- # wc Word Count (C) Program 6 of Tim Swenson’s C Page: a simple version of the Unix utility wc. It asks for a file name, reads the file a character at a time and reports the number of lines, words and characters. Written for a QL C compiler whose header file is stdio_h (YES and NO are defined there, so the #define lines are commented out). The column explains the multiple assignment nl = nw = nc = 0, character constants such as ‘\n’ and ‘\t’, and the %d format of printf. This is C source, not a SuperBASIC program, so there is no download; type it in and compile it with a QL C compiler. Published in Sinc-Link January-February 1991 ([The C Page](https://timexsinclair.com/article/the-c-page-4/)). ## Source Code ``` /* Program 6 */ #include /* #define YES 1 #define NO 0 The two constants are defined in stdio_h for this implementation, but may not be for others */ main () { int c, fd, nl, nw, nc, word; char file[20]; printf("Enter File: "); gets(file); fd = fopen(file,"r"); if (fd == NULL) { printf("File Open Error"); abort(1); } word = NO; nl = nw = nc = 0; while (( c = getc(fd)) != EOF) { ++nc; if ( c == '\n') ++nl; if ( c == ' ' || c == '\n' || c == '\t') word = NO; else if (word == NO) { word = YES; ++nw; } } fclose(fd); printf("Lines: %d Words: %d Chars: %d",nl,nw,nc); } ```