CHAPTER 1 ========= (hello.c) #include int main(void) { printf("Hello, world!\n"); return 0; } Source -> preprocssing -> compiling -> linking -> executable hello.c "gcc hello.c -o hello" hello ============================================================= (pacific_sea.[ch]) /* Measuring the Pacific Sea. */ #include "pacific_sea.h" int main(void) { int pacific_sea = AREA; /* in sq kilometers */ double acres, sq_miles, sq_feet, sq_inches; printf("\nThe Pacific Sea covers an area"); printf(" of %d square kilometers.\n", pacific_sea); sq_miles = SQ_MILES_PER_SQ_KILOMETER * pacific_sea; sq_feet = SQ_FEET_PER_SQ_MILE * sq_miles; sq_inches = SQ_INCHES_PER_SQ_FOOT * sq_feet; acres = ACRES_PER_SQ_MILE * sq_miles; printf("In other units of measure this is:\n\n"); printf("%22.7e acres\n", acres); printf("%22.7e square miles\n", sq_miles); printf("%22.7e square feet\n", sq_feet); printf("%22.7e square inches\n\n", sq_inches); return 0; } ===== the file pacific_sea.h: ----------------------- #include #define AREA 2337 #define SQ_MILES_PER_SQ_KILOMETER 0.3861021585424458 #define SQ_FEET_PER_SQ_MILE (5280 * 5280) #define SQ_INCHES_PER_SQ_FOOT 144 #define ACRES_PER_SQ_MILE 640 ===== output: ------- The Pacific Sea covers an area of 2337 square kilometers. In other units of measure this is: 5.7748528e+05 acres 9.0232074e+02 square miles 2.5155259e+10 square feet 3.6223572e+12 square inches ============================================================= (getchar.c) #include int main(void) { int c; while ((c = getchar()) != EOF) putchar(c); return 0; } ============================================================= (for_min_max.c) /* Compute the minimum, maximum, sum, and average. */ #include #include int main(void) { int i; double x, min, max, sum, avg; if (scanf("%lf", &x) != 1) { printf("No data found - bye!\n"); exit(1); } min = max = sum = avg = x; printf("%5s%9s%9s%9s%12s%12s\n%5s%9s%9s%9s%12s%12s\n\n", "Count", "Item", "Min", "Max", "Sum", "Average", "-----", "----", "---", "---", "---", "-------"); printf("%5d%9.1f%9.1f%9.1f%12.3f%12.3f\n", 1, x, min, max, sum, avg); for (i = 2; scanf("%lf", &x) == 1; ++i) { if (x < min) min = x; else if (x > max) max = x; sum += x; avg = sum / i; printf("%5d%9.1f%9.1f%9.1f%12.3f%12.3f\n", i, x, min, max, sum, avg); } } ============================================================= (for_minmax_func.c) #include float maximum(float x, float y); float minimum(float x, float y); void prn_info(void); int main(void) { int i, n; float max, min, x; prn_info(); printf("Input n: "); scanf("%d", &n); printf("\nInput %d real numbers: ", n); scanf("%f", &x); max = min = x; for (i = 2; i <= n; ++i) { scanf("%f", &x); max = maximum(max, x); min = minimum(min, x); } printf("\n%s%11.3f\n%s%11.3f\n\n", "Maximum value:", max, "Minimum value:", min); return 0; } float maximum(float x, float y) { if (x > y) return x; else return y; } float minimum(float x, float y) { if (x < y) return x; else return y; } void prn_info(void) { printf("\n%s\n%s\n\n", "This program reads an integer value for n, and then", "processes n real numbers to find max and min values."); } ============================================================= (bubble_sort.c) #include #define CLASS_SIZE 5 int main(void) { int i, j, score[CLASS_SIZE], sum = 0, tmp; printf("Input %d scores: ", CLASS_SIZE); for (i = 0; i < CLASS_SIZE; ++i) { scanf("%d", &score[i]); sum += score[i]; } for (i = 0; i < CLASS_SIZE - 1; ++i) /* bubble sort */ for (j = CLASS_SIZE - 1; j > i; --j) if (score[j-1] < score[j]) { /* check the order */ tmp = score[j-1]; score[j-1] = score[j]; score[j] = tmp; } printf("\nOrdered scores:\n\n"); for (i = 0; i < CLASS_SIZE; ++i) printf(" score[%d] =%5d\n", i, score[i]); printf("\n%18d%s\n%18.1f%s\n\n", sum, " is the sum of all the scores", (double) sum / CLASS_SIZE, " is the class average"); return 0; } output: ------- Input 5 scores: 63 88 97 53 77 Ordered scores: score[0] = 97 score[1] = 88 score[2] = 77 score[3] = 63 score[4] = 53 378 is the sum of all the scores 75.6 is the class average ============================================================= (nice_day.c) /* Have a nice day! */ #include #include #define MAXSTRING 100 int main(void) { char c, name[MAXSTRING]; int i, sum = 0; printf("\nHi! What is your name? "); for (i = 0; (c = getchar()) != '\n'; ++i) { name[i] = c; if (isalpha(c)) sum += c; } name[i] = '\0'; printf("\n%s%s%s\n%s", "Nice to meet you ", name, ".", "Your name spelled backwards is "); for (--i; i >= 0; --i) putchar(name[i]); printf("\n%s%d%s\n\n%s\n", "and the letters in your name sum to ", sum, ".", "Have a nice day!"); return 0; }