Skip to content
This repository was archived by the owner on Jan 23, 2020. It is now read-only.

How To Compile And Execute C Files

Emre Kumaş edited this page Mar 12, 2018 · 1 revision

Most easy way:

You can install an IDE for example Clion, CodeBlocks etc. and copy this code into a new created project file. And just hit the run button and the IDE will do everything for you.

But if you don't have or don't want to use an IDE, you can compile and execute C files from command prompt.

In Windows:

  1. If you want to compile in Windows, first you need to install a compiler to do this job. Most knowns are MinGW and CygWin. You can install either one, it doesn't matter. Just type in Google, go to the official website, download the installer and install it. After installation an installation manager window will pop-up. In this window, you need to select base and gcc-g++ options and install them. This will install the compiler for C.

  2. After installing one of these, you need to create a path environment for your system to use these compilers in any directories. To do this, right-click your computer icon and select properties. Then click Advanced System Settings from the left panel. Then in advanced tab, select Environment Variables. Then you will select Path from System Variables and Edit. Then you will click New and type the location of the compiler. For MinGW: C:\MinGW\bin and for CygWin: C:\CygWin\bin.

  3. Now, you successfully installed the compiler. There are tons of videos in the Web if you can't figure it out without seeing it. You can watch any videos and make sure you installed it correctly.

  4. So, now we can open the command prompt and go to the correct folder. To do this we will use cd command.
    cd directory_which_C_file_reside

  5. Now, we need to tell the compiler to compile the C file. To do this;
    gcc main.c -o main

  6. Now, we can run the executable file by typing;
    main.exe

In Linux:

  1. In Linux, you can install the compiler from the terminal. But it depends on which distribution you are using.

    If you are using Fedora/Red Hat/CentOS or Scientific Linux:
    dnf groupinstall 'Development Tools'

    If you are using Debian or Ubuntu:
    sudo apt-get install build-essential manpages-dev

  2. After installing the compiler, go to the correct folder. To do this we will use cd command.
    cd directory_which_C_file_reside

  3. Now, we need to tell the compiler to compile the C file. To do this;
    gcc main.c -o main

  4. Now, we can run the executable file by typing;
    ./main

In MacOS:

  1. In MacOS, it is very similar to Linux. First you need to install GNU Development Tools.

  2. After installing the tools, you need go to the correct directory and compile the C file by typing;
    gcc -o main main.c

  3. Then run the executable file by typing;
    ./main


I tried to simply explain how to compile and run C files in most popular operating systems. If you don't understand or take any errors, visit the web and you will find tons of videos and pages explaining this process. Wish you luck!!!

Clone this wiki locally