How To Program C In Windows
This document details the process of getting started creating console-based C programs. Panel-based programs are quite uncomplicated - they take input from the keyboard (or a file stored on deejay) and they produce output to the text-based panel window. You will need 2 things to create C programs: a text editor to write the source lawmaking for the plan and a compiler to catechumen the source lawmaking to an executable file and then the program can exist run (on Windows, executable files accept a ".exe" extension). We will use Microsoft'southward Visual Studio Community software which provides both a powerful code editor and a C compiler (as these are packaged in the same software, this is called an integrated development environment, or IDE).
To get started, we need to install Visual Studio Community. We will and then look at two different means to develop simple console-based C programs:
OK, let'southward become started by installing Visual Studio Customs...
Installing Visual Studio Customs
There are several different versions of Microsoft's Visual Studio - the Community version is free and includes all of the necessary libraries and tools that we need to develop C programs. You can google "Visual Studio Community" to observe out more than, or go straight to the relevant page on Microsoft'due south website:
Make sure "Windows" is selected and click on the "Download VS Community 2017" button as shown below:
Once yous click the "Download VS Community 2017" button, you will be prompted to save a file called "vs_community_<some numbers>......exe" - relieve this file somewhere convenient such equally your Desktop:
Just double-click on this downloaded file ("vs_community_<some numbers>......exe"). This volition launch the installer program (you may exist prompted to confirm to "Run" this plan), in which case but click "Run":
In the commencement stage of the installation a number of files will exist downloaded to your computer. You lot will just demand to be patient until this stage is complete:
In the second stage of the installation you lot volition be shown the configuration options beneath. In that location are several tabs along the top - you only need to expect at the default tab which is called "Workloads" every bit shown below.
IMPORTANT: Make sure that you click the "Desktop evolution with C++" option as shown beneath:
Once you select the "Desktop evolution with C++" option, you will see a "Summary" shown on the correct hand side of the window every bit illustrated below. Although some of these components are listed nether "Optional", it is simplest to accept all of the default options as highlighted. It is particularly of import that you lot ensure "VC++ 2017 Version ... tools" and "Windows 10 SDK" are both selected. Without these you volition not exist able to compile C programs!
IMPORTANT: Make certain that the "VC++ 2017 Version ... tools" and "Windows ten SDK" options are both selected before standing:
When y'all are ready you tin and then click the "Install" button in the lesser correct corner of the window. This will start the installer one time again, and the necessary components will be added:
One time this is finished, you may be prompted to restart your computer (click "Restart"):
After your computer has restarted, yous should detect that Visual Studio Community is now installed. To bank check that the installation was successful, you lot can endeavor launching Visual Studio. Please note, there is no need to run the installation programme again (this is the "vs_community_<some numbers>......exe" file that you downloaded from Microsoft's website). This installation program is no longer needed. Instead, to launch Visual Studio, y'all should select it from the Start menu every bit shown:
As this is the first time you have launched Visual Studio, you volition be asked to "Sign in" (just ignore this if you like by clicking "Not now, perchance later"), then y'all volition be asked to select a "theme" (the default theme called "Blue" is just fine) and then in that location will be short filibuster while the environment is configured for the first fourth dimension:
And finally, you should encounter the Visual Studio Community start page! Congratulations, y'all can at present beginning creating your first C program!
Y'all take two options for the process of developing your C programs, and these are described beneath.
Dandy, now that Visual Studio Community is installed, you have two options for developing and running C programs on Windows. The first option involves using any text editor you like to write your source code, and using the "cl" command within the Programmer Control Prompt to compile your code. The 2nd option involves using Visual Studio Customs as the development environs, and creating a Visual Studio "project" to organise your files.
Both options are described next...
Option 1: Using a text editor and the Developer Control Prompt to compile
With this option, we are going to compile our programs from the command line (using a program called the "Developer Command Prompt") rather than using the graphical Visual Studio environs. When nosotros installed Visual Studio Community, this too installed the command line tools that we need.Editing the source code
We can write our source code in any text editor we like. In this case, I am using a text editor called TextPad, and y'all tin see in the diagram below that I have created a C source file called "example.c" within a folder chosen "MyFirstProgram" which is inside a directory called "paul" on the "C:" of my computer:
Compiling and running the program
To compile our programme, we are going to run the Visual Studio command line compiler, called "cl". Nosotros run this command from an surroundings called the "Developer Command Prompt". To launch the Visual Studio "Developer Command Prompt", type "Developer" into the search box in the Start bill of fare and yous should see an choice "Programmer Control Prompt for VS 2017" announced equally shown. Click on this plan:
You should see a new window open up - and the championship of the window should be "Developer Command Prompt for VS 2017". This is the window into which we will type the commands to compile and execute our C program.
From the Developer Command Prompt, you lot change directories by typing:
- cd / (to motion to the tiptop, or root level, of the directory tree) or
- cd .. (to motility up one level in the directory tree) or
- cd DIR_NAME (to move down ane level into the directory named "DIR_NAME")
You can modify drives by typing the drive letter on its ain (for example, to switch to the "E" drive or "E:"):
- East:
You lot can list all of the files in the current directory by typing:
- dir
And finally, you tin compile your source file with the command:
- cl /W4 example.c
In this case, "example.exe" is the name of the executable file that volition be created if your code compiles successfully. In the screenshot below, you tin see that a new file called "case.exe" was created in the same directory every bit the source file.
To run the executable, y'all simply type:
- example
which means: "execute the program named "example" which is in the current directory
These commands are illustrated below:
And that'southward information technology - yous can now edit the source file, re-compile the code, and run the programme again!
Good luck!
Option 2: Using Visual Studio Customs as your evolution environment
Visual Studio Community is an integrated development environment. This means we can write our source lawmaking, compile the code, run the executable and view the output all from within the Visual Studio plan. Nosotros are now going to look at how to exercise this. Visual Studio organises programs into what it calls "Projects". So we are going to create a new Visual Studio projection in a folder called "paul" on the "C:" of our reckoner. Every bit yous can come across at the moment, this folder is empty:
Start, we launch Visual Studio Community from the Beginning card:
And we create a new project by selecting "File > New Projection..."
Make sure you lot advisedly select the correct template. This should be "Windows Desktop Wizard" which can be constitute by selecting "Windows Desktop" underneath the "Visual C++" template.
As you lot tin can meet in this case, we have called the project "ExampleProject", and we accept selected the location to be the folder "C:\paul". Also, we accept left the choice to "Create directory for solution" unchecked:
We are so shown the Windows Desktop wizard which allows united states to configure our new project. We must make sure that the Awarding blazon is Console Application (.exe). We must besides create an "empty" projection (and leave "Export Symbols", "Precompiled Header" and "Security Development Lifecycle checks" all unchecked). Once this is right, then click on "OK"
The project will then exist created, and you will be shown a graphical representation of the projection. This is illustrated below. Because the projection is empty, the first affair we must exercise is add a source file:
We can add a source file by right-clicking the "Source files" item (in the Solution Explorer pane) and and then selecting "Add together > New Item..."
When we create our source file, it is very important that we give it the correct extension (i.eastward. suffix) for a C file. This must be ".c":
We can then edit our source file by writing code directly in the Visual Studio Customs code editor:
When we are ready to compile our source file, we can select "Build Solution" from the "Build" card:
It is useful to have the "Output" pane open - in the diagram below, the "Output" pane displays the condition of the compilation process. In this example, the compilation succeeded with no errors:
Later on our lawmaking has compiled successfully, we tin can then run our program. The best mode to practice this is to select "Start without debugging" from the "Debug" carte du jour. This "Commencement without debugging" option will add together a pause once our program has executed, assuasive united states of america to view the output (until nosotros press a fundamental) before the window closes:
And, if all has gone well, we will see the output from our plan in a new control window:
It is really very useful to have an understanding of the files that Visual Studio creates and where these files are stored on deejay. If yous have followed the suggested organisation described here, then the folder in which we created the project (which, in this example, was "C:\paul") will contain just a single folder with the proper noun of the project.
Inside this "ExampleProject" folder, there will be several files, including our source file, "example.c". One of the folders within the "ExampleProject" is called "Debug".
The "Debug" folder is where the actual executable file for our program is created past the compiler. The diagram beneath illustrates the organisation on disk.
Now, if y'all want to brand changes to your program, you return to the editor and change the source file. To run the program over again, you must recompile it ("Build solution") and then run it ("Outset without debugging").
And that'southward all there is to it! Good luck!
Source: https://www.cs.auckland.ac.nz/~paul/C/Windows/index.php
Posted by: peelsering.blogspot.com
0 Response to "How To Program C In Windows"
Post a Comment