// anything to the right of // (on the same line) is a comment, // and is ignored by the compiler |
// include any libraries needed, e.g.
#include <cstdio>
// declare any global constants, e.g.
const float Pi = 3.14;
// provide the main routine, with any actions to be carried out
// "inside" the main - i.e. between the { and }
int main()
{
// variable declarations, computation, input/output, etc
}
|
// you must declare variables and constants before (above) // the point where they are first used, // providing the type, name, and initial value for the variable // for constants you must also precede the declaration with the word const) // declare a floating point constant for the value of Pi const float Pi = 3.14; // declare an integer variable to hold the radius of a circle, // giving it an initial value of 0 int radius = 0; // declare a float variable to hold the circumference of a circle float circumference = 0; |
// print the statement Hello there! followed by a newline
printf("Hello there!\n");
// print the current contents of an integer, e.g. radius
printf("The current radius is %d\n", radius);
// print the current contents of a float, e.g. Pi
printf("We are using %g as the value of Pi\n", Pi);
|
// compute circumference as Pi times twice the radius circumference = Pi * (2 * radius); // remember // / does integer division if given two ints to work on, // % gives you the remainder, e.g. // 13 / 4 gives 3 // 13 % 4 gives 1 // (since 13 = 3 * 4 + 1) |