Use the mac terminal to compile and run the c program
This article introduces how to use the Mac's built-in text editing software to write C code, and use the command line to compile and run the C program in the Mac's own terminal.
1. Install the c compilation environment on mac
Open the mac's own终端
Enter the command in the terminal command line xcode-select --install
and press Enter.
If the c compilation environment is not installed, a window will automatically pop up to guide the installation;
if the compilation environment has been installed, the terminal window will prompt in English that it has been installed.
2. Write c program with the text editing tool that comes with mac
Create a folder on the desktop, name it arbitrarily (I named it c), then open 文本编辑
the software that comes with the system, set up to write plain text, change the file name to xxx.c
the format, and save the file to the file you just created folder.
Open the text editing app
Choose New Document
In the top menu bar click 格式
-制作纯文本
Change the file name to xxx.c
format, and select the save location as the newly created folder
Then type the c code directly in Notepad, for example:
#include <stdio.h>
int main ()
{
printf("hi,world\n");
reture 0;
}
3. Use the mac terminal to compile and run the c program
Open the mac terminal and use the cd command to open the newly created folder. My folder path is: cd /users/mac/Desktop/c
.
Then enter it again cc xxx.c
, cc
which is the compilation command, xxx.c
which is the c language code file just written.
If there is an error in the code, cc
the compiler will report an error after executing the command. If there is no prompt, it means that the compilation is successful, and an a.out
executable file will be generated in the folder where the c program is located.
Let's execute this a.out
executable file and continue typing on the command line ./a/out
.
At this point, the program runs successfully. END
ps. If there are multiple .c files in a folder, compiling one of them will generate an a.out executable file, and compiling other .c files will also generate a.out files, and the one generated later will overwrite the one generated earlier.
The vim editor that comes with the mac can run directly in the terminal. I have the opportunity to write an article on how to use the vim editor to write code in the terminal and run it in the terminal.