Sample Basic C Programs
Sample Basic C Programs
Sample Basic C Programs
/* fahrenheit temperature lower = 0; upper = 300; step = 20; /* celsius temperature */ /* begin table /* end table here */ /* increment */
/* * print out the lines for the table */ fahr = lower; while(fahr <= upper){ /* get corresponding temp in degrees C */ celsius = 5 * (fahr - 32) / 9; /* print it */ printf("%d\t%d\n", fahr, celsius); fahr += step; } /* * say goodbye */ exit(0); }
/* * print a table for Fahrenheit to Celsius * from 0 F to 300 F * floating point version */ void main(void) { float fahr; /* fahrenheit temperature */ /* * print out the lines for the table */ for(fahr = LOWER; fahr <= UPPER; fahr += STEP) printf("%3.0f\t%6.1f\n", fahr, (5.0/9.0) * (fahr - 32)); /* * say goodbye */ exit(0);
/* * copy input to output: long version */ void main(void) { int c; /* input character */ /* * copy the input to the output * one char at a time */ do { /* read a char */ c = getchar(); /* write a char (unless it's */ /* the end of file marker) */ if (c != EOF) putchar(c); } while (c != EOF); /* * say goodbye */ exit(0);
/* * count the number of lines, words, and chars in the input * a word is a maximal sequence of nonspace characters, so * the quote "+++ --- hi bye 879+3" has 5 words ("+++", "---", * "hi", "bye", and "879+3") */ void main(void) { register int c; /* input char */ register int nl; /* line count */ register int nw; /* word count */ register int nc; /* char count */ register int state; /* in or not in a word? */ /* * initialize
*/ nl = nw = nc = 0; state = NOTIN_WORD; /* * handle input a char at a time */ while((c = getchar()) != EOF){ /* got another character */ nc++; /* is it a newline? */ if (c == '\n') nl++; /* is it a word separator? */ if (c == ' ' || c == '\t' || c == '\n') /* YES -- change state */ state = NOTIN_WORD; else if (state == NOTIN_WORD){ /* NO -- we're now in a word; update */ /* the counter and state if need be */ state = IN_WORD; nw++; } } /* * announce the results and quit */ printf("%6d\t%6d\t%6d\n", nl, nw, nc); exit(0); }
input char */ whitespace count */ other count */ counter in a for loop digit counts */
for(i = 0; i < 10; i++) ndigit[i] = 0; /* * handle input a char at a time */ while((c = getchar()) != EOF){ /* see what it is */ if (c >= '0' && c <= '9'){ /* it's a digit -- bump the right count */ ndigit[c - '0']++; } else if (c == ' ' || c == '\t' || c == '\n'){ /* it's whitespace */ nwhite++; } else{ /* it's neither a digit nor whitespace */ nother++; } } /* * announce the results and quit */ printf("digits: "); for(i = 0; i < 10; i++){ printf("'%c' %3d\t", i + '0', ndigit[i]); /* put 5 digits per line, for neat output */ if (i == 4) printf("\n "); } putchar('\n'); printf("whitespace: %d\nother: exit(0); }
*/
register int i; int ndigit[10]; /* * initialize the ndigit array */ for(i = 0; i < 10; i++) ndigit[i] = 0;
*/
/* * handle input a char at a time */ while((c = getchar()) != EOF){ /* see what it is */ switch(c){ case '0': case '1': case '2': case '3':
/* digit
case '4': case '5': case '6': case '7': case '8': case '9': ndigit[c - '0']++; break; case ' ': case '\t': case '\n': /* whitespace */ nwhite++; break; default: /* neither a digit nor whitespace */ nother++; break; }
/* * announce the results and quit */ printf("digits: "); for(i = 0; i < 10; i++){ printf("'%c' %3d\t", i + '0', ndigit[i]); /* put 5 digits per line, for neat output */ if (i == 4) printf("\n "); } putchar('\n'); printf("whitespace: %d\nother: %d\n", nwhite, nother); exit(0); }
*/ int power(int m, int n); /* * generate a table of powers of 2 */ void main(void) { register int i; /* counter in a for loop */ /* * generate the table */ for(i = 0; i < 10; ++i) printf("%3d %6d %6d\n", i, power(2, i), power(-3, i)); /* * bye! */ exit(0); }