0

I'd like ask why GCC compiles my C-code, while it has errors, which MSVS noticed. I've already found a solution to solve these errors. But I'd like to know, why GCC had not seen them. Here is my code:

#define _USE_MATH_DEFINES
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>


double randomer( double randitude)
{
    double random;
    if(randitude != 0)
    {
        int integerranditude = randitude*1000000;
        int base = rand()%integerranditude;
        random = (2*(base/1000000.0)) - randitude;
        printf( "%lf\n", random);
    }
    else 
    {
        random = 0.0;
    }
    return random;
}

void counttofile(double x, double period, double expression, double amplitude, double random, double randitude)
{
    FILE* wyniki = fopen("wyniki.txt", "wt");
    for(x = 0; x <= period*M_PI*2; x = x + 0.128)
    {
        random = randomer(randitude);
        expression = amplitude*(sin(x) + cos(x)) + random;
        fprintf(wyniki, "(%lf) = %lf\n", x, expression);
        /*expression = sin(x) + cos(x) + random;
        if(fabs(expression) <= fabs(amplitude)) 
        {
            fprintf(wyniki, "(%lf) = %lf\n", x, expression);
        }
        else
        {
            fprintf(wyniki, "(%lf) = poza zakresem\n", x);
        }*/
    }
    fclose(wyniki);
}   
void unitchange(double period, double amplitude)
{
    period = period * 180 / M_PI;
    amplitude = amplitude * 180 / M_PI;
}


int main(void)
{
    srand (time(NULL));
    double x, period, expression, amplitude, randitude, random;
    random = 0;
    int command;
    x = 0;
    printf("Co chciałbyś zrobić? \n1. wygenerowac wartosci funkcji sin(x) + cos(x)\n");
    printf("2. wygenerowac wartosci funkcji sin(x) + cos(x),\n   a nastepnie zaszumic o wskazana amplitude.\n");
    printf("Wpisz numer zadania: ");
    scanf("%d", &command);
    if(command == 1)
    {
            randitude = 0;
            printf("Podam wyniki dzialania sin(x) + cos(x).\nOkresl w jakim zakresie amplitudy mam podac wyniki: ");
            scanf("%lf", &amplitude);
            expression = sin(x) + cos(x);
            printf("\nIle okresow mam policzyc? ");
            scanf("%lf", &period);
            unitchange(period, amplitude);
            counttofile(x, period, expression, amplitude, random, randitude);
    }
    else if(command == 2)
    {
            printf("Podam wyniki dzialania sin(x) + cos(x), zaszumionego o zadana amplitude.\nOkresl w jakim zakresie amplitudy mam podac wyniki: ");
            scanf("%lf", &amplitude);
            expression = sin(x) + cos(x);
            printf("\nIle okresow mam policzyc? ");
            scanf("%lf", &period);
            printf("Amplituda szumu ma wynosić: ");
            scanf("%lf", &randitude); 
            unitchange(period, amplitude);
            counttofile(x, period, expression, amplitude, random, randitude);
    }   
    else
    {
        printf("\nNieprawidlowy numer polecenia. Sprobuj jeszcze raz.");
    }
    int proba = 0;
    for(proba =0; proba <=10; proba ++)
    {
        randomer(randitude);
    }
    return 0;
}

Thanks for help.

2
  • 4
    It might be helpful to post the specific error message(s) Commented Oct 16, 2013 at 19:40
  • 3
    Ah, I see a declaration after a statement. Perhaps your problem is the Microsoft compiler does not support C99. MSVS is stuck in the dark ages, gcc is not. Commented Oct 16, 2013 at 19:44

1 Answer 1

4

This will not compile in MSVS because it contains a declaration after a statement. MSVS is based on an early version of C that did not allow this. gcc is based on a later version of C that does support this.

int main(void)
{
    srand (time(NULL));
    double x, period, expression, amplitude, randitude, random;

I believe MSVS is based on C89, this feature was introduced in C99. No doubt someone will correct me if I am wrong.

Sign up to request clarification or add additional context in comments.

4 Comments

Actually it compile just fine, tried on Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86 that comes with VS2010.
@LuizFelipe Ok, maybe OP's compiler is out of date. Did you save it is a .c file or .cpp file, that makes a difference.
Yes, you are correct, the C compiler is stuck on C89, but the cpp compiler not, you can force it to use cpp compiler. The cpp compiler has partial support for C++11, but is improving. Here is a good explanation herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99
Given that this is meant to be C, compiling it as C++ is a dodgy manoeuvre. But MSVS 2012/2013 finally allow this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.