-3

So I have this code where I'm trying to define a dynamic matrix with n*n size. It turns out that, instead of the output printing the value for G, it prints 0 instead. Any idea why?

#define G 6.67408e-11

typedef struct matrix
{
    double mass;
    double cmx;
    double cmy;

}MATRIX;

MATRIX **mtr;

void main(int argc, char** argv){
    const long n = atoi(argv[1]);

    mtr = (MATRIX**)calloc(n,sizeof(MATRIX*));
    for (int i=0; i<n; ++i)
    {
        mtr[i]=(MATRIX*)calloc(n,sizeof(MATRIX));
    }
    mtr[0][0].cmx=G;
    printf("%f\n", mtr[0][0].cmx);
}

1 Answers1

2

Just use %e or %g to display the output. If you use %f, you're telling the compiler to print the number in standard format, that would be 0.0000000000667408 but since %f is formatted as default with 6 decimal ciphers the compiler rounds it to 0.000000 .

dteod
  • 195
  • 4