Title here
Summary here
Our first program will declare and use constants. Here’s the full source code.
#include <cstdio>
#include <cmath>
const char* s = "constant";
int main() {
printf("%s\n", s);
// A `const` statement can appear anywhere a `var` statement can.
const int n = 500000000;
// Constant expressions perform arithmetic with arbitrary precision.
const double d = 3e20 / n;
printf("%e\n", d);
// A numeric constant has no type until it’s given one, such as by an explicit conversion.
printf("%lld\n", static_cast<long long>(d));
// A number can be given a type by using it in a context that requires one, such as a variable assignment or function call.
// For example, here `sin` expects a `double`.
printf("%f\n", sin(n));
return 0;
}
To run the program, save the code in a file named constant.cpp
, and use a C++ compiler to build and execute it.
$ g++ -o constant constant.cpp
$ ./constant
constant
6.000000e+11
600000000000
-0.284705
Now that we can run and build basic C++ programs, let’s learn more about the language.