0% found this document useful (0 votes)
3 views11 pages

Computer Programming Lab

The document outlines a computer programming lab focused on familiarizing students with the Linux environment, including editors like Vi, Vim, and Emacs, as well as compilers Turbo C and GCC. It provides detailed explanations of basic Linux commands, programming concepts in C such as printf and scanf, and includes examples of simple C programs. The content serves as an introductory guide for students to learn programming and navigate the Linux operating system effectively.

Uploaded by

poojibv03
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Computer Programming Lab

The document outlines a computer programming lab focused on familiarizing students with the Linux environment, including editors like Vi, Vim, and Emacs, as well as compilers Turbo C and GCC. It provides detailed explanations of basic Linux commands, programming concepts in C such as printf and scanf, and includes examples of simple C programs. The content serves as an introductory guide for students to learn programming and navigate the Linux operating system effectively.

Uploaded by

poojibv03
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Computer Programming Lab

WEEK 1
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.

Definition of Linux:
Linux is a community of open-source Unix like operating systems that are based
on the Linux Kernel. It was initially released by Linus Torvalds on September 17, 1991. It is
a free and open-source operating system and the source code can be modified and distributed
to anyone commercially or non commercially under the GNU General Public License.
Initially, Linux was created for personal computers and gradually it was used in other
machines like servers, mainframe computers, supercomputers, etc. Nowadays, Linux is also
used in embedded systems like routers, automation controls, televisions, digital video
recorders, video game consoles, smartwatches, etc. The biggest success of Linux is
Android(operating system) it is based on the Linux kernel that is running on smart phones and
tablets. Due to android Linux has the largest installed base of all general-purpose operating
systems. Linux is generally packaged in a Linux distribution.

Linux Environment:
A Linux environment refers to the computing environment created by the Linux
operating system. It encompasses the entire ecosystem and resources provided by Linux,
including the kernel, system libraries, utilities, user interfaces, and various software components.
Here's an explanation of key aspects of a Linux environment:

Linux Editors:
Linux editors are software applications used for creating, editing, and manipulating text files and
code within the Linux operating system. They are essential tools for developers, system
administrators, and users who need to work with text-based configurations, scripts, programming
code, and various types of documents. There are several popular text editors available in the
Linux environment, each with its own features and capabilities. Here's an explanation of some
common Linux editors:
What is vi
The vi editor is elaborated as visual editor. It is installed in every Unix system. In other
words, it is available in all Linux distros. It is user-friendly and works same on different
distros and platforms. It is a very powerful application. An improved version of vi editor
is vim.

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.

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.

1. vi /home/sssit/Downloads/[Link]

2. Vim is an editor to create or edit a text file.

There are two modes in vim. One is the command mode and another is the insert
mode.

In the command mode, user can move around the file, delete text, etc.

In the insert mode, user can insert text.

Changing mode from one to another

From command mode to insert mode type a/A/i/I/o/O ( see details below)
From insert mode to command mode type Esc (escape key)

Some useful commands for VIM

Text Entry Commands (Used to start text entry)

a Append text following current cursor position

A Append text to the end of current line

i Insert text before the current cursor position

I Insert text at the beginning of the cursor line

What is Emacs?

Emacs is a text editor designed for POSIX operating systems and available on Linux,
BSD, macOS, Windows, and more. Users love Emacs because it features efficient
commands for common but complex actions and for the plugins and configuration
hacks that have developed around it for nearly 40 years.

Basic emacs Commands


M-x help Help (Note: M-x means to first hit the escape key and then hit x.)
Quit emacs (Note: C-x means to press the control key and while you are
C-x C-c
holding it down, press x. Other places use the notation ^X or ctrl-X.)
C-x C-f Find a file (i. e. open a file)
C-x C-s Save a file (the one you see)
Get out of the command that you are in - try this when you don't know what
C-g
is going on
C-x u Undo (you can repeat this to undo several previous commands)

Other commands in Linux


The ls command in Linux
The ls command is used to list files and directories in the current working directory. This
is going to be one of the most frequently used Linux commands you must know of.
The pwd command in Linux
The pwd command allows you to print the current working directory on your terminal. It’s
a very basic command and solves its purpose very well.

The cd command in Linux


While working within the terminal, moving around within directories is pretty much a
necessity. The cd command is one of the important Linux commands you must know
and it will help you to navigate through directories. Just type cd followed by directory as
shown below.
root@ubuntu:~# cd <directory path>

ii) Exposure to Turbo C, gcc

Definition of Turbo c:
Turbo C is a legacy integrated development environment (IDE) and compiler for the C and C++
programming languages. It was originally developed by Borland, a software company, and was
popular in the 1980s and 1990s, primarily for writing programs on the MS-DOS operating
system. Here's an explanation of Turbo C:

1. **Compiler**: Turbo C included a fast and efficient C and C++ compiler that could generate
executable files for MS-DOS. It supported the ANSI C standard, making it suitable for
developing standard-compliant code.
2. **Library Support**: Turbo C came with a set of libraries that provided functions for
common tasks, such as file input/output, math operations, and graphics. These libraries made it
easier for developers to create a wide range of applications.

3. **Debugger**: Turbo C included a debugger that allowed developers to set breakpoints, step
through code, and inspect variables during program execution. This was crucial for identifying
and fixing bugs (Errors) in software.

4. **Turbo C++**: Borland also released Turbo C++, an extension of Turbo C, specifically for
C++ programming. Turbo C++ included support for C++ language features and object-oriented
programming.

Commands in Turbo C:
Turbo C, which was a popular integrated development environment (IDE) and compiler for the C
and C++ programming languages, had its own set of commands and keyboard shortcuts for
various tasks. Below are some of the commonly used commands and keyboard shortcuts in
Turbo C:

SHORT CUT KEYS IN ‘C’ LANGUAGES

1. **File Operations**:

- **New File**: `Ctrl+N` - Create a new source code file.

- **Open File**: `Ctrl+O` - Open an existing source code file.

- **Save File**: `Ctrl+S` - Save the current file.

- **Save As**: `F2` - Save the current file with a different name.

2. **Editing**:

- **Cut**: `Ctrl+X` - Cut the selected text.

- **Copy**: `Ctrl+C` - Copy the selected text.

- **Paste**: `Ctrl+V` - Paste copied text.

- **Undo**: `Ctrl+Z` - Undo the last action.

- **Redo**: `Ctrl+Y` - Redo the last undone action.


- **Find**: `Ctrl+F` - Search for text in the current file.

- **Replace**: `Ctrl+H` - Replace text in the current file.

3. **Compiling and Running**:

- **Compile**: `F9` - Compile the current source code file.

- **Run**: `Ctrl+F9` - Compile and run the current program.

- **Debug**: `Ctrl+Alt+F5` - Start debugging the program.

4. **Debugging**:

- **Set Breakpoint**: `F5` - Set or remove a breakpoint at the current line.

- **Run to Cursor**: `Ctrl+F7` - Execute the program until the cursor line is reached.

- **Toggle Full-Screen Mode**: `Alt+Enter`.

To See the Output Results :Alt+F5.

7. **Other**:

- **Exit Turbo C**: `Alt+X` - Close the Turbo C IDE.

GCC:
GCC, which stands for "GNU Compiler Collection," is a widely used and highly
respected set of compilers for various programming languages, primarily designed for the C,
C++, and Fortran programming languages. It's a free and open-source compiler suite developed
by the GNU Project. Here's a basic explanation of GCC:

1. **Compiler Suite**: GCC is a collection of compilers, not just a single compiler. It includes
individual compilers for different programming languages like C (gcc), C++ (g++), and Fortran
(gfortran), among others.

2. **Cross-Platform**: GCC is cross-platform, which means it can run on a variety of operating


systems, including Linux, Unix, mac OS, and Windows, making it a versatile tool for software
development.

3. **Compilation Process**: GCC translates source code written in a high-level programming


language (e.g., C, C++) into machine code that can be executed by a computer. The compilation
process typically involves several stages, including preprocessing, compilation, assembly, and
linking.

4. **Open Source**: GCC is released under the GNU General Public License (GPL) and is part
of the Free Software Foundation's efforts to promote free and open-source software. This means
that the source code of GCC is freely available for anyone to view, modify, and distribute.

iii) Writing simple programs using printf(), scanf()

Explanation for printf Statement in C:


In the C programming language, the `printf` statement is a fundamental function “used for out
putting data to the standard output”, which is usually the terminal or console. “It allows you
to display text and values on the screen”. Here's a basic explanation of the `printf` statement in
C:

what is stdio.h?
The stdio.h is a header file that contains the information needed to include input/output
routines in our program. For instance, printf, scanf, and so on.

The stdio.h header file should be included in our source code if we intend to utilize
the printf or scanf functions in our program.

Otherwise, the software will give an error or warning stating that there was an implicit
declaration of the built-in function "printf," which it doesn't know what printf or scanf is.

#include is a directory for preprocessors.

It will insert the file specified by the angle brackets "<>" into the current source file.

If you use #include<stdio.h> in your Program code, it will include the stdio.h file into your
source code, which contains information for all input and output functions.

This particular file is composed of several standard #defines to define some of the standard I/O operations.
1. A main() function is a user-defined function in C that means we can pass parameters to
the main() function according to the requirement of a program.
2. A main() function is used to invoke the programming code at the run time, not at the
compile time of a program.
3. A main() function is followed by opening and closing parenthesis brackets

**Return Value**: A return 0 means that the program will execute successfully and did what it was
intended to do. return 1: A return 1 means that there is some error while executing the program, and it is
not performing what it was intended to do.

Example 1:

#include <stdio.h>
int main()
{
// Displays the string inside quotations
printf("C Programming");
return 0;
}
OUTPUT:

C Programming

Example 2:

#include <stdio.h>
int main()
{
int testInteger = 5;
printf("Number = %d", testInteger);
return 0;
}
Output:

Number = 5

Exapmple 3:

#include <stdio.h>
int main()
{
float number1 = 13.5;
double number2 = 12.4;

printf("number1 = %f\n", number1);


printf("number2 = %lf", number2);
return 0;
}

Output: number1 = 13.500000


number2 = 12.400000

Example 4:

#include <stdio.h>
int main()
{
char chr = 'a';
printf("character = %c", chr);
return 0;
}
Output:

character = a

Explanation for scanf Statement in C:


In the C programming language, the `scanf` function is used for “ reading and inputting data
from the standard input (typically the keyboard) into variables”. It is a fundamental function
for interactive console-based input. Here's a basic explanation of the `scanf` function in C:

scanf function format specifiers

- `%d`: Read an integer.

- `%f`: Read a floating-point number.

- `%c`: Read a character.

- `%s`: Read a string (sequence of characters).

- `%lf`: Read a double (for floating-point numbers with more precision).

- `%u`: Read an unsigned integer.


- `%x`: Read a hexadecimal integer.

- `%o`: Read an octal integer.

- `%p`: Read a pointer value.

- `%n`: Count the number of characters read.

Why &?
While scanning the input, scanf needs to store that input data somewhere. To store this input
data, scanf needs to known the memory location of a variable. And here comes the ampersand
to rescue.
 & is also called as address of the operator.
 For example, &var is the address of var.
Example of scanf
Below is the C program to implement scanf:
#include <stdio.h>

int main()
{
int a, b;

printf("Enter first number: ");


scanf("%d", &a);

printf("Enter second number: ");


scanf("%d", &b);

printf("A : %d \t B : %d" ,
a , b);

return 0;
}

Output
Enter first number: 5
Enter second number: 6
A:5 B:6
Example 2:

#include<stdio.h>
int main(){
int x=0,y=0,result=0;
printf("enter first number:");
scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);
result=x+y;
printf("sum of 2 numbers:%d ",result);

return 0;
}
Output:

enter first number:9


enter second number:9
sum of 2 numbers:18

Example 3:

#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

You might also like