So I figured out with the aid of math.stackexchange that the problem was related with updating the position and velocity earlier than the time they should be updated and more intermediate variables were needed. The now working code is below:
//Compilation and run
//gcc oscillatorimprovedEuler.c -lm -o oscillatorimprovedEuler && ./oscillatorimprovedEuler
#include <stdio.h>
#include <math.h>
// The global constans are defined in the following way (having the constant value througout the program
#define m 1.0 // kg
#define k 1.0 // kg/sec^2
#define h 0.1 // sec This is the time step
#define N 200 // Number of time steps
int main(void)
{
// We need to define this many variables to avoid early updating the position and velocity
double x = 0.0, xpre = 0, xcor = 0;
double v = 5.0, vpre = 0, vcor = 0; // Just like the previous case
double t = 0;
double E = (m * v * v + k * x * x) / 2.0; // This is the energy in units of Joules
FILE *fp = fopen("oscillatorimprovedEuler.dat", "w+");
int i = 0;
for(i = 0; i < N ; i++)
{
if (i == 0)
{
fprintf(fp, "%f \t %f \t %f \t %f \n", x, v, E, t);
}
xpre = x + (h) * v;
vpre = v - (h) * k * x / m;
vcor = v - (h) * ((k * x / m) + (k * xpre / m)) / 2.0;
xcor = x + (h) * (v + vpre) / 2.0;
E = (m * vcor * vcor + k * xcor * xcor) / 2.0;
t += h;
fprintf(fp, "%f \t %f \t %f \t %f \n", xcor, vcor, E, t);
x = xcor, v = vcor;
}
fclose(fp);
return 0;
}