// line_counting.c
#include <stdio.h>
/* count lines in input */
int main(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);
return 0;
}
Word Counting
// word_counting.c
#include <stdio.h>
#define IN 1 /* Inside a word */
#define OUT 0 /* OUtside a word */
/* count lines, words, and characters in input */
int main(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;
}
else if (c == ' ' || c == 'n' || c == 't')
{
state = OUT;
}
else if (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);
return 0;
}
Arrays
// arrays.c
#include <stdio.h>
/* count digits, white space, others */
int main(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'];
}
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);
}
Functions
// power.c
#include <stdio.h>
// Function prototype
int power(int m, int n);
/* test power function */
int main(int argc, char *argv[])
{
for (int i = 0; i < 10; ++i)
{
printf("%d %d %d\n", i, power(2, i), power(-3, i));
}
return 0;
}
/* power: raise base to n-th power; n >= 0 */
int power(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>
#define MAXLINE 1000 /* Maximum input line size */
int get_line(char line[], int maxline);
void copy(char to[], char from[]);
/* print longest input line */
int main(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);
}
return 0;
}
/* get_line: read a line into `s`, return `length` */
int get_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 */
void copy(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>
#define MAXLINE 1000 /* 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 */
int get_line(void);
void copy(void);
/* print longest input line; specialized version */
int main(int argc, char *argv[])
{
int len = 0; /* Current line length */
extern int max;
extern char longest[];
while ((len = get_line()) > 0)
{
if (len > max)
{
max = len;
copy();
}
}
if (max > 0)
{
printf("%s", longest);
}
return 0;
}
/* get_line: specialized version */
int get_line(void)
{
int c = 0;
int i = 0;
extern char 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 */
void copy(void)
{
int i = 0;
extern char line[];
while ((longest[i] = line[i]) != '\0')
{
++i;
}
}