So I have MinGW and with tweaking, I can run GCC in my CMD. What format do I need to have the file in to run it from the CMD? I tried simply just saving a notepad2 file in C scheme and it won't recognize it.
2 Answers
Seems like you are skipping over the fundamentals of compiling source code into binary executables. You should read up on that. But to cut to the chase, if you have a C program that you have typed into an editor, and saved into a file with .c extension -- let's use bedbug.c for example -- then you would use GCC with the command:
gcc bedbug.c -o bedbug
which if completely successful will say absolutely nothing. But you will then have a bedbug.exe that you can run simply by typing bedbug at the CMD prompt. You need to specify the -o part to give the name of the .exe, otherwise it will, for historical reasons, create a file named a.exe (which you could then rename as a separate step, but that's not how people usually do it).
- 8,185
C is the oldest and most basic general-purpose programming language developed by Denis Ritchie.
This tutorial will help you write, compile, and run your first C program. You will do it in the following three steps:
- Use Notepad++ to edit the C source code files.
- Use the Cygwin Terminal on Windows 10.
- Get an executable file by compiling the C source code file using the GCC compiler.
Step 1: Write a C Program in Notepad++ and Save it.
Firstly, open the Notepad++ text editor. To do so on your Windows 10 desktop, press the Win key, and in the search window, type Notepad++. Later, hit Enter.
You will get the text editor where you can write, edit, and save your C programs. You can write your own C program or simply copy the below one and paste it into your Notepad++ file.
#include <stdio.h>
int main()
{
printf("Hello World\n");
printf("Welcome to the Tutorial\n");
return 0;
}
This is your first C program.
After you paste it into your Notepad++ file, it is time to save it. Go to the File menu, and choose Save As. Later, select the C source file option, provide the name to the file and add the .c extension. Let us give the name hello.c.
Before you do anything else, make sure to note the location of the saved file. In my case, it is
_C:/Users/program/C/hello.c _
This is the path that leads to the saved file - hello.c.
You have written the program and saved it. Aren’t you curious to know what output it will produce?
Obviously, you might be. To get the output, you need to execute that program. But, the program you have written is in C language, which is not understandable by computer systems or machines. So, you need to convert it into an executable file. This means you need to transform your program into a form that is easily understandable by computers.
To get an executable file of hello.c, we will use the GCC compiler. Let us see that in the next step.
Step 2: Open Cygwin Terminal
Cygwin is a runtime environment for C and all other source code written for Unix-like operating systems. It runs natively on Microsoft Windows. So, it is obvious that you get it on Windows 10; no need for additional download and installation.
You can find the Cygwin terminal on Windows 10 in a similar way you found Notepad++.
- Hit the Win key.
- In the search bar, type Cygwin.
- You will get the Cygwin terminal in the first option itself as _Cygwin64 Terminal. _Click on it to open.
Finally, you are ready to create an executable file of your program.
Step 3: Navigate to hello.c through Cygwin Terminal
To navigate to your Notepad++ file - hello.c, you need its location or path. The location is C:/Users/program/C/hello.c
Now, you need to change your current directory and go to the directory where you have saved your file, i.e., C:/Users/program/
To do so, you need to use the cd command, which specifies Change Directory.
cd C:/Users/program/
Now, with the pwd (print working directory) command, check where you are currently and print your directory.
pwd
If everything is well, you get the following
/cygdrive/c/Users/program/
You can make sure that your source code file is in the directory you are currently in by using the ls command. This command lists all files present in the current directory.
ls
Your output should match the following one:
hello.c
Step 4: Compile hello.c to Get an Executable File
To create an executable file, you need to use the GCC compiler. Use the command gcc followed by the source file name and the executable file name, as shown below:
gcc hello.c -o hello.exe
The above command generates an executable file, hello.exe, which contains a compiled program ready for execution.
Step 5: Run the Executable File
To run the executable file, add ./ as a prefix to the name of the executable file, i.e., hello.exe.
./hello.exe
You can now see the output on the screen of the terminal.
Hello World
Welcome to the Tutorial
Kudos to you! You have successfully written and compiled, and executed your first C program. You can even modify the program and print whatever you want.
Thesaurus
After reading this tutorial, you might have come across some new concepts or words. Also, you might be curious to know more about them in detail. Let us discuss a few important concepts below.
- C Source File
It is a file containing a program in the C programming language. Generally, it has the .c extension. In our example, we had hello.c.
Many of you think that what exactly this .c means. It is nothing but just a simple hint for you to distinguish your C program files from other computer files having the .txt extension.
Moreover, you can edit the .c files with any text editor. In our tutorial, we used Notepad++. This text editor provides the feature of syntax highlighting for the C language.
Like C, there are many other programming languages. They are simply used to provide instructions to computer systems to perform specific tasks. Generally, programming languages have human-readable syntax, which is then converted into machine-executable code.
- Executable File
It is a file that is executable by a computer system. Unlike the source code file, an executable file is not human-readable. Also, it does not contain any understandable text; rather consists of numbers and characters.
When you open an executable file in Notepad++ or any text editor, you see something that is meaningless. But, it is understandable by computers.
You will need a compiler to translate the C source code file into an executable file so that computers can understand it and display the expected output.
- Compiler
A compiler is a computer program that translates the source code file into an executable file. It is basically a language processor or translator. It assists you in communicating with computer systems.
In our tutorial, we used the GCC compiler to convert a C source code file into an executable file.
Besides this, compilers come with an integrated development environment (IDE). Some popular IDEs are NetBeans, Eclipse, and Visual Studio. An IDE is a suite of development tools that facilitate the development of applications.
- **Cygwin Terminal **
It is a POSIX-compatible programming and runtime environment for the source code written for UNIX-like operating systems. It allows you to browse files from your system and run command-line programs, like the GCC compiler.
Earlier, the interaction of humans with computer systems would happen with command-line tools, which have text-based interfaces. But modern operating systems provide a graphical user interface (GUI) for communication.