none
Limit code execution in C RRS feed

  • Question

  • Hello,

    I want to write a simple program just with C language in Visual Studio. 

    Now if I used the C++ library in Visual Studio, we don't have any problem and code can run successfully but I should use just C syntax and library.

    How can I limit code execution in C? (for example, I received an error or warning when I used C++ syntax or library in C project)

    Thanks

    • Edited by Arash_89 Saturday, December 14, 2019 7:14 AM
    Saturday, December 14, 2019 7:12 AM

Answers

  • From MSDN :

    "

    The C and C++ languages are similar, but not the same. The Visual C++ compiler uses a simple rule to determine which language to use when it compiles your code. By default, the Visual C++ compiler treats all files that end in .c as C source code, and all files that end in .cpp as C++ source code.

    To force the compiler to treat all files as C non-dependent of file name extension, use the /Tc compiler option.

    "


    • Marked as answer by Arash_89 Tuesday, December 17, 2019 8:02 PM
    Saturday, December 14, 2019 9:09 AM
  • What do you mean by a C library or C++ library? 

    The libraries contain executable code and you have no idea what language they were written in.  Since libraries contain multiple modules, there could even be a mix of languages.  Some modules could even be written in assembler.

    • Marked as answer by Arash_89 Tuesday, December 17, 2019 8:02 PM
    Saturday, December 14, 2019 2:23 PM
  • The C language does not have classes and related features and anything developed for C++ will use classes and related features and therefore any library developed for C++ is very much not likely to work using C.


    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by Arash_89 Tuesday, December 17, 2019 8:02 PM
    Sunday, December 15, 2019 6:17 PM
  • The compiler decides the syntax based on your file name.  If you compile "test.cpp", it uses a C++ compiler.  If you compile "test.c", it uses a C compiler.  It's possible to override that with the /Tp or /Tc switches, but it's better to use the file name.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Arash_89 Tuesday, December 17, 2019 8:02 PM
    Monday, December 16, 2019 7:38 AM

  • I want to write a simple program just with C language in Visual Studio. 

    How can I limit code execution in C? (for example, I received an error or warning when I used C++ syntax or library in C project)

    Just as a footnote to the replies from Castorix31 and Tim Roberts, you will
    probably find that all the project templates supplied with VS create source
    files with the .cpp extension.  So they will be compiled as C++ using a C++
    compiler. If you change the extension to .C by right-clicking the mouse
    cursor on the source file name in the Solution Explorer window under
    "Source Files" and choosing "Rename" from the menu, then the IDE will 
    compile it as C code using a C compiler.

    You can try it by creating a new project using the project template for a
    Windows Desktop Console application. It generates a source file with this
    code in it and an extension of .cpp:

    #include <iostream>

    int main()
    {
        std::cout << "Hello World!\n";
    }

    If you do a Build it should compile with no errors.

    Now change the extension to .C in the Solution Explorer window as described 
    above and Rebuild. You should get a ton of errors as that is not valid C code.

    - Wayne

    • Marked as answer by Arash_89 Tuesday, December 17, 2019 8:02 PM
    Monday, December 16, 2019 10:13 AM

All replies

  • From MSDN :

    "

    The C and C++ languages are similar, but not the same. The Visual C++ compiler uses a simple rule to determine which language to use when it compiles your code. By default, the Visual C++ compiler treats all files that end in .c as C source code, and all files that end in .cpp as C++ source code.

    To force the compiler to treat all files as C non-dependent of file name extension, use the /Tc compiler option.

    "


    • Marked as answer by Arash_89 Tuesday, December 17, 2019 8:02 PM
    Saturday, December 14, 2019 9:09 AM
  • What do you mean by a C library or C++ library? 

    The libraries contain executable code and you have no idea what language they were written in.  Since libraries contain multiple modules, there could even be a mix of languages.  Some modules could even be written in assembler.

    • Marked as answer by Arash_89 Tuesday, December 17, 2019 8:02 PM
    Saturday, December 14, 2019 2:23 PM
  • The C language does not have classes and related features and anything developed for C++ will use classes and related features and therefore any library developed for C++ is very much not likely to work using C.


    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by Arash_89 Tuesday, December 17, 2019 8:02 PM
    Sunday, December 15, 2019 6:17 PM
  • The compiler decides the syntax based on your file name.  If you compile "test.cpp", it uses a C++ compiler.  If you compile "test.c", it uses a C compiler.  It's possible to override that with the /Tp or /Tc switches, but it's better to use the file name.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Arash_89 Tuesday, December 17, 2019 8:02 PM
    Monday, December 16, 2019 7:38 AM

  • I want to write a simple program just with C language in Visual Studio. 

    How can I limit code execution in C? (for example, I received an error or warning when I used C++ syntax or library in C project)

    Just as a footnote to the replies from Castorix31 and Tim Roberts, you will
    probably find that all the project templates supplied with VS create source
    files with the .cpp extension.  So they will be compiled as C++ using a C++
    compiler. If you change the extension to .C by right-clicking the mouse
    cursor on the source file name in the Solution Explorer window under
    "Source Files" and choosing "Rename" from the menu, then the IDE will 
    compile it as C code using a C compiler.

    You can try it by creating a new project using the project template for a
    Windows Desktop Console application. It generates a source file with this
    code in it and an extension of .cpp:

    #include <iostream>

    int main()
    {
        std::cout << "Hello World!\n";
    }

    If you do a Build it should compile with no errors.

    Now change the extension to .C in the Solution Explorer window as described 
    above and Rebuild. You should get a ton of errors as that is not valid C code.

    - Wayne

    • Marked as answer by Arash_89 Tuesday, December 17, 2019 8:02 PM
    Monday, December 16, 2019 10:13 AM