C
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Arrays and Strings
-
Section 5: Pointers
-
Section 6: Memory Management
-
Section 7: Structures and Unions
-
Section 8: The Preprocessor and Build Process
-
Section 9: Standard Library: stdio.h
-
Section 10: Standard Library: stdlib.h
-
Section 11: Standard Library: string.h
-
Section 12: Standard Library: ctype.h and wctype.h
-
Section 13: Standard Library: math.h, complex.h, fenv.h, tgmath.h
-
Section 14: Standard Library: Type and Limit Headers
-
Section 15: Standard Library: Error Handling and Debugging
-
Section 16: Standard Library: Localization and Encoding
-
Section 17: Standard Library: time.h
-
Section 18: Standard Library: Concurrency (C11)
-
Section 19: POSIX and System Programming (unistd.h)
-
Section 20: More Data Structures
-
Section 21: Algorithms in C
-
Section 22: Bitwise Operations
-
Section 23: Command-Line Programs
-
Section 24: Debugging and Best Practices
-
Section 25: Compiler and Language Internals
-
Section 26: Embedded and Cross-Platform Considerations
-
Section 27: Networking Basics
-
Section 28: Practical Projects
-
Section 29: Interview Practice
-
Section 30: C23 Modern Features
-
Section 31: More Practice and Review
107: Power and Square Root Functions
In most modern languages, you're used to having a handy operator for exponents—like ** in Python. C isn't that generous. If you want to raise a number to a power or find a square root, you have to step outside the core language and lean on the standard math library.
Mapping out a distance calculator
To see these functions in action, I want to build a simple utility that calculates the distance between two points on a 2D plane. If you remember your geometry, the distance formula is just the Pythagorean theorem in disguise: distance = sqrt((x2-x1)^2 + (y2-y1)^2). This is a perfect scenario because we need both a power function for the squaring and a square root function for the final result.
First, I'll include <math.h>. Without this, the compiler has no idea what pow() or sqrt() are. I'm using double for all my coordinates because math functions in C almost exclusively work with floating-point numbers. Using integers here would lead to a lot of annoying casting.
#include <stdio.h>
#include <math.h>
int main() {
double x1 = 2.0, y1 = 3.0;
double x2 = 5.0, y2 = 7.0;
double dx = x2 - x1;
double dy = y2 - y1;
// We need to square the differences
double distance_sq = pow(dx, 2) + pow(dy, 2);
double distance = sqrt(distance_sq);
printf("The distance is: %f\n", distance);
return 0;
}
The "Undefined Reference" headache
Here is where I usually trip up the first few times I do this. I hit compile with gcc main.c -o dist and... boom. A wall of errors. Specifically: undefined reference to 'pow' and undefined reference to 'sqrt'.
It's frustrating because the code is syntactically perfect. The problem is that <math.h> is just a header file—it tells the compiler what the functions look like, but it doesn't actually contain the code for the functions themselves. That code lives in a separate library file. On Linux and macOS, you have to explicitly tell the linker to link the math library using the -lm flag.
I have to change my compilation command to: gcc main.c -o dist -lm. Now it works. Just remember: if you see "undefined reference" to a math function, stop staring at your code and check your linker flags.
Refining the math for performance
Now, I'll let you in on a little industry secret. While pow(dx, 2) is readable, it's actually quite slow. The pow() function is designed to handle anything—fractional exponents, negative bases, the works. It's a heavy piece of machinery.
When you're just squaring a number, it's much more efficient to just multiply the number by itself. I'm going to rewrite the logic to be more "C-like"—fast and direct.
#include <stdio.h>
#include <math.h>
int main() {
double x1 = 2.0, y1 = 3.0;
double x2 = 5.0, y2 = 7.0;
double dx = x2 - x1;
double dy = y2 - y1;
// Much faster than calling pow() twice
double distance_sq = (dx * dx) + (dy * dy);
double distance = sqrt(distance_sq);
printf("The distance is: %.2f\n", distance);
return 0;
}
I also added %.2f to the printf. It cleans up the output so we aren't staring at six decimal places we don't actually need.
📋 Practical Task
Build a Hypotenuse Finder for Right Triangles
Write a program that asks the user to input the lengths of the two shorter sides (the legs) of a right-angled triangle. Your program should then calculate and print the length of the hypotenuse using the formula: hypotenuse = sqrt(a^2 + b^2).
- Use
doublefor your variables to ensure precision. - Use the
pow()function for the squaring part of the calculation. - Use the
sqrt()function for the final result. - Ensure you compile your code with the
-lmflag to avoid linker errors.
There are no comments for now.