Sie sind auf Seite 1von 9

Lab 1: Installation of Programming

Environment and Writing Your First C


Program.
Programming in C is fun and interesting. In this session of C programming lab class, you will
create, edit, compile and execute a sample C program. Before we begin to learn about C
programs, you must configure your computer with C compiler.

To create and execute a C program you require minimum two software’s.

1. A text editor (such as Notepad, Notepad++, Gedit, TextEdit, Sublime etc.).


2. A C compiler (Optionally CodeBlocks IDE).

In this sessional, I will discuss about C compiler and steps to setup C compiler along with
CodeBlocks in your machine.

1.1 The GCC C compiler


A Compiler is a program or set of programs that converts source code written in a high-level
language to low-level language (assembly language or machine language). The compiler checks
source code file for syntactical errors and translates the source file to low-level machine code, if
it is error free.

There are many C compilers available such as GCC C, Borland Turbo C, Visual C, Quick C
etc. In this entire C programming course, we will work with GCC C compiler.

GCC C compiler is reliable, efficient and popular C/C++ compiler among developers. It is open
source and available to download for all most all operating systems. It is pre-installed C compiler
in all UNIX based operating systems.

1.2 What is CodeBlocks IDE?


CodeBlocks is an IDE (Integrated Development Environment) used to create, edit, compile,
debug and execute C/C++ programs in single place. It is very powerful IDE for developing C
projects.

In this entire C programming course, we will use CodeBlocks IDE to create, compile, debug and
execute C programs. However, there are many other IDE's for developing C projects such as
Netbeans, Eclipse, DevC++, Microsoft Visual Studio Code etc.
1.2.1 Download and install CodeBlocks with C compiler in
Windows
CodeBlocks is available in two variant - with and without compiler. For Windows it comes with
MinGW C compiler suite. MinGW C compiler is a windows variant of GCC C compiler.

Download the latest build of CodeBlocks with C compiler from its official website.

Note: You must download the CodeBlocks with MinGW setup executable file.

The download size is about 80MB, which hardly takes 1-2 minutes on good internet connection.

After downloading, run the installer. Installation is simple next...next stuff. The installation
wizard will guide you throughout the installation processes.

1.2.1.1 Configure CodeBlocks and GCC C compiler

On first run CodeBlocks requires few things to gear up. So let us first configure CodeBlocks.

1. Associate CodeBlocks with C compiler. CodeBlocks automatically detects our installed


C compiler. Set the detected compiler as default and click OK button.
2. Associate C/C++ files with CodeBlocks IDE. Make sure all C/C++ files opens in
CodeBlocks by default.
3. Set an idle perspective to use CodeBlocks for beginner.

4. Sometimes you will encounter problems running the compiler and debugger with
CodeBlocks. To resolve the issue follow the instructions.

1. Verify the Compiler's Path:


Goto "Settings" menu ⇒ "Compiler" ⇒ In "Selected Compiler", choose "GNU
GCC Compiler" ⇒ Select tab "Toolchain Executables" ⇒ Check the
"Compiler's Installation Directory". It shall be set to the "MinGW" sub-
directory of the CodeBlocks installation directory, for example, suppose that
CodeBlocks is installed in " C:\Program Files\CodeBlocks\", set it to "
C:\Program Files\CodeBlocks\MinGW".

2. Verify the Debugger’s Path:


Similarly, check the debugger's path. Goto "Settings" menu ⇒ "Debugger" ⇒
Expand "GDB/CDB debugger" ⇒ Select "Default" ⇒ In "Executable path",
provide the full-path name of "gdb.exe", for example, " C:\Program
Files\CodeBlocks\MinGW\gdb32\bin\gdb32.exe".
1.2.2 Creating C program in CodeBlocks IDE
1. Open CodeBlocks IDE and create a new file. Click on File → New → File.

2. From the New form template window select C/C++ source and click Go button.
3. If you see a welcome message, click next to skip the welcome message. Make sure you have
checked Skip this next time checkbox if you do not want to see this welcome message again.

Next, select your language from C/C++ source window and click Next button.
4. Give name to your file and specify the location.

It is always recommended to save all your C programs in a C workspace (directory). Creating a


C workspace is onetime process. For now, create a new C workspace (folder) with name “CP
Lab 1” anywhere in your computer.

Once you created a C workspace, click the ellipsis button present under File name with full
path. Browse your C workspace; mention the file name click Finish.
5. Write and save your first C program. Press Ctrl + S to save.

1.2.3. Compiling and running C program in CodeBlocks


Once you created your first C program it’s time to compile and run the program.

To compile and run a C program, click Build → Build and run to compile and build your C
program, alternatively use the shortcut key F9.

In case, your program contains any errors. Error messages are shown in the Build messages tab
below the code editor.
If your program runs successfully, then the following output window will appear showing the
message “Hello World”.

Exercise:

1. Write a C program which prints the following message:

.. see

………I

… … … remember!

Das könnte Ihnen auch gefallen