#
symbol,
#include <cstdio>
const <datatype> <identifier> =
<value>;
const int MinutesInDay = 1440; const float YardsInMetre = 1.196;
int main() {
int StartTime; // Start time, integer value float CalculatedCost; // Cost, floating point value char UserInitial; // Initial, a single character
;
{
}
CalculatedCost = 17.6 * 9; UserInitial = 'M';
Return a status value to the shell that controls user programs (0 is the default "everything is ok" status value):
return 0;
Finally, indicate the end of the main program section by matching main's opening bracket with a closing bracket:
}
// The most commonly written program in most languages #include <cstdio> int main() { printf("Hello World!"); return 0; }If compiled and run, the output would look like:
Hello World!Some notes on the Hello World code above:
cstdio
contains the code for the printf
routine
;
( )
and { }
//
to the end of the line,
have no effect on the code (compiler ignores them)
used to write explanations of the code for yourself and other programmers
They are functionally identical, but one would clearly be much easier to understand and modify than the other.
POOR STYLE IS POOR PROGRAMMING - IT CAUSES ERRORS AND COSTS TIME AND RESOURCES IN THE REAL WORLD
#include <cstdio> const float Pi =3.14 ;int main() { float radius; float diameter;float circumference ;float area; printf( "Enter radius\n" ; scanf( "%f", radius ); diameter=2 *radius;circumference= diameter* Pi;area=Pi*radius*radius;printf("Radius %f diameter %f", radius , diameter ); printf ("area %f\n" ,area);return 0;}
#include <cstdio> const float Pi = 3.14; int main() { float radius; float diameter; float circumference; float area; printf("Enter radius\n"); scanf("%f", &radius); diameter = 2 * radius; circumference = diameter * Pi; area = Pi * radius * radius; printf("Radius %f diameter %f ", radius, diameter); printf("%f area %f\n", diameter, area); return 0; }
#include <cstdio> using namespace std; // Conversion factor const float MetresToYards = 1.196; int main() { float SqMetres; // input value float SqYards; // converted value // Prompt and read input printf("Enter the fabric size in sq. metres\n"); scanf("%f", &SqMetres); // Apply conversion factor SqYards = MetresToYards * SqMetres; // Display results printf("%f square metres equals %f square yards\n", SqMetres, SqYards); // finished return 0; }