Sei sulla pagina 1di 19

Compiler and Editor

Compiler
A compiler is a computer program which helps you transform source code written in a high-level language
into low-level machine language. It translates the code written in one programming language to some other
language without changing the meaning of the code. The compiler also makes the end code efficient which
is optimized for execution time and memory space.
Most high-level languages will include their own compiler or have toolkits available that can be used to
compile the program. Two popular compilers are Eclipse for Java and DEV C++ for C and C++. Depending
on how big the program is, it should take a few seconds or minutes to compile. If no errors are encountered
while being compiled, an executable file is created.

Editor
Editors or text editors are software programs that enable the user to create and edit text files.
In the field of programming, the term editor usually refers to source code editors that include many special
features specifically designed to simplify and speed up typing of source code, such as syntax highlighting,
indentation, autocomplete and brace matching functionality. These editors also provide a convenient way
to run a compiler, interpreter, debugger, or other program relevant for the software-development process.
So, while many text editors like Notepad can be used to edit source code, if they don't enhance, automate
or ease the editing of code, they are not source-code editors.
Example: Notepad, Notepad++, Eclipse Editor, Dev C++ Editor

4th generation languages with its advantages and disadvantages


Advantages:

 Programming productivity is increased. One line of 4GL code is equivalent to several lines of
3GL code.
 System development is faster.
 Program maintenance is easier.
 The finished system is more likely to be what the user envisaged, if a prototype is used and the
user is involved throughout the development.
 End user can often develop their own applications.
 Programs developed in 4GLs are more portable than those developed in other generation of
languages.
 Documentation is improved because many 4GLs are self documenting.
Disadvantages:

 The programs developed in the 4GLs are executed at a slower speed by the CPU.
 The programs developed in these programming languages need more space in the memory of the
computer system.

Compare if else statement with switch statement

In this C++ programming “If-else” and “switch” both are selection statements. The selection statements,
transfer the flow of the program to the particular block of statements based upon whether the condition is
“true” or “false”. The fundamental difference between if-else and switch statements is that the if-else
statement “selects the execution of the statements based upon the evaluation of the expression in if
statements”. The switch statements “selects the execution of the statement often based on a keyboard
command”.

Comparison Chart
Example of IF ELSE statement:

1. int i=45, j=34;


2. if (i==45 & j==34){
3. cout<< " i ="<<i;
4. }else{
5. cout<< " j ="<<j;
6. }
7.
8. //output
9. i=45

Example of Switch statement:

1. int c;
2. cout<<" choose the value from 1 to 3"; cin>>i;
3. switch( i ){
4. case 1:
5. cout<<"you choose dark choclate";
6. break;
7. case 2:
8. cout<<"you choose candy";
9. break;
10. case 3:
11. cout<<"you choose lollypop";
12. break;
13. .
14. .
15. default
16. cout<<"you choose nothing";
17. }

Break and Continue Statement


In programming execution of code is done line by line. Now in order to alter this flow C++ provides two
statements break and continue which mainly used to skip some specific code at specific line.

Following are the important differences between continue and break.


Break Statement Example:
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
cout << i << "\n";
}

Output: 0 1 2 3
This example jumps out of the loop when i is equal to 4:

Continue Statement Example:

for (int i = 0; i < 10; i++) {


if (i == 4) {
continue;
}
cout << i << "\n";
}
Output: 0 1 2 3 5 6 7 8 9

This example skips the value of 4:


Syntax Error vs Logical Error

Input and Output Statement

C++ comes with libraries which provides us with many ways for performing input
and output. In C++ input and output is performed in the form of a sequence of
bytes or more commonly known as streams.

Input Stream: If the direction of flow of bytes is from the device(for example,
Keyboard) to the main memory then this process is called input.

Output Stream: If the direction of flow of bytes is opposite, i.e. from main
memory to device( display screen ) then this process is called output.

Header files available in C++ for Input/Output operations are:


1. iostream: iostream stands for standard input-output stream. This header file
contains definitions to objects like cin, cout, cerr etc.
2. iomanip: iomanip stands for input output manipulators. The methods declared in
this files are used for manipulating streams. This file contains definitions of setw,
setprecision etc.
3. fstream: This header file mainly describes the file stream. This header file is used
to handle the data being read from a file as input or data being written into the file
as output.
The two keywords cout in C++ and cin in C++ are used very often for printing outputs
and taking inputs respectively. These two are the most basic methods of taking input
and printing output in C++. To use cin and cout in C++ one must include the header
file iostream in the program.

Example:

#include <iostream>
using namespace std;

int main()

int age;

cout << "Enter your age:";

cin >> age;

cout << "\nYour age is: " << age;

return 0;
}

Input is : 18
Output is :
Enter your age:
Your age is: 18
Program Life Cycle

Program life Cycle


A typical C program, compiled by Dev-C++ for instance will go through many transformations before
being executed in hardware. These days IDE made this process easier and real quick. You program is
ready to be executable in just couple of seconds.
There are following stages involved from writing a code to execution of the program:
Stage 1: Editor
The procedure to develop and execute a code in C++ start with writing code in IDE (Integrated
Development Environment) or Editor. This editor will create .cpp extension file. There are many IDE
available to execute a program like Eclipse, Visual Studio.
Stage 2: Preprocessor
In this stage preprocess take the program and convert it to pure C++ code by converting symbols and adding
directives. EXAMPLE: #include<iostream> will tell the preprocessor to read all the contents of the iostream
header file with is in .h format and include the contents into the program. C++ has already many directives
like #if , #else, #define.
Stage 3: Compiler:
In this stage compiler translate the program in to Low Level Assembly language and store in .obj file i.e
object file. This file contain binary code which is understood by machine.
At this stage if complier finds any mistake in code like missing semicolon or spelling mistake etc. it stops
and ask you to fix and then again compile.

Stage 4: Linker:
As the name suggest it links multiple object file in to single executable file .exe this is ready to be loaded
in memory. At advance level there will be multiple source codes for a single program there you have link
all to make one .exe file.
Stage 5: Loader:
At this stage executable file is loaded in to RAM (Read only memory). It’s basically a program of operating
system that allocate this memory space and transfer control to beginning of instruction.
Stage: CPU:
At this stage program is executed. If there is some input from peripherals then it waits for your input and
after that it shows your desired result. While giving data through peripheral if there is mistake then whole
program will stop and will start again from stage 2. For Example in program there was instruction of getting
integer from peripherals but input was a non-integer. So it will give error until you clear.
Editor Preprocessor Compiler

CPU Loader Linker

Do While and While Loop

While Loop:

A while loop is a control flow statement that allows code to be executed repeatedly based on a
given Boolean condition. The while loop can be thought of as a repeating if statement.

Syntax :
while (boolean condition)
{
loop statements...
}
Flow Diagram
Example:
#include <stdio.h>

int main()

{ int i = 5;

while (i < 10) {

printf("GFG\n");

i++; }

return 0; }

Output: GFG GFG GFG GFG GFG

Do While Loop:

do while loop is similar to while loop with the only difference that it checks for the
condition after executing the statements, and therefore is an example of Exit Control
Loop.
Syntax:
do
{
statements..
}
while (condition);
Example:

#include <stdio.h>

int main()

{ int i = 5;

do {

printf("GFG\n");
i++;

} while (i < 10);

return 0; }

Output: GFG GFG GFG GFG GFG

WHILE DO-WHILE

Condition is checked first then statement(s) Statement(s) is executed atleast once,

is executed. thereafter condition is checked.

It might occur statement(s) is executed zero

times, If condition is false. At least once the statement(s) is executed.


WHILE DO-WHILE

No semicolon at the end of while. Semicolon at the end of while.

while(condition) while(condition);

If there is a single statement, brackets are

not required. Brackets are always required.

Variable in condition is initialized before the variable may be initialized before or within the

execution of loop. loop.

while loop is entry controlled loop. do-while loop is exit controlled loop.

Qualities of Good Program:


Qualities of Good Programmer

A Willingness to learn
This is a trait that is highly overlooked by applicants when technology is always evolving and the
skills and abilities a programmer has today will likely be outdated in a few years. It’s important to
be a programmer who has an interest in keeping up with the latest trends and is eager to take any
opportunity to learn new skills and improve existing ones.

Problem-solving skills.
Great developers are usually independent and amazing self-learners. They have the ability to
learn new technologies on their own and aren’t intimidated by new challenges. For those who
have never attempted to create an application from scratch, programming can best be compared
to solving an extremely difficult math equation. A good programmer thrives on being innovative
and finding ways to make something work, despite the odds.

Communication Skills
Good communication skills directly correlate with good development skills. A great developer is
able to understand problems clearly, break them down into hypotheses and propose solutions in
a coherent manner. They understand concepts quickly, or ask the right questions to understand,
and don’t need to have everything written down specification document. Great offshore
developers usually speak multiple languages coherently and are very comfortable with
documentation in English.

Passion for the work.


While some programming staff can simply serve as nine-to-fivers or clock watchers, many hiring
managers are interested in finding someone who will gladly put in long hours when needed. True
programmers are self-proclaimed “computer geeks,” spending their time gaming, building
servers, or creating apps for themselves or friends. While this passion isn’t a necessity, it’s often a
way to differentiate top-shelf programmers from the rest.

Debugging skills.
Creating code is only part of a programmer’s job. When software doesn’t work as expected, a
programmer is expected to get to the root of the problem quickly and effectively. Instead of
spending hours blindly making changes, search for a programmer who prefers to carefully
investigate his code and research possible issues until an answer is found.

Constraints
Every project or job has several constraints whether it be time or budget. A good programmer
knows how to code in terms of time and space complexity. Since budget is really important in a
lot of projects, a good programmer will create a software using fewer resources. A good
programmer knows how to manage the project requirements and is very flexible.

Respect for deadlines.


Most programmers just like anyone else work on projects with deadlines. While it’s important
that managers understand a reasonable turnaround time for coding a new application or
repairing an existing one, programmers should also show respect for deadlines.
History of C Language

It is good to learn about the history of C programming. C was developed and written
by Dennis M. Ritchie in the year 1972, and hence he is known as the founder of C.

C programming language was developed to overcome the difficulties found in older


programming languages such as BCPL, BASIC, B, etc.
C is imperative language and designed to compile in a relatively straightforward manner
which provides low-level access to the memory. With the gradual increase in the
popularity of the program, the language and its compiler have become available on a
wide range of platforms from embedded microcontrollers to supercomputers.
With the introduction of K&R C language (which is a new edition of C published in 1978
by Brian Kernighan and Denis Ritchie), several features have been included in C
language.

Some of these features are:

 Standard I/O (Input/Output) Library


 long int - data type
 unsigned int - data type
 Compound assignment operators

During the late 1980s, C was started to use for a wide variety of mainframe computers,
micro and mini computers which began to get famous. Gradually C got its superset -
i.e., C++ which has added features, but it's developed from C with all its initial concepts.

Array and Types of Array

Arrays:-
When there is a need to use many variables then There is a big problem because we will
Conflict with name of variables So that in this Situation where we wants to Operate on
many numbers then we can use array . The Number of Variables also increases the
complexity of the Program. So that we uses Arrays.
Arrays are Set of Elements having same data type or we can Say that Arrays are Collection
of Elements having same name and same data type But Always Remember Arrays are
Always Start From its index value and the index of array is start From 0 to n-1.

Suppose we wants to Access 5th Element of array then we will use 4th Element Because
Arrays are Start From 0 and arrays are always stored in Continuous Memory Locations
The Number of Elements and Types of array are Identified by Subscript of array Elements.
The Various types of Array those are provided by c as Follows:-
1. Single Dimensional Array
2. Two Dimensional Array
3. Three Dimensional array
4. Character Array or Strings.

Single Dimensional Array


A dimensional is used representing the elements of the array for example
int a[5]
The [] is used for dimensional or the sub-script of the array that is generally used for
declaring the elements of the array For Accessing the Element from the array we can use
the Subscript of the Array like this
a[3]=100
This will set the value of 4th element of array
So there is only the single bracket then it called the Single Dimensional Array
This is also called as the Single Dimensional Array
2) Two Dimensional Array or the Matrix
The Two Dimensional array is used for representing the elements of the array in the
form of the rows and columns and these are used for representing the Matrix A Two
Dimensional Array uses the two subscripts for declaring the elements of the Array
Like this int a[3][3]
So This is the Example of the Two Dimensional Array In this first 3 represents the total
number of Rows and the Second Elements Represents the Total number of Columns The
Total Number of elements are judge by Multiplying the Numbers of Rows * Number of
Columns in The Array in the above array the Total Number of elements are 9

3) Multidimensional or the Three Dimensional Array :The Multidimensional


Array are used for Representing the Total Number of Tables of Matrix A Three
dimensional Array is used when we wants to make the two or more tables of the Matrix
Elements for Declaring the Array Elements we can use the way like this
int a[3][3][3]

In this first 3 represents the total number of Tables and the second 3 represents the total
number of rows in the each table and the third 3 represents the total number of Columns
in the Tables
So this makes the 3 Tables having the three rows and the three columns
The Main and very important thing about the array that the elements are stored
always in the Contiguous in the memory of the Computer

4) Character Array of String: -Like an integer characters are also be in the Array The
Array of Characters are called as the Strings They are Generally used for representing the
Strings Always Remember that a String is Terminated with the \0 or Null Character

 Single Dimention Character Array


Like The array elements of Integer Types The Character array also are the Single
Dimensional or The Two Dimensional Array
Single Dimensional Array The Single Dimensional array are used for creating the Number
of characters like
char name[10]
in this we can use the 10 characters on the name variable Means we can give the name as
10 characters long

 Two Dimention Character Array


When we talk about the Two Dimensional of the Character array The first Subscript of the
array if used for representing the Total Numbers of Strings and the second Subscript is
used for defining the length of the array Characters in the String
like This
char name[5][10]

It declares the 5 names and each having the characters up to 10 So the First Subscript is
used for defining the total number of the Strings and the Second is used for Length of the
Characters
Pointer and Structure

Pointers:

Pointers are symbolic representation of addresses. They enable programs to simulate


call-by-reference as well as to create and manipulate dynamic data structures. It’s
general declaration in C/C++ has the format:
Syntax:
datatype *var_name;
int *ptr; //ptr can point to an address which holds int data

How to use a pointer?


 Define a pointer variable
 Assigning the address of a variable to a pointer using unary operator (&) which
returns the address of that variable.
 Accessing the value stored in the address using unary operator (*) which returns
the value of the variable located at the address specified by its operand.

Example:
// C++ program to illustrate Pointers in C++

#include <bits/stdc++.h>
using namespace std;
void geeks()
{
int var = 20;

//declare pointer variable


int *ptr;

//note that data type of ptr and var must be same


ptr = &var;

// assign the address of a variable to a pointer


cout << "Value at ptr = " << ptr << "\n";
cout << "Value at var = " << var << "\n";
cout << "Value at *ptr = " << *ptr << "\n";
}
//Driver program
int main()
{
geeks();
}

Output:
Value at ptr = 0x7ffcb9e9ea4c
Value at var = 20
Value at *ptr = 20

Strcuture:

A Structure in C++ is a group of data elements grouped together under one name. These data
elements, known as members, can have different types and different lengths. It is a user defined
data type which allows you to combine data items of different kinds.
Imagine you want to store details of a person for e.g. his name, age, height, date of
birth and weight. In a typical scenario you will have to create 5 variables to store these values, but
what if you have to store details of 20 people. Then you will have to create 100 variables and that
will be very tedious and inefficient. In such scenarios we can create a struct datatype
named Person and include all the details as member variables of this datatype to create a custom
datatype. We can then simply create 20 variables of this new structure datatype or a single array of
size 20.
Example:

To access any member of a structure, we use the member access operator (.). The member
access operator is coded as a period between the structure variable name and the structure
member that we wish to access

Potrebbero piacerti anche