Online C compiler - No signup needed
To run the code in the online c compiler, just click "Run". You can also edit it by clicking "Edit".
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
return 0;
}
Is C programming still relevant in 2026?
Yes, C programming remains highly relevant in 2026. It's the backbone of operating systems (Linux, Windows) and performance-critical applications where direct hardware control are important. In Astronomy, many packages are still written in C.
I built this online c compiler because I think starting with C forces you to understand the logic behind programming languages. It forces you to understand how data is stored in computer memory, how variables map to bytes, how arrays really work, how pointers and addresses connect data together, why memory allocation matters, etc. If you understand how a compiled language like C works, it will be much easier for you to learn an interpreted language like Python. Switching from C to Python or other interpreted languages is much easier than the reverse.
When I was a Physics undergraduate student, the first program I learned was C. At the time, I did not even have a laptop so I had to learn all the logics first. This helped me a lot. Later when I had to switch to other programming languages like Matlab, things were a lot easier.
If you are just starting, I suggest you start with C.
My recommendation is that you should not rush to write code immediately. First, understand the mathematical foundation and algorithmic strategy. Sketch the logic on paper. Write things by hand. After that, translate your understanding into programming syntax. You do not have to memorize syntaxes; you can just google them. The most important thing is to understand the algorithm.
I have included here an exercise for you to understand how to approach a problem step by step in C. You can try to solve it yourself; don't look at the solution yet. Try to write your own program and use the online c compiler.
Limitations of the online C compiler
For security reason, I have implemented the following restrictions:
- Rate limiting: 10 executions per minute
- Memory limit: 2GB maximum
- CPU quota: 50%
- File limit: 8192 open files maximum
- Task limit: 200 concurrent processes
- Allowed headers: stdio.h, stdlib.h, string.h, math.h, ctype.h, limits.h, float.h, stdbool.h, stdint.h, stddef.h, errno.h, assert.h, stdarg.h, inttypes.h
I think these restrictions are reasnable for educational purposes. The online compiler is not meant to be used for heavy computation.
Numerical integration using the trapezoidal rule
Computing the definite integral of a function is an excellent exercise because it teaches you how to turn a maths idea into a clear algorithm.
Given a function \( f(x) \), we want to calculate:
For many functions, we cannot find an analytical solution. This is where we need numerical integration. For this exercises, we will use the trapezoidal rule to .....
Here is how it works. Instead of calculating the exact area under a curve, we approximate it using trapezoids.
Imagine you want to find the area under a curve between points \( a \) and \( b \). If you divide this interval into \( n \) subintervals, each of width \( h = \frac{b-a}{n} \), you create \( n \) trapezoids. The area of each trapezoid approximates the area under the curve for that segment.
Step 1: divide the interval
Partition \([a, b]\) into \( n \) equal subintervals:
Step 2: calculate trapezoid areas
For each subinterval \([x_i, x_{i+1}]\), the area of the trapezoid is:
This is simply the average of the two function values, multiplied by the width.
Step 3: sum all trapezoids
The total approximate integral is:
$$I \approx \frac{h}{2} \left[ f(x_0) + 2f(x_1) + 2f(x_2) + \cdots + 2f(x_{n-1}) + f(x_n) \right]$$Have you noticed any pattern? Well, the first and last terms appear once, while all intermediate terms appear twice (because each interior point is shared by two adjacent trapezoids).
We can rewrite this more compactly as:
Numerical solution
Let's apply what we have discussed to a concrete example where we know the answer analytically. We want to compute:
So the exact answer is 2. Let's see how close we get numerically!
Think through these steps before coding:
- Input: define the function \( f(x) \), lower bound \( a \), upper bound \( b \), and number of intervals \( n \)
- Calculate step size: \( h = (b - a) / n \)
- Initialize sum: Start with \( sum = f(a) + f(b) \)
- Loop through interior points: For \( i = 1 \) to \( n-1 \):
- Calculate \( x_i = a + i \times h \)
- Add \( 2 \times f(x_i) \) to the sum
- Multiply by h/2: \( integral = (h / 2) \times sum \)
- Output: Display the result
Numerical accuracy considerations
The trapezoidal rule has an error that decreases as \( O(h^2) \), meaning if you double the number of intervals (halve \( h \)), the error decreases by a factor of 4. This is important for understanding convergence.
Note that more intervals generally mean better accuracy, but also more computation. For smooth functions like \( \sin(x) \), even \( n = 100 \) gives excellent results.
Translating the theory into a C code
Now that we understand the mathematics and algorithm, let's implement it in C. I have put the code into the online C compiler. You can click "Run" to see the results. If you want to do more practices, click "Edit" and then run the code.
Code structure
We first define f(x) separately. This modular approach means you can easily change the function you're integrating—just modify this one function!
The Algorithm Implementation: The trapezoidal_rule() function directly translates our mathematical formula into C:
double h = (b - a) / n;— Calculate step sizedouble sum = f(a) + f(b);— Initialize with endpoints- The loop adds
2.0 * f(x_i)for each interior point return (h / 2.0) * sum;— Final calculation
Using the online C compiler
#include <stdio.h>
#include <math.h>
// Define the function to integrate
// We'll integrate sin(x) from 0 to π
double f(double x) {
return sin(x);
}
// Trapezoidal rule implementation
double trapezoidal_rule(double a, double b, int n) {
double h = (b - a) / n; // Step size
double sum = f(a) + f(b); // First and last terms
// Add the intermediate terms (each multiplied by 2)
for (int i = 1; i < n; i++) {
double x_i = a + i * h;
sum += 2.0 * f(x_i);
}
// Multiply by h/2 to get final result
return (h / 2.0) * sum;
}
int main(void) {
// Define integration bounds
double a = 0.0; // Lower bound
double b = M_PI; // Upper bound (π)
int n = 100; // Number of intervals
printf("=== Numerical integration using trapezoidal rule ===\n\n");
printf("Computing: ∫ sin(x) dx from 0 to π\n");
printf("Exact answer: 2.0\n\n");
// Test with different numbers of intervals
printf("Results with varying accuracy:\n");
printf("%-15s %-15s %-15s\n", "Intervals (n)", "Approximation", "Error");
printf("---------------------------------------------\n");
int intervals[] = {10, 50, 100, 500, 1000};
double exact = 2.0;
for (int i = 0; i < 5; i++) {
int current_n = intervals[i];
double result = trapezoidal_rule(a, b, current_n);
double error = fabs(result - exact);
printf("%-15d %-15.10f %-15.10f\n", current_n, result, error);
}
printf("\n=== Demonstration Complete ===\n");
printf("Notice how error decreases as n increases!\n");
return 0;
}
- Click "Copy" to copy the code
- Click "Edit" to modify parameters
- Try changing the function—replace
sin(x)withx*xorexp(x) - Adjust the integration bounds (a and b)
- Change the number of intervals (n)
- Click "Run" to see your results (remember: 10 runs per minute limit)
Expected output
When you run this program in the online C compiler, you should see the following output:
=== Numerical integration using trapezoidal rule === Computing: ∫ sin(x) dx from 0 to π Exact answer: 2.0 Results with varying accuracy: Intervals (n) Approximation Error --------------------------------------------- 10 1.9835235375 0.0164764625 50 1.9993570999 0.0006429001 100 1.9998392694 0.0001607306 500 1.9999934945 0.0000065055 1000 1.9999983736 0.0000016264 === Demonstration complete === Notice how error decreases as n increases!
What you have learned
This exercise demonstrates an example workflow of numerical computing:
- Mathematical understanding (trapezoidal rules)
- Algorithm design (breaking down the problem into logical steps)
- Implementation (translating mathematics into working code)
- Verification (comparing numerical results with known answers)
- Convergence analysis (Observing how accuracy improves)
Importance of the methodology
This theory -> methodology -> implementation workflow is how scientists and engineers work. When you understand the mathematics and algorithm, translating them into a working script becomes natural.
Try to experiment with different functions and parameters using the online c compiler.
Next steps and challenges
Challenge yourself:
- Integrate \( \int_{0}^{1} e^{-x^2} dx \) (the Gaussian integral)
- Implement Simpson's rule (a more accurate method)
- Compare trapezoidal vs. Simpson's rule for the same function
- Try integrating a discontinuous function—what happens?
- Measure computation time for different values of n
Comments (0)
No comments yet. Be the first to comment!
Leave a comment