简体   繁体   中英

errors when switching back and forth between VisualStudio and CodeBlocks using C

I am learning to program late in life (50+ yrs old). I am using Visual Studio 2015 and Code::Blocks ver 16.01.

I have some questions.

First, why do VS and CB require 'tweaks' to make them compile and run the code included below? What is up with M_PI? (I thought it was part of , but apparently VS doesn't recognize it) Why do I have to do a #define in VS but not in CB? Why doesn't M_PI display its defined value?

Side note: I understand scanff has been deprecated, but if you must go down that path feel free. We are required to use it in class.

This is my initial entry into Code Blocks. It compiles and runs. this uses stdio.h and math.h

 // >>>> CODEBLOCKS VERSION <<<<
    #include <stdio.h>
    #include <math.h>

    // M_PI is 3.14159265358979323849

    int main ( void )
    {
        double myNumber = 0.0;

        printf("Enter a number.\n");
        scanf("%lf", &myNumber);

        printf("You entered: %lf.", myNumber);
        printf("This is the value of pi: %.20lf.", M_PI);

        return 0;
    }

When I paste this into Visual Studio and compile, I get an error. Specifically, 'identifier “M_PI” is undefined'. If I add #define M_PI 3.14159265358979323849, the error goes away, BUT I get Code C4996 -- 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. If I change the scanf to scanf_s the error clears, code compiles and runs the same as the codeblocks version. This uses stdio.h and math.h and

 // >>>> VISUAL STUDIO VERSION <<<<
    #include <stdio.h>
    #include <math.h>

    #define M_PI 3.14159265358979323849

    int main(void)
    {
        double myNumber = 0.0;

        printf("Enter a number.\n");
        scanf_s("%lf", &myNumber);

        printf("You entered: %lf.", myNumber);
        printf("This is the value of pi: %.20lf.", M_PI);

        return 0;
    }

Thanks in advance!

由于某些未知原因,在包含math.h之前,MSVS需要#define _USE_MATH_DEFINES

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM