C语言 Visual Studio 中的 C 编程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20821416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:33:58  来源:igfitidea点击:

C programming in Visual Studio

cvisual-studio

提问by HelloWorld

Can I use Visual Studio to learn C programming? In the new project menu I can choose between Visual Basic, Visual C#, Visual C++, Visual F# and others but I don't see "C" or "Visual C".

我可以使用 Visual Studio 学习 C 编程吗?在新项目菜单中,我可以在 Visual Basic、Visual C#、Visual C++、Visual F# 等之间进行选择,但我看不到“C”或“Visual C”。

采纳答案by 72DFBF5B A0DF5BE9

Short answer: Yes, you need to rename .cpp files to c, so you can write C: https://msdn.microsoft.com/en-us/library/bb384838.aspx?f=255&MSPPError=-2147217396

简短回答:是的,您需要将 .cpp 文件重命名为 c,这样您就可以编写 C:https: //msdn.microsoft.com/en-us/library/bb384838.aspx?f=255&MSPPError =-2147217396

From the link above:

从上面的链接:

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 regardless of file name extension, use the /Tc compiler option.

默认情况下,Visual C++ 编译器将所有以 .c 结尾的文件视为 C 源代码,并将所有以 .cpp 结尾的文件视为 C++ 源代码。要强制编译器将所有文件视为 C 而不考虑文件扩展名,请使用 /Tc 编译器选项。

That being said, I do not recommend learning C language in Visual Studio, why VS? It does have lots of features you are not going to use while learning C

话虽如此,我不建议在 Visual Studio 中学习 C 语言,为什么是 VS?它确实有很多你在学习 C 时不会用到的功能

回答by Switch

Yes, you very well can learn C using Visual Studio.

是的,您可以很好地使用 Visual Studio 学习 C。

Visual Studio comes with its own C compiler, which is actually the C++ compiler. Just use the .cfile extension to save your source code.

Visual Studio 带有自己的 C 编译器,它实际上是 C++ 编译器。只需使用.c文件扩展名来保存您的源代码。

You don't have to be using the IDE to compile C. You can write the source in Notepad, and compile it in command line using Developer Command Promptwhich comes with Visual Studio.

您不必使用 IDE 来编译 C。您可以在记事本中编写源代码,然后使用Visual Studio 附带的开发人员命令提示符在命令行中编译它。

Open the Developer Command Prompt, enter the directory you are working in, use the clcommand to compile your C code.

打开 Developer Command Prompt,进入你正在工作的目录,使用cl命令编译你的 C 代码。

For example, cl helloworld.ccompiles a file named helloworld.c.

例如,cl helloworld.c编译一个名为helloworld.c.

Refer this for more information: Walkthrough: Compiling a C Program on the Command Line

有关更多信息,请参阅:演练:在命令行上编译 C 程序

Hope this helps

希望这可以帮助

回答by Simon

Yes it is, none of the Visual Stdio editions have C mentioned, but it is included with the C++ compiler (you therefore need to look under C++). The main difference between using C and C++ is the naming system (i.e. using .cand not .cpp).

是的,Visual Stdio 版本都没有提到 C,但它包含在 C++ 编译器中(因此您需要在 C++ 下查看)。使用 C 和 C++ 的主要区别在于命名系统(即使用.c而不是.cpp)。

You do have to be careful not to create a C++ project and rename it to C though, that does not work.

您必须小心,不要创建 C++ 项目并将其重命名为 C,但这不起作用

Coding C from the command line:

从命令行编码 C:

Much like you can use gccon Linux (or if you have MinGW installed) Visual Studio has a command to be used from command prompt (it must be the Visual Studio Developer Command Prompt though). As mentioned in the other answeryou can use clto compile your c file (make sure it is named .c)

就像您可以gcc在 Linux 上使用(或者如果您安装了 MinGW)一样,Visual Studio 有一个可以从命令提示符使用的命令(但它必须是 Visual Studio 开发人员命令提示符)。正如在另一个答案中提到的,您可以cl用来编译您的 c 文件(确保它被命名为.c

Example:

例子:

cl myfile.c

Or to check all the accepted commands:

或者检查所有接受的命令:

C:\Program Files (x86)\Microsoft Visual Studio17\Community>cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27030.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

C:\Program Files (x86)\Microsoft Visual Studio17\Community>

Coding C from the IDE:

从 IDE 编码 C:

Without doubt one of the best features of Visual Studio is the convenient IDE.

毫无疑问,Visual Studio 的最佳功能之一是方便的 IDE。

Although it takes more configuring, you get bonuses such as basic debugging before compiling (for example if you forget a ;)

虽然需要更多的配置,但在编译之前你会得到一些好处,比如基本的调试(例如,如果你忘记了一个;

To create a C project do the following:

要创建 C 项目,请执行以下操作:

Start a new project, go under C++ and select Empty Project, enter the Nameof your project and the Locationyou want it to install to, then click Ok. Now wait for the project to be created.

启动一个新项目,在 C++ 下选择Empty Project,输入Name您的项目的名称和Location您希望它安装到的位置,然后单击Ok。现在等待项目创建。

enter image description here

enter image description here

Next under Solutions Explorerright click Source Files, select Addthen New Item. You should see something like this:

Next 在Solutions Explorer右击下Source Files,选择Addthen New Item。您应该会看到如下内容:

enter image description here

enter image description here

Rename Source.cppto include a .cextension (Source.cfor example). Select the location you want to keep it in, I would recommend always keeping it within the project folder itself (in this case C:\Users\Simon\Desktop\Learn\My First C Code)

重命名Source.cpp以包含.c扩展名(Source.c例如)。选择要保存的位置,我建议始终将其保存在项目文件夹本身中(在这种情况下C:\Users\Simon\Desktop\Learn\My First C Code

It should open up the .cfile, ready to be modified. Visual Studio can now be used as normal, happy coding!

它应该打开.c文件,准备修改。Visual Studio 现在可以用作正常、快乐的编码!

回答by Terry Chen

Yes, you can:

是的你可以:

You can create a C-language project by using C++ project templates. In the generated project, locate files that have a .cpp file name extension and change it to .c. Then, on the Project Properties page for the project (not for the solution), expand Configuration Properties, C/C++ and select Advanced. Change the Compile As setting to Compile as C Code (/TC).

您可以使用 C++ 项目模板创建 C 语言项目。在生成的项目中,找到文件扩展名为 .cpp 的文件并将其更改为 .c。然后,在项目(不是解决方案)的“项目属性”页面上,展开“配置属性”、“C/C++”并选择“高级”。将编译为设置更改为编译为 C 代码 (/TC)。

https://docs.microsoft.com/en-us/cpp/ide/visual-cpp-project-types?view=vs-2017

https://docs.microsoft.com/en-us/cpp/ide/visual-cpp-project-types?view=vs-2017

回答by cscit

Download visual studio c++ express version 2006,2010 etc. then goto create new project and create c++ project select cmd project check empty rename cc with c extension file name

下载visual studio c++ express version 2006,2010等。然后转到创建新项目并创建c++项目选择cmd项目检查空重命名cc与c扩展文件名

回答by Neil Roy

You can use Visual Studio for C, but if you are serious about learning the newest C available, I recommend using something like Code::Blocks with MinGW-TDM version, which you can get a 32 bit version of. I use version 5.1 which supports the newest C and C++. Another benefit is that it is a better platform for creating software that can be easily ported to other platforms. If you were, for example, to code in C, using the SDL library, you could create software that could be recompiled with little to no changes to the code, on Linux, Apple and many mobile devices. The way Microsoft has been going these days, I think this is definitely the better route to take.

你可以使用 Visual Studio for C,但如果你认真学习可用的最新 C,我建议使用类似 Code::Blocks 和 MinGW-TDM 版本的东西,你可以获得 32 位版本。我使用支持最新 C 和 C++ 的 5.1 版。另一个好处是它是一个更好的平台,用于创建可以轻松移植到其他平台的软件。例如,如果您要使用 SDL 库用 C 语言编写代码,您可以创建可以在 Linux、Apple 和许多移动设备上重新编译而无需更改代码的软件。微软这些天的发展方式,我认为这绝对是更好的选择。