Lab 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Lab - 01

To get familiar with Programming Environment and Fundamentals of


Programming Language

Theory:
The Development Environment - Integrated Development Environment (IDE):
The C compiler has its own built-in text editor. You may also use a commercial text editor or
word processor that can produce text files. The important thing is that whatever you write your
program in, it must save simple, plain-text files, with no word processing commands embedded
in the text. The files you create with your editor are called source files, and for
C++ they typically are named with the extension .CPP, .CP, or .C.

The C Developing Environment, also called as Programmers’ Platform, is a screen display with
windows and pull-down menus. Code of the program, error messages and other information are
displayed in separate windows. The menus may be used to invoke the operations necessary to
develop the program, debug and execute the program.

Invoking the IDE


To invoke the IDE from the windows you need to double click the Dev C++ icon.
This makes you enter the IDE interface, which initially displays only a menu bar at the top of the
screen and a status line below will appear. The menu bar displays the menu names and the status
line tells what various function keys will do.

Opening New Window


To type a program, you need to open an Edit Window. For this, open file menu and click “new”
or press [CTRL +N]. A window will appear on the screen where the program may be typed.

Writing a Program
When the Edit window is active, the program may be typed. Use the certain key combinations to
perform specific edit functions.

Saving a Program
To save the program, select save command from the file menu. This function can also be
performed by pressing the [CTRL+S]. A dialog box will appear asking for the path and name of
the file. Provide an appropriate and unique file name. You can save the program after compiling
too but saving it before compilation is more appropriate.

Making an Executable File


The source file is required to be turned into an executable file. This is called “Making” of
the .exe file. The steps required to create an executable file are:
1. Create a source file, with a .cpp extension.
2. Compile the source code into a file with the .obj extension.
3. Link your .obj file with any needed libraries to produce an executable program.

Compiling the Source Code


Although the source code in your file is somewhat cryptic, and anyone who doesn't know C will
struggle to understand what it is for, it is still in what we call human-readable form. But, for the
computer to understand this source code, it must be converted into machine-readable form. This
is done by using a compiler. Hence, compiling is the process in which source code is translated
into machine understandable language.

Creating an Executable File with the Linker


After your source code is compiled, an object file is produced. This file is often named with the
extension .OBJ. This is still not an executable program, however. To turn this into an executable
program, you must run your linker. C programs are typically created by linking together one or
more OBJ files with one or more libraries. A library is a collection of linkable files that were
supplied with your compiler.

Project/Make
Before compiling and linking a file, a part of the IDE called Project/Make checks the time and
date on the file you are going to compile.

Compiling and linking in the IDE


In the Dev C++, compiling and linking can be performed together in one step. There are two
ways to do this: you can select Make EXE from the compile menu, or you can press the F9 key.

Executing a Program
If the program is compiled and linked without errors, the program is executed by selecting
Run from the Run Menu or by pressing the F10 key.

The Development Cycle


If every program worked the first time you tried it that would be the complete development
cycle: Write the program, compile the source code, link the program, and run it.
Unfortunately, almost every program, no matter how trivial, can and will have errors, or bugs, in
the program. Some bugs will cause the compile to fail, some will cause the link to fail, and some
will only show up when you run the program. Whatever type of bug you find, you must fix it,
and that involves editing your source code, recompiling and relinking, and then rerunning the
program.

Correcting Errors
If the compiler recognizes some error, it will let you know through the Compiler window.
You’ll see that the number of errors is not listed as 0, and the word “Error” appears instead of the
word “Success” at the bottom of the window. The errors are to be removed by returning to the
edit window. Usually these errors are a result of a typing mistake. The compiler will not only tell
you what you did wrong; they’ll point you to the exact place in your code where you made the
mistake.

Exiting IDE
An Edit window may be closed in a number of different ways. You can click on the small square
in the upper left corner, you can select close from the window menu, or you can press the
[ALT+F4] combination.

Building Blocks of Programming Language:


In any language there are certain building blocks:
 Constants
 Variables
 Operators
 Methods to get input from user (cin , getch( ) etc.)
 Methods to display output (Escape Sequences etc.) and so on.

Variables and Constants


If the value of an item can be changed in the program then it is a variable. If it will not change
then that item is a constant. The various variable types (also called data type) in C are: int, float,
char, long etc. For constants, the keyword const is added before declaration.

Operators
There are various types of operators that may be placed in three categories:
Basic: + - * / %
Assignment: = += -= *= /= %=
(++, - - may also be considered as assignment operators)
Relational: < > <= >= == !=

Escape Sequences
Escape Sequence causes the program to escape from the normal interpretation of a string, so that
the next character is recognized as having a special meaning. The back slash “\” character is
called the Escape Character”. The escape sequence includes the following:
\n => new line
\b => back space
\r => carriage return
\” => double quotations

Getting Input From the User


The input from the user can be taken by the following techniques: scanf( ), getch( ),
getche(),getchar( ) etc.

Examples:

1. Implementing a Simple C Program


#include<iostream>
using namespace std;
int main()
{
cout<<"\n Hello World";
return 0;
}

2. Demonstrating the fundamentals of C Language


#include<iostream>
using namespace std;
int main()
{
int num1,num2,sum,product;
cout<<" The program takes two numbers as input and prints their sum and
product"<<endl;
cout<<" Enter first number:";
cin>>num1;
cout<<" Enter second number:";
cin>>num2;
sum=num1+num2;
product=num1*num2;
cout<<"\n "<<num1<<"+"<<num2<<"="<<sum;
cout<<"\n "<<num1<<"*"<<num2<<"="<<product;
return 0;
}

Lab Tasks
1- Write a program to calculate the Area ( A= π r2 ) and circumference of a circle (C=2 π r),
where r = radius is taken as input and is declared as a constant. The precision of should be the
number of characters in your name. Display the result to 4 decimal places.
2- Write single C ++ statement to output the following on the screen:
My name is “Your Name”
And roll number is “Your_roll_no”
I am a student of “Your Department”
3- Write a program that perform basic arithmetic operations by getting values from User.
4- Write a program that accept value from user and display its square and cube without using
predefined functions?

You might also like