datatypes & comments example

In this code sample, I’ve placed lots of comments. To see how well I’ve done at commenting, I’ll leave it to you to ask any questions in the forums.


/* a simple calculator
July 19, 2005
Ron R.
C Tutorials for the home ed student
int main()

// declare dataspace
int first; // first integer
int second; // second integer
/* note you can declare 2 variable of the same type
in the same declaration */
float firstf,secondf; // first and second float values

// now get some values from the keyboard
printf(“\nEnter a whole number: “);
// scanf() scans formatted input
scanf(“%i”,&first); // %i is the formatting for an integer
printf(“Enter a second whole number: “);
scanf(“%i”,&second); // store the value in the variable second
// now decimals
printf(“\nEnter 2 decimal numbers separated by a space: “);
/* %f is the formatting for an floating point
you can also scan for more than 1 value at a time
scanf(“%f %f”,&firstf,&secondf);

// show some math
printf(“\n%i + %i = %i\n”,first,second,first + second);
printf(“%i – %i = %i\n”,first,second,first – second);
printf(“%i x %i = %i\n”,first,second,first * second);
printf(“\n%f + %f = %f\n”,firstf,secondf,firstf + secondf);
printf(“%f – %f = %f\n”,firstf,secondf,firstf – secondf);
printf(“%f * %f = %f\n”,firstf,secondf,firstf * secondf);

return 0;


Give it a try and see what happens with different ranges of values. Also, try giving it some bogus data like letters, etc.

Author: Ron

Homeschooling dad of 4 (ages 27 - 14), grampy to 3, WordPress core contributor, former farmboy & software developer by profession.