Development Environment - Learning C by Example (2015)

Learning C by Example (2015)

1. Development Environment

This chapter explains how to start with development environment using C.

1.1 Getting Started

C is a general-purpose, imperative computer programming language. It supports structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations.

In this chapter, we prepare development environment to develop C program. You can write C program with your program. We focus on Windows, Linux and Mac platforms.

1.2 Compilers

There are many C compilers to develop C program. In this book, we use GCC as C compiler for Linux, Mac and Windows. For Windows users, you also can use Visual C++ compiler.

1.2.1 Linux

Installation of GCC C is easy. For Ubuntu Linux, you can do it using console and write this script

$ sudo apt-get install build-essential

If installation already finished, you can check GCC version on console and write script as below

gcc --version

You will see gcc version, for instance shown in Figure above.

1.2.2 Windows

If your computer has Windows platform, you can use Cygwin. Download it on http://cygwin.com/setup.exe and then run it.

On setup dialog, you choose Devel package as follows.

win

After installed, you will get Cygwin terminal. Run it.

You can check GCC version. The following is a sample of output for GCC version

image

1.2.3 Mac

To install GCC, you can install Xcode so you get GCC. After installed, you can verify it by checking GCC version.

mac1-1

1.3 Development Tools

Basically you can use any editor tool for instance, vi, vim, gedit, Eclipse. The following is vi editor in Ubuntu.

Here is gedit editor

1.4 Hello World

Now we start to write the first program using C.

Firstly, open your text editor and create new file, called hello.c

Let's write this code

#include <stdio.h>

int main() {

printf("Hello C\n");

return 0;

}

Save this file.

Now open your Terminal and compile this file.

sudo gcc hello.c

If you check, you will get file a.out. GCC will generate a.out file if you don't specify an output file name. You can define the output compiled file as follows

sudo gcc hello.c -o hello

After compiled, you can run the compiled file by writing this command (for instance, the compiled file is hello.out)

./hello

A sample output of program is run on Mac platform.

mac1-2