0% found this document useful (0 votes)
66 views

C Lab Manual

The document provides information about getting familiar with the basic Linux programming environment in the first week. It discusses learning Linux text editors like Vi, Vim, and Emacs. It also covers getting exposure to Turbo C and GCC compilers by writing simple programs using functions like printf() and scanf(). It then gives detailed instructions on how to install and use Turbo C to compile and run a simple C program.

Uploaded by

Bhagya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

C Lab Manual

The document provides information about getting familiar with the basic Linux programming environment in the first week. It discusses learning Linux text editors like Vi, Vim, and Emacs. It also covers getting exposure to Turbo C and GCC compilers by writing simple programs using functions like printf() and scanf(). It then gives detailed instructions on how to install and use Turbo C to compile and run a simple C program.

Uploaded by

Bhagya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Week-1

Fmiliarizing with programming environment

Aim : getting familiar with the

Basic linux programming environment its editors like Vi , Vim & Emacs .

Exposure to Turbo C ,gcc

Writing simple programs using printf() ,scanf()

i)Basic linux programming environment its editors like Vi , Vim & Emacs .

Linux Text Editors


Linux text editors can be used for editing text files, writing codes, updating user
instruction files, and more. A Linux system supports multiple text editors. There are two
types of text editors in Linux, which are given below:

o Command-line text editors such as Vi, nano,vim, pico, and more.


o GUI text editors such as gedit (for Gnome), Kwrite, and more.

o A text editor plays an important role while coding. So, it is important to select the
best text editor. A text editor should not only be simple but also functional and
should be good to work with.

o A text editor with IDE features is considered as a good text editor.

1.Vi/VIM editor
Vim editor is one of the most used and powerful command-line based editor of the Linux
system. By default, it is supported by most Linux distros. It has enhanced functionalities of
the old Unix Vi editor. It is a user-friendly editor and provides the same environment for all
the Linux distros. It is also termed as programmer's editor because most programmers
prefer Vi editor.

Vi editor has some special features such as Vi modes and syntax highlighting that makes it
powerful than other text editors. Generally, it has two modes:
Command Mode: The command mode allows us to perform actions on files. By default, it
starts in command mode. In this mode, all types of words are considered as commands.
We can execute commands in this mode.

Insert Mode: The insert mode allows to insert text on files. To switch from command mode
to insert mode, press the Esc key to exit from active mode and 'i' key.

By default, the vi editor starts in command mode. To enter text, you have to be in insert
mode, just type 'i' and you'll be in insert mode. Although, after typing i nothing will appear
on the screen but you'll be in insert mode. Now you can type anything.

To exit from insert mode press Esc key, you'll be directed to command mode.

If you are not sure which mode you are in, press Esc key twice and you'll be in command
mode.

Using vi
The vi editor tool is an interactive tool as it displays changes made in the file on the screen
while you edit the file.

In vi editor you can insert, edit or remove a word as cursor moves throughout the file.

Commands are specified for each function like to delete it's x or dd.

The vi editor is case-sensitive. For example, p allows you to paste after the current line
while P allows you to paste before the current line.

vi syntax:

1. vi <fileName>

In the terminal when you'll type vi command with a file name, the terminal will get clear
and content of the file will be displayed. If there is no such file, then a new file will be
created and once completed file will be saved with the mentioned file name.

Linux vi example
Let's understand vi through an example:
To start vi open your terminal and type vi command followed by file name. If your file is in
some other directory, you can specify the file path. And if in case, your file doesn't exist, it
will create a new file with the specified name at the given location.

Example:

1. vi /home/sssit/Downloads/file.txt

Look at the above snapshot, we are creating a new file file.txt (as this file doesn't exist) and
have entered the full path for the directory Downloads.

Command mode
This is what you'll see when you'll press enter after the above command. If you'll start
typing, nothing will appear as you are in command mode. By default vi opens in command
mode.

Look at the above snapshot, it is blank as it is a new file. To start typing, you have to move
to the insert mode. At the end of the terminal window, directory name and file name are
displayed.
Insert mode
To move to the insert mode press i. Although, there are other commands also to move to
insert mode which we'll study in next page.

Look at the above snapshot, after pressing i we have entered into insert mode. Now we can
write anything. To move to the next line press enter.

Once you have done with your typing, press esc key to return to the command mode.

To save and quit


You can save and quit vi editor from command mode. Before writing save or quit
command you have to press colon (:). Colon allows you to give instructions to vi.

exit vi table:

Commands Action

:wq Save and quit

:w Save
:q Quit

:w fname Save as fname

ZZ Save and quit

:q! Quit discarding changes made

:w! Save (and write to non-writable file)

To exit from vi, first ensure that you are in command mode. Now, type :wq and press enter.
It will save and quit vi.

Type :wq to save and exit the file.

Look at the above snapshot, command :wq will save and quit the vi editor. When you'll
type it in command mode, it will automatically come at bottom left corner.

If you want to quit without saving the file, use :q. This command will only work when you
have not made any changes in the file.
Look at the above snapshot, this file is modified and hence on typing :q it displays this
message at bottom left corner.

The above file can be saved with the command :!q. It discards the changes made in the file
and save it.

Look at the above snapshot, we have typed :!q, it will save our file by discarding the
changes made.

To switch from command to insert mode:


Command Action

i Start typing before the current character


I Start typing at the start of current line

a Start typing after the current character

A Start typing at the end of current line

o Start typing on a new line after the current line

O Start typing on a new line before the current line

To move around a file:


Commands Action

j To move down

k To move up

h To move left

l To move right

To jump lines:
Commands Action

G Will direct you at the last line of the file

`` Will direct you to your last position in the file


To delete:
Commands Action

X Delete the current character

X Delete the character before the cursor

R Replace the current character

Xp Switch two characters

Dd Delete the current line

D Delete the current line from current character to the end of the line

dG delete from the current line to the end of the file

ii) start with Turbo C and GCC

Install Turbo C in your computer’s C Drive and follow the below shown commands to open it.

METHOD 1: Using command prompt.


Step1: Win + R ; will open a run command prompt.
Step2: Type cmd and hit Enter ; will open command prompt.
Step3: Now type cd\ and hit Enter (will takes you to root directory i.e C drive)
Step4: Type cd TC and hit Enter (Now you are in TC folder)
Step5: Type cd bin and hit Enter ( Now you are redirected to bin folder, a subfolder in tc)
Step6: Type TC and hit Enter (will open TC window now you can write edit and compile your programs,
through this command you are accessing an application called TC.exe )
Image: how_to_TC

METHOD 2: Navigating through folders.


Step1: Go to C Drive
Step2: Open TC folder
Step3: Open BIN folder
Step4: Double click on TC.exe
We are doing pretty much same in both the methods :).

How_to_TC2-1

Compile and Run C Program


To compile and run a C language program, you need a C compiler. A compiler is software
that is used to compile and execute programs. To set up a C language compiler on your
Computer/laptop, there are two ways:

1. Download a full-fledged IDE like Turbo C++ or Microsoft Visual C++ or DevC++,
which comes along with a C language compiler.

Or, you can use any text editor to edit the program files and download the C compiler
separately and then run the C program using the command line
Using an IDE - Turbo C

We will recommend you to use Turbo C or Turbo C++ IDE, which is the oldest IDE for C
programming. It is freely available over the Internet and is good for a beginner.

Step 1: Open turbo C IDE(Integrated Development Environment), click on File, and then
click on New

Step 2: Write a Hello World program that we created in the previous article - C Hello
World program.
Step 3: Click on Compile menu and then on Compile option, or press the keys and
press Alt + F9 to compile the code.
Step 4: Click on Run or press Ctrl + F9 to run the code. Yes, C programs are first compiled
to generate the object code and then that object code is Run.
Step 5: Output is here.
Run C Program Without using any IDE

 If you do not wish to set up an IDE and prefer the old-school way, then download
the C compiler which is called gcc from the GCC website https://gcc.gnu.org/install/

 Once you have downloaded and installed the gcc compiler, all you have to do
is, open any text editor, copy and paste the C program code for C Hello World
Program, and save it with the name the helloworld.c like any other file you save
with a name.

 Now, Open the Command prompt or Terminal(if you use Ubuntu or Mac OS), and
go to the directory where you have saved the helloworld.c program file.

 Type the command gcc hello.c to compile the code. This will compile the code, and if
there are no errors then it will produce an output file with name a.out(default name)

 Now, to run the program, type in ./a.out and you will see Hello, World displayed on
your screen.

$ gcc hello.c

$ ./a.out

Copy

Hello, World

Difference between Compile and Run in C?

You must be thinking about why it is a 2 step process, first, we compile the code and then
we run the code. We did the same thing with Turbo C and the same is with the command
line or Terminal too.

 Well, the compilation is the process where the compiler checks whether
the program is correct syntax-wise and whether there are no errors in the syntax,
and if the code is fine converts the C language source code into machine-
understandable object code.
 When we run a compiled program, it is just the already compiled code that is run.

 This difference is clear when we run a C program using the command line.

 When you compile the code, a .out file is generated, which is then Run to execute
the program.

 Once a .out file is generated, and then you make any changes to your program in
the source code file, you will have to again compile the code, otherwise, the .out
file will have the old source code and will keep running the old program itself.

iii) printf() and scanf() in C


The printf() and scanf() functions are used for input and output in C language. Both
functions are inbuilt library functions, defined in stdio.h (header file).

printf() function
The printf() function is used for output. It prints the given statement to the console.

The syntax of printf() function is given below:

printf("format string",argument_list);

The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

scanf() function
The scanf() function is used for input. It reads the input data from the console.

scanf("format string",argument_list);

#include<stdio.h>
int main(){
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}

Output
enter a number:5
cube of number is:125
conclusion:
Hence we familiared with the programming environments like linux
And turboc ,gcc.

You might also like