// line_counting.c#include<stdio.h>/* count lines in input */intmain(int argc,char*argv[]){int c =0; /* Input character */int nl =0; /* Number of lines */while ((c =getchar()) != EOF) {if (c =='\n') {++nl; } }printf("%d\n", nl);return0;}
Word Counting
// word_counting.c#include<stdio.h>#defineIN1 /* Inside a word */#defineOUT0 /* OUtside a word *//* count lines, words, and characters in input */intmain(int argc,char*argv[]){int c =0; /* Input character */int nl =0; /* Number of lines */int nw =0; /* Number of words */int nc =0; /* Number of characters */int state =0; /* Inside or outside a word */ state = OUT;while ((c =getchar()) != EOF) {++nc;if (c =='\n') {++nl; }elseif (c ==' '|| c =='n'|| c =='t') { state = OUT; }elseif (state == OUT) { state = IN;++nw; } }printf("Number of lines: %d\n", nl);printf("Number of words: %d\n", nw);printf("Number of characters: %d\n", nc);return0;}
Arrays
// arrays.c#include<stdio.h>/* count digits, white space, others */intmain(int argc,char*argv[]){int c =0;int i =0;int nwhite =0;int nother =0;int ndigit[10] = {0};while ((c =getchar()) != EOF) {if (c >='0'&& c <='9') {++ndigit[c -'0']; }elseif (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);}
Functions
// power.c#include<stdio.h>// Function prototypeintpower(int m,int n);/* test power function */intmain(int argc,char*argv[]){for (int i =0; i <10; ++i) {printf("%d%d%d\n", i, power(2, i), power(-3, i)); }return0;}/* power: raise base to n-th power; n >= 0 */intpower(int base,int n){int p =1;for (int i =0; i <= n; ++i) { p *= base; }return p;}
Arguments-Call by Value
In C, all function arguments are passed "by value". This means that the called function is given the values of its arguments in temporary variables rather than the originals. This leads to some different properties than are seen with "call by reference" languages like Fortran or with var parameters in Pascal, in which the called routine has access to the original argument, not a local copy.
Character Arrays
In C, strings are represented by char arrays. Every char array is terminated by a null byte (\0). For example, a string constant like "hello\n" is stored in memory as in the following diagram:
Below is a sample program that takes input and prints out the longest line:
// longest_line.c#include<stdio.h>#defineMAXLINE1000 /* Maximum input line size */intget_line(char line[],int maxline);voidcopy(char to[],char from[]);/* print longest input line */intmain(int argc,char*argv[]){int len =0; /* Current line length */int max =0; /* Maximum length seen so far */char line[MAXLINE] = {0}; /* Current input file */char longest[MAXLINE] = {0}; /* Longest line saved here */while ((len =get_line(line, MAXLINE)) >0) {if (len > max) { max = len;copy(longest, line); } }if (max >0) {printf("%s", longest); }return0;}/* get_line: read a line into `s`, return `length` */intget_line(char s[],int lim){int c =0;int i =0;for (i =0; i < lim -1&& (c =getchar()) != EOF && c !='\n'; ++i) { s[i] = c; }if (c =='\n') { s[i] = c;++i; } s[i] ='\0';return i;}/* copy: copy `from` into `to`; assume `to` is big enough */voidcopy(char to[],char from[]){int i =0;while ((to[i] = from[i]) !='\0') {++i; }}
External Variables and Scope
In C, local variables are called "automatic variables". If you wish local variables retain their values between calls, use the static keyword.
In C, global variables are called "external varaibles". An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it; this states the type of the variable. The declaration may be an explicit extern statement or may be implicit from context.
Below is an updated version of longest_line.c which shows the usage of external variables:
// longest_line_extern.c#include<stdio.h>#defineMAXLINE1000 /* Maximum input line size */int max =0; /* Maximum length seen so far */char line[MAXLINE] = {0}; /* Current input file */char longest[MAXLINE] = {0}; /* Longest line saved here */intget_line(void);voidcopy(void);/* print longest input line; specialized version */intmain(int argc,char*argv[]){int len =0; /* Current line length */externint max;externchar longest[];while ((len =get_line()) >0) {if (len > max) { max = len;copy(); } }if (max >0) {printf("%s", longest); }return0;}/* get_line: specialized version */intget_line(void){int c =0;int i =0;externchar line[];for (i =0; i < MAXLINE -1&& (c =getchar()) != EOF && c !='\n'; ++i) { line[i] = c; }if (c =='\n') { line[i] = c;++i; } line[i] ='\0';return i;}/* copy: specialized version */voidcopy(void){int i =0;externchar line[];while ((longest[i] = line[i]) !='\0') {++i; }}