###### Books/Introduction to C Programming Language/ Chapter 2

C is highly portable and you can run it on pretty much any system.

You will need a text editor. I like Vim, which will run on pretty much anything, but you should use whatever you are comfortable with. You can also use Visual Studio Code if you want to develop on a graphical text editor.

You will also need a C compiler that can handle at least C99. Ideally you will have an environment that looks enough like Linux that you can also run other command-line tools like gdb, make, and possibly git. How you get this depends on your underlying OS.

### Linux

Pretty much any Linux distribution will give you this out of the box. You may need to run your package manager to install missing utilities like the gcc C compiler.

### OSX

OSX is not Linux, but it is Unix under the hood. You will need a terminal emulator (the built-in Terminal program works, but I like iTerm2. You will also need to set up XCode to get command-line developer tools. The method for doing this seems to vary depending on which version of XCode you have.

You may end up with gcc being an alias for clang instead of the GNU version of gcc. Most likely the only difference you will see is the details of the error messages.

Other packages can be installed using Homebrew.

### Windows

What you can do here depends on your version of Windows.

• For Windows 10, you can install Windows Subsystem for Linux. This gives you the ability to type bash in a console window and get a full-blown Linux installation. You will need to choose a Linux distribution: if you don’t have a preference, I recommend Ubuntu.
• You will need to use the apt program to install things like gcc. If you use Ubuntu, it will suggest which packages to install if you type a command it doesn’t recognize.
• You can also run ubuntu.exe from Windows to get a nicer terminal emulator than the default console window.
• For other versions of Windows, you can install a virtualization program like VirtualBox or VMware and run Linux inside it.
• You may also be able to develop natively in Windows using Cygwin, but this is probably harder than the other options and may produce surprising portability issues when moving your code to Linux.

## How to compile and run programs

The basic steps are

• Creating the program with a text editor of your choosing.
• Compiling it with gcc.
• Running it.

### Creating the program

Use your favorite text editor. The program file should have a name of the form foo.c; the .c at the end tells the C compiler the contents are C source code. Here is a typical C program:

#include <stdio.h>

/* print the numbers from 1 to 10 */
int main(int argc, char **argv)
{
int i;

puts("Now I will count from 1 to 10");
for(i = 1; i <= 10; i++) {
printf("%d\n", i);
}

return 0;
}


### Compiling and running a program

Here’s what happens when I compile and run it:

$gcc -g3 -o count count.c$ ./count
Now I will count from 1 to 10
1
2
3
4
5
6
7
8
9
10
\$


The first line is the command to compile the program. The dollar sign is my prompt, which is printed by the system to tell me it is waiting for a command. The command calls gcc with arguments -g3 (enable maximum debugging info), -o (specify executable file name, otherwise defaults to a.out), count (the actual executable file name), and count.c (the source file to compile). This tells gcc that we should compile count.c to count with maximum debugging info included in the executable file.

The second line runs the output file count. Calling it ./count is necessary because by default the shell (the program that interprets what you type) only looks for programs in certain standard system directories. To make it run a program in the current directory, we have to include the directory name.

Don’t worry if you don’t understand everything that this program does. The concepts will be explained later.