Hi,
In the following post, meant for beginners, I'll be guiding you step by step in writing, compiling, and executing a C language Source Code.
So, first thing first, what is a SOURCE CODE???
"In computing, source code is any collection of code, possibly with comments, written using a human-readable programming language, usually as plain text."
-Wikipedia
Basically, What you're gonna type in a computer for making a program is called a Source Code.
Different source code files have their respective extension, to say:
C source code will be '.c' ;
C++ source code will be '.cpp' ;
Python source code will be '.py' ;
.
.
.
etc...
Now, Compilation:
"The name compiler is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language, object code, or machine code) to create an executable program."
-Wikipedia
The process done by the compiler is called Compilation.
Compilers are Language-specific and a Language may have many compilers. Say for example:
C and C++ have GCC (GNU Compiler Collection) and LLVM ;
Python has python3 (Currently Latest Version, Previously it was python only);
Java has javac included with JDK(Java Development Kit from Oracle)
.
.
.
etc...
Finally, Execution:
The GCC compiler will convert our C-source-code into an object file and directly thereafter to a default executable a.out unless specified to any other name.
"The carrying out or putting into effect of a plan, order, or course of action is called Execution."
-Google Dictionary
In Terminal, " . " refers to the present working directory (the folder you're working into) and " / " commands to execute.
To run the default executable type " ./a.out " and to execute any other executable " ./<Name_Of_Executable> " .
Execution is referred as to running a program. (A program is executing means it is running.)
Doing all this in Linux:
Steps:
1. Open a Terminal ;
2. Check GCC Availability (version) ;
3. Open any Text Editor, Type the source code, save, and exit ;
4. Compile by giving the executable a suitable name ;
5. Running/Executing the executable created in the last step.
1. Opening the Terminal:
Method 1:
Press the window button and type 'Terminal', the following is a snippet of the result:
The Terminal
Click on the Terminal icon and terminal will open.
Method 2:
If you're using Ubuntu (viz prescribed by our prestigious SRIIT) then press the key combination ctrl+alt+t to open terminal.
If you're using any other flavor/distro of Linux, set a shortcut for Terminal:
Go to Keyboard Settings
Scroll down to the bottom and click on ' + ' to add a keyboard shortcut
After that enter the shortcut name (here Terminal) command for the shortcut ( here gnome-terminal, it is the command you can type in the Terminal for the same operation that this shortcut will do) and then add the required key combination for the shortcut.
Try pressing the Key-Combination you registered and the Terminal will open.
2. Check the GCC Availability (version) :
In a Terminal type gcc --version there are 2 possibilities:
1. if GCC is installed, the following will print:
2 if GCC ain't installed, the following will appear:
if this appears, you've to install gcc as follow:
type sudo apt-get install gcc
enter your password (It won't show as getting printed) and press enter,
the following process will initiate:
enter Y when it asks to choose [Y/n].
After the process completes, again check the version of gcc with
gcc --version
{ If the process has some problem, feel free to communicate with me along with the screenshot of the Terminal when it gets stuck.}
3. Open any Text Editor, Type the source code, save, and exit :
We prefer Gedit text editor for our purpose of writing source code because it supports a huge number of language-specific highlights.
You may use Vim, Leafpad, or Nano too...
You may use Vim, Leafpad, or Nano too...
For keeping your Programs in one place built a new folder, open it, and right click in its empty space,
then click on Open In Terminal option, the following shall pop up:
Here I've created a folder named "Programming in C" on my Desktop.
Now to write a source code we need a text editor and the name of our source code file, say the name is FirstProgram and this will be a C source code (thus will have a ' .c ' extension) and the editor is Gedit, so the command will be gedit FirstProgram.c and then press Enter, as follows :
The following window shall pop up:
Note: Gedit has a customizable interface, yours may not be as same as that of mine,
but functioning is all the same.
Here you shall type the source code in C language, save and close it (unsaved text have an asterisk before its title.)
1. There's a ' * ' before the title ' FirstProgam.c ' it means the text ain't saved yet.
2. This color of highlights is a specialty of Gedit Text Editor, different language,
accordingly highlightings. (see at bottom of gedit, currently, it's using C highlights.)
After saving the .c file can be seen in the folder, it is the source code of the executable we're going to create.
4. Compile by giving the executable a suitable name:
Now we have a source-code file named FirstProgram.c this is to be compiled to an executable, for C we use the GCC, and I want to name it as SoftwareOfFirstProgram.
The general format of the command is:
gcc -o ExecutableName SorceCodeName.c
Take care of the spaces.
So, the command for us is:
gcc -o SoftwareOfFirstProgram FirstProgram.c
gcc -o ExecutableName SorceCodeName.c
Take care of the spaces.
So, the command for us is:
gcc -o SoftwareOfFirstProgram FirstProgram.c
So, in this case, the source code refers to the executable of our desired name and can be executed with the same name.
If we don't wish to specify the executable name, the GCC has a default output executable: a.out
So if we just enter the command gcc SourceCode.c OR cc SourceCode.c , Then the SourceCode will refer to the executable a.out:
In both the cases, the behavior of the executable is the same, just the name is different. It is advisable to give a specific name to make it easy to identify which executable is for which SourceCode.
5. Running/Executing the executable created in the last step:
Now, we've our SourceCode and its compiled executable so now it's time to run the Executable.
Depending upon the type of compilation command given, you have two ways to execute a source code.
1. Name provided:
If you've used a command of format gcc -o ExecutableName SourceCode.c then for running you need to type ./ExecutableName , We gave the name SoftwareOfFirstProgram , so our command to execute will be ./SoftwareOfFirstProgram :
2. Name Not Provided:
When we don't provide a name to our executable, the source code is by default compiled to a.out viz an executable.
If you used a command of the format gcc SourceCode.c OR cc SourceCode.c then for executing, the command we need to enter is ./a.out :
It Shall be noted that both the outputs are exactly the same, whereas the method of compiling and running is entirely different.
If you have any problem with any of these 5 Steps, feel free to contact me:
MAYANK YADAV
Email: mayankyadavclasses@gmail.com
Don't forget to mention that you're following me on Blogger :-p
Also, follow me on:
LinkedIn: yadavmayank742
Twitter: yadavmayank742














Comments
Post a Comment