/* File: hello_world.c * * A simple program that prints "Hello World". * main() is declared with no arguments, but no type checking */ #include int main() { printf("Hello World\n"); return 0; } ----------------------------------------------------------------------------- /* File: hello_world2.c * * A simple program that prints "Hello World". * main() is declared with its full arguments */ #include int main(int argc, char *argv[]) { printf("Hello World\n"); return 0; } ----------------------------------------------------------------------------- /* File: hello_world3.c * * A simple program that prints "Hello World". * main() is declared with no arguments, allowing type checking */ #include int main(void) { printf("Hello World\n"); return 0; } ----------------------------------------------------------------------------- /* File: sums.c * * A simple program that prints the sum of each number * from 0 to MAX_VALUE. */ #include #define MAX_VALUE 10 /* The max sum to compute */ int main(void) { int i; /* generic loop variable */ int sum = 0; /* the sum accumulator */ for (i = 0; i <= MAX_VALUE; i++) { sum += i; /* short for sum = sum + i */ printf("sigma %d = %d\n", i, sum); } return 0; } ----------------------------------------------------------------------------- /* File: capitalize.c * * Prints out a string in which all letters are capitalized */ #include #define STRING_LENGTH 80 /* max length of string */ int main(void) { char text[STRING_LENGTH] = "hello there"; /* the string */ int i; /* a loop counter */ i = 0; while (text[i] != '\0') /* traverse the string */ { if ((text[i] >= 'a') && (text[i] <= 'z')) /* if a lowercase letter */ text[i] = text[i] - 'a' + 'A'; /* then capitalize it */ i++; } printf("%s\n", text); return 0; } ----------------------------------------------------------------------------- /* File: concat.c * * Reads in strings and concats them into one long string. * Terminates and prints result when user enters "end". */ #include /* Standard I/O library */ #include /* The strings library */ #define true 1 /* define true and false */ #define false 0 #define MAX_STRING 80 /* the max string length */ #define NO_MORE_INPUT "end" /* the terminating string */ int main(void) { char instring[MAX_STRING]; /* the input string */ char text[MAX_STRING]; /* the concatenated string */ char done = false; /* flag when finished */ text[0] = '\0'; /* initialize the result string */ while (!done) { if (scanf("%s", instring) != 1) /* read in string */ { /* ... if there is an error ... */ printf("Error reading input\n"); done = true; } else if (!strcmp(instring, NO_MORE_INPUT)) { /* user types "end" */ printf("%s\n", text); /* print the final string */ done = true; /* and exit */ } else if (strlen(instring) + strlen(text) < MAX_STRING) strcat(text, instring); /* still room, so concatenate */ else /* otherwise we are out of space */ { printf("Error: out of space\n"); done = true; } } return 0; } ----------------------------------------------------------------------------- /* File: xchoosey.c * * An inefficient calculation of "x choose y" */ #include int factorial(int x); /* prototype for factorial function */ int main(void) { int x = 10; int y = 3; int z; z = factorial(x) / (factorial(y) * factorial(x-y)); printf("%d choose %d = %d\n", x, y, z); return 0; } /* * factorial() - takes an integer x and returns x! */ int factorial(int x) { int accumulator = 1; while (x > 0) { accumulator = accumulator * x; x--; } return (accumulator); }