Scientific notation in C
What you'll learn: How to use the e notation to represent very large or very small decimal numbers without typing endless zeros.
The problem with big numbers
Imagine you are writing a program to calculate the distance between stars or the weight of a single atom. In standard decimal form, these numbers are a nightmare to type. You might end up writing 0.00000000000000000000001 or 602200000000000000000000.
Not only is this tedious, but it is also prone to "finger-slip" errors. If you accidentally type one too many zeros, your entire calculation will be off by a factor of ten. In C, floating-point types (float and double) solve this using Scientific Notation.
Enter the 'e'
In C, the letter e (or E) stands for "times ten to the power of." It acts like a shortcut for moving the decimal point.
2.5e3means (which is )1.0e-4means (which is )
Think of the number after the e as an instruction for the decimal point. A positive number tells the decimal to jump to the right (making the number larger), while a negative number tells it to jump to the left (making it smaller).
Coding with scientific notation
You can assign these values directly to your variables. C handles the math behind the scenes, storing them as standard floating-point numbers.
#include <stdio.h>
int main() {
// The speed of light in m/s (approx 300,000,000)
double speedOfLight = 3.0e8;
// The charge of an electron (very small!)
double electronCharge = -1.602e-19;
printf("Light speed: %f\n", speedOfLight);
printf("Electron charge: %e\n", electronCharge);
return 0;
}
In the code above, %f prints the number in standard decimal format, while %e tells C to print the output back in scientific notation. This is incredibly helpful when your results are so small or large that a standard printout would just show a long string of zeros.
Key takeaway
Use e notation in C to represent powers of ten, making your code cleaner and preventing errors when working with extreme scales.