Your first program
Print the message Hello, world!, followed by a newline.
Hello, world!The first session clarifies what happens between the moment we type text into an editor and the moment the computer executes a program. Understanding the four stages - writing, compiling, linking, execution - explains most of the errors you will run into for the rest of the semester.
gcc program.c -o program.exe then ./program.exe| Stage | What it does | Result | Typical errors |
|---|---|---|---|
| Writing | writing the code in an editor or IDE | .cpp file | - |
| Preprocessing | expands #include and #define | expanded source code | header file not found |
| Compilation | translates the source into machine code | .obj file | wrong syntax, incompatible type, undeclared variable |
| Linking | links object modules and libraries together | .exe file | function declared but not defined, missing main |
| Execution | loads the program into memory and runs it | - | division by zero, invalid memory access |
undefined reference to ..., with no line number, is a linking error:
the function was declared, but its body was not found anywhere.Writing a program uses syntactic units that fall into a few categories:
| Category | Description | Examples |
|---|---|---|
| Identifiers | names given to types, variables, functions | radius, _maxx, a5, A_B_C |
| Keywords | reserved identifiers, with predefined meaning | int, while, return, class |
| Punctuation marks | delimit regions of code | [ ] ( ) { } , : ; # |
| Whitespace | separators, improve readability | space (32), tab (9), newline (10) |
| Operators | specify the operations performed | + - * / % = == && |
| Constants | fixed values, numeric or strings | 23, 3.14, 'A', "text" |
| Identifier | Valid? | Reason |
|---|---|---|
Radius | yes | starts with a letter |
radius | yes | different from Radius - the language is case sensitive |
_maxx | yes | can start with an underscore |
a5 | yes | the digit is not in the first position |
5a | no | the first character is a digit |
A%B | no | % is not an allowed character |
Total, total, and TOTAL are three completely distinct
variables. This is a frequent source of "undeclared variable" errors for beginners.The general structure of a program is made up of three zones:
| Zone | Contents | Mandatory? |
|---|---|---|
| I - preprocessing directives | #include, #define | optional |
| II - global declarations | types, function prototypes, global variables | optional |
| III - function definitions | function bodies, including main() | mandatory |
main(). It is unique and cannot be overloaded. Unlike Pascal, in C/C++ functions
cannot be defined one inside another.
#include <stdio.h> // zone I: preprocessing directives
#define PI 3.14159 // symbolic constant
int runCount = 0; // zone II: global variable
int main(void) // zone III: definition of the main function
{
float radius, area; // local declarations
printf("Enter the radius: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Circle area = %.2f\n", area);
return 0; // 0 = program finished successfully
}
#include <iostream>
using namespace std;
const double PI = 3.14159; // in C++, const is preferred over #define
int main()
{
double radius, area;
cout << "Enter the radius: ";
cin >> radius;
area = PI * radius * radius;
cout << "Circle area = " << area << endl;
return 0;
}
printf, scanf) that take an explicit format. In C++, streams are
used (cout, cin), and the type is deduced automatically by the
compiler - safer and more convenient.Here you can write and run C code directly on the page. The program runs in the browser, with no compiler installed: press Run and watch the output below. The Step by step button executes the program instruction by instruction and shows, on the right, the value of each variable at that moment.
\n from the first printf and run it
again. Notice that the two lines end up on the same line - \n is the only thing
that moves the cursor to the next line.This is where you write the code. The ✓ Check button runs your program on several data sets and shows you right away where the result differs. At the end you submit the track you chose, and the instructor grades it automatically.
Every exercise has the program already started, and the comments tell you what is left to add. Write the code, press ✓ Check and read what each failed test says. If you get stuck, open the hints one at a time. You may also print messages for the user (such as Enter n:); the test looks at what you print at the end.
Print the message Hello, world!, followed by a newline.
Hello, world!Read two integers and print their sum.
2 35Read the radius r of a circle (a real number) and print its area with two decimals. Use the constant PI from the skeleton.
13.14Read the length and width of a rectangle (integers). Print the perimeter and the area on one line, separated by a space.
4 314 12Read a temperature in degrees Celsius (a real number) and print it in degrees Fahrenheit, with one decimal: F = C · 9 / 5 + 32.
100212.0Problems start from an empty main. The output format must be followed exactly (spaces and newlines do not matter unless the statement says otherwise), so do not print prompts. The visible tests cover only the simple cases; grading also runs hidden tests chosen to catch the edge cases.
Read a whole number of seconds s (0 ≤ s < 1 000 000). Print the duration in the format hh:mm:ss, each field with at least two digits.
366101:01:01Read the real coordinates x1 y1 x2 y2 of two points in the plane. Print the distance between them, with three decimals.
0 0 3 45.000Read an amount s in cents (integer, 0 ≤ s ≤ 100 000). Print the minimum number of 50, 10, 5 and 1 cent coins that make it up, in that order, on one line.
871 3 1 2Read three integers. On one line, print their average computed with integer division and the real average, with two decimals.
7 8 108 8.33; character and read the error message - note the line indicated.compute();) and observe that the error occurs at link time, not at compile time.int sum; and use Sum.#define for PI and %8.3f formatting for alignment. Compare the
results with those obtained using M_PI from <math.h> and
explain any differences.