Programming in C- Set_up Environment
Programming in C- Set_up Environment
1. Text editor
2. Compiler
A text editor is where you write your programs. A compiler translates your program into a
binary stream of 1s and 0s which computer understands. So whenever you compile a
program, the compiler creates a new binary file called executable (having .exe extension in
Windows) which computer can execute.
1. Create a program
2. Compile program
3. Run program
You can create your programs using your default text editor like Notepad in Windows or vim
in Linux, but to make things easy we will install a cross-platform IDE(Integrated
Development Environment) called Code Blocks.
IDE allows you to create, compile and run programs from a single environment. Code Blocks
comes with a compiler called GCC to compile C and C++ programs. If you are not using IDE
then you need to create the program using your text editor. Then to compile the program you
need to open command prompt or terminal and type some commands. We will first learn how
to write a simple program using an IDE, then I will show you how you can create programs
using your text editor and compile it using GCC compiler.
Installing Code Blocks in Windows
It is crucial for you to select MinGW setup because this will install the GCC compiler
on your system.
2. After downloading double click on the setup to run it and you will be presented with
the following window. Click next to continue.
3. Accept the license agreement and click next.
4. In the next window, Code Blocks will ask you to select components you want to
install. Select Full Installation, it will look something like this:
Make sure "MinGW Compiler Suite" is selected because this option will install GCC
on your system.
7. After completing the installation, Code Blocks will prompt you to run it.
8. Click no and then click on the Next button.
Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments
Let us look at a simple code that would print the words "Hello World" −
#include <stdio.h>
int main() {
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
Let us take a look at the various parts of the above program −
The first line of the program #include <stdio.h> is a preprocessor command, which
tells a C compiler to include stdio.h file before going to actual compilation.
The next line int main() is the main function where the program execution begins.
The next line /*...*/ will be ignored by the compiler and it has been put to add
additional comments in the program. So such lines are called comments in the
program.
The next line printf(...) is another function available in C which causes the message
"Hello, World!" to be displayed on the screen.
The next line return 0; terminates the main() function and returns the value 0.
Let us see how to save the source code in a file, and how to compile and run it. Following
are the simple steps −
Open a text editor and add the above-mentioned code.
Save the file as hello.c
Open a command prompt and go to the directory where you have saved the file.
Type gcc hello.c and press enter to compile your code.
If there are no errors in your code, the command prompt will take you to the next line
and would generate a.out executable file.
Now, type a.out to execute your program.
You will see the output "Hello World" printed on the screen.
$ gcc hello.c
$ ./a.out
Hello, World!