100% found this document useful (2 votes)
655 views9 pages

CPL Week1

This document provides an overview of Week 1 activities for a programming course, which include: 1) Getting familiar with the programming environment on computers and writing a first program. 2) Suggested experiments involve a tutorial on problem-solving using computers and a lab on familiarization with programming environments, including basic Linux environment/editors, exposure to C/C++ compilers, and writing simple programs using print and scanf functions. 3) Detailed information is provided on basic commands for the vi text editor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
655 views9 pages

CPL Week1

This document provides an overview of Week 1 activities for a programming course, which include: 1) Getting familiar with the programming environment on computers and writing a first program. 2) Suggested experiments involve a tutorial on problem-solving using computers and a lab on familiarization with programming environments, including basic Linux environment/editors, exposure to C/C++ compilers, and writing simple programs using print and scanf functions. 3) Detailed information is provided on basic commands for the vi text editor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

WEEK 1

WEEK 1
Objective: Getting familiar with the programming environment on the computer and writing the
first program.
Suggested Experiments/Activities:
Tutorial 1: Problem-solving using Computers.
Lab1: Familiarization with programming environment
i Basic Linux environment and its editors like Vi, Vim & Emacs etc.
ii Exposure to Turbo C, gcc
iii Writing simple programs using printf(), scanf()

Date:
i) Basic Linux environment and its editors like Vi, Vim & Emacs etc.
Emacs and Vim
Introduction
You've probably never heard of emacs or Vim, especially if you're just starting out your career
in software engineering. It is also normal that as a windows Operating System user, you've only
been working with the notepad, notepad++, sublime text, visual studio code, and all other
common text editors.In the Linux operating system, Emacs and vi are the major editors that are
used to manipulate files.
Emacs
Emacs (Editor Macros) was developed by David A Moon in 1976 making it one of the oldest
pieces of software which exists today. GNU Emacs, the most used variant(fork) of Emacs, was
released in 1985 by Richard Stallman, the founder of GNU/Linux.
Emacs is a highly advanced, extensible, and customizable text editor that also offers an
interpretation of the Lisp programming language at its core. Different extensions can be added to
support text editing functionalities.
Emacs has the following features:
 User documentation and tutorials
 Syntax highlighting using colors even for plain text.
 Unicode supports many natural languages.
 Various extensions including mail and news, debugger interface, calendar, and many
more.
Vi(m)
Vim (Vi Improved), as the name suggests is an improved clone of the text editor. The text editor
was originally developed by Billy Joy in 1976 for the proprietary Unix operating system. Bram
Moolenar later enhanced vi and released it as vim in 1991.
Vim is a powerful command-line-based text editor that has enhanced the functionalities of the
old Unix Vi text editor. It is one the most popular and widely used text editors among System
Administrators and programmers that is why many users often refer to it as a programmer’s
editor. It enables syntax highlighting when writing code or editing configuration files.
The UNIX vi editor is a full-screen editor and has two modes of operation:
1. Command mode commands which cause action to be taken on the file, and
2. Insert mode in which entered text is inserted into the file. In the command mode, every
character typed is a command that does something to the text file being edited; a
character typed in the command mode may even cause the vi editor to enter the insert
mode.
In the insert mode, every character typed is added to the text in the file; pressing
the (Escape) key turns off the Insert mode.
While there are several vi commands, just a handful of these is usually sufficient for
beginning vi users.
A good example is Vi iMproved, also known as Vim, which gradually became more
popular than Vi as it added more features and improvements that are not found in Vi.
While both Vim and Emacs support plugins that enhance their functionality, Emacs supports a
lot more customization of the editor environment. This is arguably the most important feature of
emacs and is responsible for much of emacs' devoted following.
Emacs can be extended on the fly by redefining built-in elisp functions, either by typing
the new definition into Emacs or by loading elisp files. Groups of related changes are called
"modes" and can be easily configured to be automatically used for particular types of files
(buffers). So it is easy to define modes for different programming languages or frameworks such
as "lisp mode" or "Ruby on Rails mode" or "PHP mode". These modes can directly modify even
core behaviors of Emacs, automatically format or colorize text and add standard template or
"boilerplate" text such as function declarations and closures.
Programmers find Emacs to be far more customizable to their particular requirements than
Vim. Yet some programmers may find Vim to ultimately be better suited to their programming
environment due to its equally customizable and fully modal operation.
Emacs can be extended in elisp, while Vim has its own internal scripting language and
supports the use of other programming languages for plugin development.
While Emacs can be extended in elisp, Vim has its own internal scripting language and supports
the use of other programming languages for plugin development can also be extended on the fly
by using the command mode. The command mode allows configuration options to be set,
functions to be defined, and macros to be made. Configuration files for Vim are just commands
that can be input through the command mode.
1. Graphical User Interface (GUI)
Both Vim and Emacs have graphical user interfaces. Virtually all menu items on the graphical
interface for both editors are simply ways to handle a shortcut command or quick configuration.
The GUIs for the editors do not provide virtually any additional functions beyond those available
in the CLI (command-line interface).Emacs uses XDisplay or gtk2 for its GUI. Vim can use
many other GUI libraries, such as gtk, gnome, gnome2, motif, athena, and neXtaw, in addition to
gtk2.
1. Vi is lighter and faster than Emacs.
2. Emacs is more customizable than Vi.
3. Emacs can emulate Vi but not the other way around.
4. Emacs later developed a GUI while Vi did not.
5. Emacs continued developing while Vi was succeeded by its variants.
BASIC VI EDITOR COMMANDS
To start vi:
Example: > vi letter will open a new file called letter to edit, or if letter already exits, open the
exiting file.
Command Effect

vi filename edit filename starting at line 1

vi +n filename edit filename beginning at line n

vi +filename edit filename beginning at the last line

vi -r filename recover filename after a system crash

vi +/patter filename edit filename starting at the first line containing pattern

Command Mode vs. Insert Mode


Insert mode is the mode to be in when inserting text into the file. Command mode is the mode to
be in when giving commands which will move the cursor, delete text, copy and paste, save the
file etc.
When entering a file, vi is in command mode. To enter text, you must enter insert mode. If in
insert mode, enter command mode by hitting the escape, <esc>, key.
To insert text:
Command Insert Text

i before cursor

a after cursor

A at the end of the line

o open a line below the current line

O open a line above the current line

r replace the current character

R replace characters until <ESC>, overwrite

To move the cursor:


You must be in Command Mode to use commands that move the cursor. Each of these
commands can be preceded with a Repeat Factor.
Examples:
8j will move the cursor down 8 lines
3w will move the cursor 3 words to the right.
Command Moves the cursor

SPACE, l (el), or right arrow space to the right


h or left arrow space to the left

j or down arrow down one line

k or up arrow up one line

w word to the right

b word to the left

$ end of the line

0 (zero) beginning of the line

e end of the word to the right

- beginning of previous line

) end of the sentence

( beginning of the sentence

} end of paragraph

{ beginning of paragraph

To Delete Text:
The d command removes text from the Work Buffer. The amount removed depends on the
Repeat Factor and the Unit of Measure you enter after d. If you delete by mistake: give the
command u (undo) immediately after you give the delete command.
Examples: 3dd will delete 3 lines beginning with the current line.
3dw or d3w will delete 3 words
Command Action

d0 delete to beginning of line

dw delete to end of word

d3w delete to end of third word

db delete to beginning of word


dW delete to end of blank delimited word

dB delete to beginning of blank delimited word

dd delete current line

5dd delete 5 lines starting with the current line

dL delete through the last line on the screen

dH delete through the first line on the screen

d) delete through the end of the sentence

d( delete through the beginning of the sentence

x delete the current character

nx delete the number of characters specified by n.

nX delete n characters before the current character

Viewing Different Parts of the Work Buffer:


^Character means that you should hold down the Control key while striking the indicated
character key.
Command Moves the cursor

^D forward one-half screenful

^U backward one-half screenful

^F forward one screenful

^B backward one screenful

nG to line n (Ex: 25G moves the cursor to line #25)

H to the top of the screen

M to the middle of the screen

L to the bottom of the screen


^L refresh the screen

Yanking (copy) and Putting (paste) Text:


Example: 3yy will yank (copy) 3 lines
p will put the 3 lines just yanked on the line below the current cursor.
In the following list M is a Unit of Measure that you can precede with a Repeat Factor, n.
Command Effect

yM yank text specified by M

y3w yank 3 words

nyy yank n lines

Y yank to the end of the line

P put text above current line

p put text below current line

Changing Text
Example: cw allows you to change a word. The word may be replaced by as many word as
needed. Stop the change by hitting < esc &gt.
c3w allows you to change 3 words.
Ending an Editing Session
Command Effect

:w writes the contents of the work buffer to the file

:q quit

:q! quit without saving changes

ZZ save and quit

:wq save and quit

:w filename saves to filename (allows you to change the name of the file)

Miscellaneous commands
Command Effect

J join the current line and the following line


:set number number the lines on the screen (not actually added to file)

:set nonumber turns off numbering of lines

:r filename reads filename into the current file at the location of the cursor

:set showmode displays INPUT MODE at the lower right hand corner of screen

~ change uppercase to lowercase and vice-versa

Searching for an expression


When in command mode if you enter / the cursor will go to the bottom of the screen.
Follow this with an expression and vi will find the next occurrence of that expression in the
file. Example: /Bill will find the next occurrence of Bill.

ii. Exposure to Turbo C, gcc


Turbo C:
It was a software development tool for writing programs in the C language. As an IDE,
it included a source code editor, a fast compiler, a linker and an offline help file for reference.
Version 2 included a built-in debugger. Turbo C was a follow-up product to Borland's Turbo
Pascal, which had gained widespread use in educational institutions because the
Pascal language was suited for teaching programming to students.
Although Turbo C was initially developed by a different company, it shared a lot of
features with Turbo Pascal, namely, the look-and-feel of the interface and the
various programming and debugging tools included.
However, it was not as successful as Turbo Pascal because of competition from other
C products such as Microsoft C, Watcom C, Lattice C, etc. Nevertheless, Turbo C still had the
advantage in compile speed and price.
The first version was released on May 13, 1987, and it offered the first-ever edit-compile-
run environment for software development on IBM PCs. Turbo C was not originally developed
by Borland but was bought from Bob Jervis and was initially called Wizard C. Turbo Pascal did
not have pull-down menus before this time, and it was only on its fourth version that it received a
face lift to look like Turbo C.
Borland as a company no longer develops and sells these products, but Turbo C still lives
on as a free download from various online repositories, although it is really an old technology
without real technical support and is no longer viable for modern software development. Turbo C
eventually evolved into Turbo C++, then into Borland C++ and, finally, into C++ Builder.
Turbo C features: Inline assembly with full access to the C language symbolic structures and
names – This allowed programmers to write some assembly language codes right into their
programs without the need for a separate assembler. Support for all memory models -- This had
to do with the segmented memory architecture used by 16-bit processors of that era, where each
segment was limited to 64 kilobytes (Kb).The models were called tiny, small, medium, large and
huge, which determined the size of the data used by a program, as well as the size of the program
itself. For example, with the tiny model, both the data and the program must fit within a single
64-Kb segment. In the small model, the data and the program each used a different 64- Kb
segment. So in order to create a program larger than 64 Kb or one that manipulates data larger
than 64 Kb, the medium, large and huge memory models had to be used. In contrast, 32-bit
processors used a flat memory model and did not have this limitation. Speed or size optimization
-- The compiler could be configured to produce an executable program that was either fast or
small in size, but not both. Constant folding -- This feature allowed the Turbo C compiler to
evaluate constant expressions during compile time rather than during run time.

Short cut Keys in Turbo C:

F1 For Help,F2 Save,F3 Open,F4 Go to cursor,F5 Zoom,F6 Next,F7 Trace into,F8 Step over,F9
Make,F10 Menu,Alt+X Quit,Alt+Bksp Undo,Shift+Alt+Bksp Redo,Shift+Del Cut,Ctrl+Ins
Copy,Shift+Ins Paste,Ctrl+Del Clear,Ctrl+L Search again,Alt+F7 Previous error,Alt+F8 Next
error,Ctrl+F9 Run,Ctrl+F2 Program reset,Alt+F9 Compile,Alt+F4 Inspect,
ctrl+F4 Evaluate/Modify,Ctrl+F3 Call stack,Ctrl+F8 Toggle breakpoint,Ctrl+F5
Size/Move,Alt+F3 Close,Alt+F5 User screen,Alt+0 List all,Shift+F1 Index,Ctrl+F1 Topic
search,Alt+F1 Previous topic,Ctrl+F7 Add watch

GCC: The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project
supporting various programming languages.GCC is a key component of the GNU tool chainand
the standard compilerfor most projects related to GNU and Linux, the most notable is the Linux
kernel. The Free Software Foundation (FSF) distributes GCC under theGNU General Public
License(GNUGPL). GCC has played animportant role in the growth offree software, as both a
tool and an example.When it was first released in 1987, GCC 1.0 was named the GNU C
Compiler since it onlyhandled the C programming language . It was extended tocompile C++ in
December of that year. Front ends/w were later developed for Objective-C

Creating / Editing / Compiling and Executing C Programs in Linux:Creating:


i. 1. Open any Text Editor like Vi/Emacs/Gedit from terminalEx: vi filename.cii. Enter
the program in the file.iii. Save the file: Go Last Line Mode (Esc + Shift :), type wq

Compiling:Method 1:

$ gcc filename.c

If No syntactical errors it gives $ prompt. If any syntactical errors are there in the program, it
displays the errors. Correct the errors by opening the file using vi filename.c

Method 2:

$gcc –o filename filename.c

If no syntactical errors are there in the program,It displays the errors.Correct the errors by
opening the file using vi filename.c

Executing:

For Methods 1:

$./a.out

For Methods 2:

$./filename

You might also like