Let’s begin programming in C Programming language. first of all we are going to install the compiler on three different operating systems so that whichever operating systems you are using, you can compile and run your program.
We divide the compiler for 3 operating systems.
- Windows
- Linux
- Mac
Installing C Compiler on Windows
Above image is screenshot from https://www.bloodshed.net/
In the above image, Dev C++ Binary Link is given. It is for Windows 98, NT, 2000 and XP, However it should work fine on latest Windows as well. You should download the installation binary and double click on it. This will invoke wizard and next button with parameters it needs. You have to supply path where you want to install the program and click next button until you are done with the installation. After installation, you can find icon on desktop or in start menu > Programs. After finding Icon for the program, click on it and this will incoke Dev C++ Gui Interface as follows.
In the above IDE, you can create, edit, compile and run the C Programs.
Installing C Compiler on Linux
In Ubuntu search for Terminal. Or Press, CTRL + ALT + T.
This will invoke the terminal and now we fire following command.
sudo apt-get install gcc
This will install gcc compiler on linux.
By default Linux has GCC Compiler as its default C Compiler. We are demonstrating installation on Ubuntu Command line. (Different Linuxes have different packages.
Installing clang on Mac
Step:1 – Open Mac Terminal (Reference: https://support.apple.com/guide/terminal/open-or-quit-terminal-apd5265185d-f365-44cb-8b09-71a064a42125/mac)
Step:2 – Run following Command on Terminal to install command line developer tools on Mac
$ xcode -select --install
$ clang --version
To compile the C program, we need to enter in directory in terminal and run following command.
$ cd learnc
$ clang -o hello.o hello.c (provided that we have already created hello.c file using text editor or using terminal assuming you know the terminal commands to edit and save the files. If you do not know terminal please visit our vim course or read working with files in linux.)
First C Program
hello.c
#include<stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
Now we examine hello.c program and make our mind clear about writing C programs.
C Programs consists of main function. This is the beginning of any C Program. But you will ask I see some other syntax before main. So how main is the beginning of the program?
It is a good question. main() function is the one which compiler always looks for and starts executing program. Inside main function we can call other functions like printf(“text here”);
When we call printf, this function is defined already in C System Library, called stdio.h and this is where all code for printf is written. C Compiler is providing us usability so that we don’t have to write code again and again, C has its own library and set of functions. This functions we can call anytime to reuse the code of C Library, but we need to know which function is available in which library.
So If we want to write any C Program, we need to know folllowing sequence.
- Don’t start including library right from the beginning. Because we should start writing main function first. And then we can go ahead and include libraries as we make use of them calling some functions from inbuilt libraries.
- Write main as following.
- main() { code goes here }
- Write return statement with semi-colon at the end. Remember that every statement has an end in C language and to show to C Compiler, we have finished writing one line of the statement, it is ended with semi-colon symbol which is in single inverted comma looks like this ‘ ; ‘
- Return statement is written to provide return code to the operating system. If our program executes successfully and everything has run at the end of the code 0 (zero) is returned to the operating system to let operating system know that program has executed successfully. If program has not executed successfully, compiler will send non-zero code to operating system and this will determine that program has some issue.
How to write C Program?
We repeat our process now.
step1: write main function.
main()
{
}
In the above step, we have created smallest C Program in the world.
Step:2 – write ‘int’ in front of ‘main()’ function to return an integer to Operating system.
Now, as we discussed every C Program returns integer to the operating system. (Zero or Non-zero integer) and this integer is handed over by main function. So we should write main function as follows with return statement.
int main()
{
return 0;
}
Step:3 – add printf function in main. But printf function is defined somewhere else (in the system library). So we have to include it in our program.
#include<stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
In the above program, printf(“Hello World!”); statement has semi-colon at the end. (we already discussed above). And this function is coming from stdio.h header file. So we have included it on top of the hello.c file as above. And above code is complete hello.c file. And it is our first C Program.
Now we see how to compile and run the C Program.
Compiling and Running Program in Linux
In Ubuntu Linux or any other Linux, we run following command.
$ gcc -o hello.o hello.c ( this will compile and create an output file hello.o)
$ ./hello.o ( this will run the program)
Output: Hello World!
Compiling and Running Program in Mac
In Mac terminal we run following commands to compile and run hello.c
$ clang -o hello.o hello.c (this will compile hello.c file and produce output executable file hello.o)
$ ./hello.o
Output: Hello World!
Compiling and Running Program in Windows.
In Dev C++ We installed on Windows, its an IDE (Integrated Development Environment). It has all the functionality of Editing the Source file (hello.c file in our case) and creating hello.exe (executable) file.
Conclusion
In the above article, we have installed C Compiler for Windows, Linux and Mac. We also created basic C Program to Print Hello World on Screen. After creating hello.c program we compiled it and ran it successfully.
Skills acquired: Installing C Compiler, Compiling C Program, Running C Program.