Wednesday, November 21, 2018

Running a C-Program using the Command prompt on Windows

Make sure you have a compiler installed, for my case i used GCC. Usually, if its not installed, when you type the command "gcc -v" on the command line,  you will get an error "gcc is not recognised as an internal or external command".  If you get this error, even after installing the program, then try setting the environment variables as bellow;

  • Open control panel
  • Type "environment"
  • Click to open "Edit the system environment variables for your account"
  • Select PATH,  then Edit
  • In the edit section of variable value, add the path where  your compiler is stored right after the termination (;)
    • The path where your gcc compiler could be located in the directory "C:\MinGW\bin" or according to your customised path.
    • On mine, since i had installed code blocks, its location was C:\Program Files (86)\CodeBlocks\MinGW\bi
Write your program in any editor for example Notepad, save it with a .c extension. I stored mine on the desktop
  • Open the command Line/Prompt
  • Type "cd desktop" if your program is stored on the desktop Or navigate to the directory where your program is stored
  • Type: gcc <fileName.c > e,g gcc loop.c This will compile your code and produce an executable with a name e.g a.exe because we never specified the name of our executable. 

    • You can name your file by following this structure; gcc -o <name_of_executable> <name_of_source_code
  • Run the program by typing the executable file name on your command line E.g "a" 
Here is the code i used

















Bellow are the commands and the output