CP WEEK1 ASSIGNMENT
CP WEEK1 ASSIGNMENT
Objective: Getting familiar with the programming environment on the computer and
writing the first program.
Suggested Experiments/Activities:
Tutorial1:Problem-solvingusingComputers.
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()
i) Basic Linux environment and its editors like Vi, Vim & Emacs etc.
A basic Linux environment consists of several key components and concepts. Linux is a
versatile and open-source operating system that comes in various distributions (distros),
such as Ubuntu, CentOS, Debian, and Fedora. Here's an overview of the fundamental
elements of a Linux environment:
Kernel: The Linux kernel is the core of the operating system. It manages hardware
resources, provides low-level system services, and interacts with the hardware.
Shell: The shell is the command-line interface that allows you to interact with the Linux
system. Common shells include Bash, Zsh, and Fish. It interprets your commands and
communicates with the kernel to execute them.
File System: Linux uses a hierarchical file system, similar to other Unix-like operating
systems. The root directory ("/") is the top-level directory, and everything in Linux is
organized as a tree structure under it. Common directories include /home (user home
directories), /etc (system configuration files), and /bin (system binaries).
Users and Permissions: Linux is a multi-user operating system, and each user has a unique
username and user ID (UID). File permissions are used to control who can access, modify,
or execute files and directories. Permissions are defined using three groups: owner, group,
and others.
Processes: A process is a running program or task on the system. You can manage and
monitor processes using commands like ps, top, and kill. Processes can run in the
background, and you can start, stop, and manage them.
Package Management: Linux distributions use package managers like APT
(Debian/Ubuntu), YUM (CentOS/Fedora), or DNF (Fedora) to install, update, and remove
software packages. This ensures that software is installed with its dependencies and can be
easily managed.
Networking: Linux provides robust networking capabilities, allowing you to configure
network interfaces, set up firewalls, and manage network services. Common networking
tools include ifconfig, iptables, and netstat.
Text Editors: Linux has several text editors, including Vim, Nano, and Emacs, which are
used for editing configuration files and scripts.
File Permissions: Linux uses a permission system that defines who can read, write, and
execute files and directories. You can set file permissions using the chmod command.
File and Directory Operations: Linux offers various commands for managing files and
directories, such as ls (list files), cp (copy), mv (move), rm (remove), and mkdir (create
directory).
System Services: Linux has a variety of system services and daemons that run in the
background to provide core functionality. You can manage these services using tools like
systemd, service, or systemctl.
Text Processing: Linux provides powerful text processing tools like grep, sed, and awk
for searching, manipulating, and extracting information from text files.
User Management: You can create, modify, and delete user accounts and groups using
commands like useradd, usermod, and userdel. Passwords can be managed using passwd.
Remote Access: Linux systems can be accessed remotely using tools like SSH (Secure
Shell) for secure access and SCP (Secure Copy) for secure file transfers.
These are some of the fundamental elements and concepts of a basic Linux environment.
The exact commands and tools may vary depending on the specific Linux distribution you
are using, but the core principles remain consistent across most distributions.
Vi:
Vi is a powerful and widely used text editor on Linux systems. It has a steep learning
curve, but once you get the hang of it, it can be a very efficient tool. Here are some basic
commands and tips to help you work with Vi:
Opening a File: To open a file with Vi, open your terminal and type:
Copy code
vi filename
If the file doesn't exist, Vi will create a new one.
Modes:
Normal Mode: The default mode for navigation and manipulation.
Insert Mode: For inserting and editing text.
Command-Line Mode: For saving, quitting, and performing other operations.
Switching Between Modes:
Press i to enter Insert Mode (insert text before the cursor).
Press a to enter Insert Mode (insert text after the cursor).
Press Esc to exit Insert Mode and return to Normal Mode.
Navigation:
Use the arrow keys or h, j, k, and l in Normal Mode for left, down, up, and right
respectively.
Move by words: w (next word), b (previous word).
Move to the beginning of the line: 0.
Move to the end of the line: $.
Move to the start of the document: gg.
Move to the end of the document: G.
Editing:
Delete a character: x.
Delete a line: dd.
Undo the last change: u.
Redo the last undone change: Ctrl + r.
Copying and Pasting:
Copy (yank) a line: yy.
Paste after the cursor: p.
Paste before the cursor: P.
Search and Replace:
Search for a word: /searchterm. Use n to go to the next occurrence.
Search and replace: :%s/old/new/g to replace all occurrences of "old" with "new".
Remove the g to replace only the first occurrence.
Saving and Quitting:
Save changes and stay in Vi: :w.
Quit Vi without saving: :q.
Save and quit: :wq or :x.
Other Useful Commands:
:q! - Quit Vi without saving (force quit).
:e! - Revert changes to the last saved version.
:set number - Display line numbers.
:set nonumber - Hide line numbers.
Multiple Files:
To edit multiple files in Vi, open Vi with vi followed by the file names, e.g., vi file1.txt
file2.txt.
Use :n to switch to the next file and :N to switch to the previous file.
Remember that Vi can be challenging at first, but practice is the key to mastering it. If you
find it too difficult, you can use simpler text editors like Nano or graphical editors like
Gedit, which have more user-friendly interfaces.
Emacs:-
Emacs is a highly extensible and powerful text editor with a learning curve but a vast array
of features and capabilities. Here's a basic guide on how to work with Emacs:
1. Starting Emacs: To start Emacs, open your terminal and type:
bashCopy code
emacs
2. Basic Navigation:
Use the arrow keys or C-p (up), C-n (down), C-b (left), and C-f (right) to navigate.
Move to the beginning of the line: C-a.
Move to the end of the line: C-e.
Move to the beginning of the buffer: M-<.
Move to the end of the buffer: M->.
3. Modes: Emacs has various modes, each designed for specific tasks like text editing,
programming, and more. The most common modes are:
Text Mode: For regular text editing.
Fundamental Mode: A basic mode that provides standard text editing features.
Programming Modes: Such as Python Mode, C Mode, etc., for code editing.
4. Basic Editing:
To enter text, simply start typing in the buffer.
Cut (kill) a line: C-k.
Copy (kill-ring save) a line: M-w.
Paste (yank) the last killed text: C-y.
5. Search and Replace:
Search for a string: C-s (incremental search). Press C-s again to search for the next
occurrence.
Search and replace: M-% (query-replace).
6. Saving and Quitting:
Save the current buffer: C-x C-s.
Save all buffers: C-x s.
Quit Emacs: C-x C-c.
7. Multiple Buffers and Windows:
Open a new buffer: C-x C-f and specify the file name.
Switch between buffers: C-x b and type the buffer name.
Split the window horizontally: C-x 2.
Split the window vertically: C-x 3.
Move between windows: C-x o.
8. Help:
Access built-in help: C-h.
Get help on a specific function or keybinding: C-h k followed by the key combination.
Get help on a topic: C-h i to open the info viewer.
9. Customization:
Emacs is highly customizable. You can create or modify customizations in your ~/.emacs
or ~/.emacs.d/init.el file. Add custom functions, keybindings, and more.
10. Extensions and Packages: Emacs has a vast ecosystem of packages and extensions
that enhance its functionality. You can use the package manager, like M-x package-list-
packages, to install and manage packages.
11. Advanced Features:
Emacs has many advanced features, including version control integration (Git, SVN),
integrated terminals, an email client, and more. Explore these features as you become more
comfortable with the basics.
Emacs is a highly extensible editor, and its real power comes from the ability to customize
and extend it to suit your specific needs. It has a rich ecosystem of packages and resources
available to help you get the most out of it.
TurboC:-
TurboC is an integrated development environment (IDE) and compiler for the C and C++
programming languages. It was widely used in the past but is now quite outdated. If you
still want to work with TurboC, you can follow these basic steps:
1. Installation:
You will need to install TurboC on your system. Keep in mind that TurboC is quite old and
may not be compatible with modern operating systems. It's best to use a virtual machine or
an older system for this purpose.
2. Creating a New Project:
In TurboC, you typically work on projects. To create a new project:
Open TurboC.
Click on "File" and select "New."
Choose "Project" and give your project a name.
Select the project type (C or C++).
Choose where you want to save your project.
3. Writing Code:
Once you have a project, you can start writing code:
Click on "File" and select "New" to open a new source file.
Write your C or C++ code in the source file.
Save your code by clicking "File" and selecting "Save."
4. Compiling and Running:
To compile and run your code:
Click on "Run" and select "Compile" to compile your code.
Check the compiler output for any errors.
If there are no errors, click on "Run" and select "Run" to execute your program.
5. Debugging:
TurboC provides basic debugging features:
Use "Run" and "Debug" to start debugging your program.
You can set breakpoints and step through your code.
6. Saving and Closing:
Save your work by clicking "File" and selecting "Save."
When you're done, click "File" and select "Exit" to close TurboC.
Please note that TurboC is quite old and not recommended for modern development. It
may not work correctly on modern operating systems, and there are many better
alternatives available. If you are learning C or C++, consider using a more modern
development environment, such as Visual Studio Code, CLion, or Code::Blocks, which
provide better features, compatibility, and support for the C and C++ languages.
GCC:-
GCC (GNU Compiler Collection) is a widely used open-source compiler suite for various
programming languages, including C, C++, and more. Here's how to work with GCC to
compile and run C/C++ programs:
1.Installation: Before you can use GCC, ensure that it's installed on your system. If it's not
already installed, you can typically install it using your system's package manager. For
example, on Ubuntu, you can run:
bashCopy code
sudo apt install build-essential
On Fedora:
bashCopy code
sudo dnf install gcc-c++
2. Writing Code:
Create your C or C++ source code using a text editor of your choice (e.g., my_program.c
for C or my_program.cpp for C++).
3. Compiling C Programs:
To compile a C program using GCC, open your terminal and navigate to the directory
containing your source code:
bashCopy code
gcc -o output_file input_file.c
-o output_file: This option specifies the name of the output executable file.
input_file.c: This is the name of your C source code file.
For example, to compile my_program.c:
bashCopy code
gcc -o my_program my_program.c
4. Compiling C++ Programs:
For C++ programs, use the g++ command:
bashCopy code
g++ -o output_file input_file.cpp
For example, to compile my_program.cpp:
bashCopy code
g++ -o my_program my_program.cpp
5. Running Your Program:
After successfully compiling your program, you can run it:
bashCopy code
./output_file
For example:
bashCopy code
./my_program
6. Command-Line Options:
GCC offers a variety of command-line options that you can use to customize the
compilation process. For instance, you can use -Wall to enable warnings and -g to include
debugging information in the executable. These options can help you catch errors and
debug your program more effectively.
7. More Complex Projects:
For more complex projects involving multiple source files, you can use Makefiles or other
build systems to manage the compilation process.
8. Debugging:
If you want to debug your program, you can use the GNU Debugger (GDB), which is a
separate tool. To debug a program, compile it with debugging symbols using the -g flag
and then run it with GDB:
bashCopy code
g++ -g -o my_program my_program.cpp gdb my_program
GDB provides various commands for debugging, such as setting breakpoints, inspecting
variables, and stepping through your code.
GCC is a powerful compiler tool with many features, and this guide covers only the basics.
Depending on your specific needs, you may want to explore more advanced options and
build systems to manage larger projects efficiently.
Simple C Programs
Format Specifiers for I/O
As you can see from the above examples, we use
%d for int
%f for float
%lf for double
%c for char
Here's a list of commonly used C data types and their format specifiers.
Data Type Format Specifier
int %d
char %c
float %f
double %lf
unsigned int %u
unsigned
%lu
long int
unsigned
%llu
long long int
signed char %c
Data Type Format Specifier
unsigned char %c
C Output
In C programming, printf() is one of the main output function. The function sends formatted
output to the screen. For example,
Example 1: C Output
#include <stdio.h>
int main()
{
// Displays the string inside quotations
printf("C Programming");
return 0;
}
C Input
In C programming, scanf() is one of the commonly used function to take input from the user.
The scanf() function reads formatted input from the standard input such as keyboards.