A compiler for C



Example Program
  1. /* C Example Program
  2. Input: An integer, listlen, where listlen is less than
  3. 100, followed by listlen-integer values
  4. Output: The number of input values that are greater than
  5. the average of all input values */
  6.  
  7. int main (){
  8. int intlist[99], listlen, counter, sum, average, result;
  9. sum = 0;
  10. result = 0;
  11. scanf("%d", &listlen);
  12. if ((listlen > 0) && (listlen < 100)) {
  13. /* Read input into an array and compute the sum */
  14. for (counter = 0; counter < listlen; counter++) {
  15. scanf("%d", &intlist[counter]);
  16. sum += intlist[counter];
  17. }
  18. average = sum / listlen;
  19. /* Count the input values that are > average */
  20. for (counter = 0; counter < listlen; counter++)
  21. if (intlist[counter] > average) result++;
  22. /* Print result */
  23. printf("Number of values > average is:%d\n", result);
  24. }
  25. else
  26. printf("Error—input list length is not legal\n");
  27. }


Readability Example
  1. /* factorial.c -- It computes repeatedly the factorial of an integer entered * by the user. It terminates when the integer entered is not * positive. */
  2.  
  3. #include int fact(int n);
  4.  
  5. int main(void)
  6. {
  7. int current;
  8.  
  9. printf("Enter a positive integer [to terminate enter non-positive] > ");
  10.  
  11. scanf("%d", ¤t);
  12. while (current > 0)
  13. {
  14. printf("The factorial of %d is %d\n", current, fact(current));
  15. printf("Enter a positive integer [to terminate enter non-positive] > ");
  16. scanf("%d", ¤t);
  17. }
  18. }
  19.  
  20. /* n is a positive integer. The function returns its factorial */
  21. int fact(int n)
  22. {
  23. int lcv; /* loop control variable */
  24. int p; /* set to the product of the first lcv positive integers */
  25.  
  26. for(p=1, lcv=2; lcv <= n; p=p*lcv, lcv++)
  27. {
  28. return p;
  29. }
  30. }