Fundamentals of Computer
Programming
(CS-110)
Course Instructors:
Mehwish Kiran
1
How a
Computer runs
a Program?
High-level programming code is transformed
into machine-understandable code through
either
compilation
or
interpretation,
depending on the language, and may involve
intermediate representations or runtime
environments to facilitate execution.
• Source code
• Compilation or Interpretation
• Intermediate Representation
• Execution
• Run-time Libraries
• Operating System Interaction
• Execution Results
2
Compiled vs. Interpreted Languages
3
Compiled
Languages
• Compilation involves translating the entire source code into
machine code all at once. The result is an executable file that
can be run directly by the computer's hardware.
4
Detailed steps of a Compiler
• Lexical Analysis
• Syntax Analysis
• Semantic Analysis
• Intermediate Code Generation
• Optimization
• Code Generation
5
Interpreted
Languages
• Instead of translating the entire code into machine
code at once, an interpreter reads and executes the
code line by line. It translates each line or statement
into machine code just before it's executed.
6
Integrated Development Environment
(IDE)
• An integrated development environment is a software
application that provides comprehensive facilities for
software development.
• An IDE normally consists:
• a source-code editor,
• build automation tools, and
• a debugger.
7
Integrated Development
Environment (IDE)
- Steps
• Computer programs are usually
written (coded) as ASCII text into a
source code file with a extension
(Examples: .cpp, .c .py) to identify it
as a source code file.
• *Errors: Any unsaved code
• The first step is to execute the preprocessor compiler directives (e.g.
#include will insert header files into
the code). Saves code as translation
unit.
• *Errors: Missing header files
8
Integrated Development
Environment (IDE)
- Steps
• The second step of compiler is to open the
translation unit file and converting the
programming language code to machine
instructions for the CPU, a data area and a
list of items to be resolved by the linker.
Saves it as an object file.
• *Errors: Syntax or violation of the
programming language rules
• The linker opens the program object file
and links it with the library object files as
needed. Saves it as executable file.
• *Errors: Linker items unresolved
9
Integrated Development
Environment (IDE)
- Steps
• The IDE directs the operating system’s
program called the loader to load the
executable file into the computer's
memory and have the Central Processing
Unit (CPU) start processing the instructions
• *Errors: Logical errors
10
Popular C++ IDEs
11
Basic C++
Program
Structure
12
Basic C++ Program Structure
13
Types of Errors
https://www.geeksforgeeks.org/errors-in-cc/
14
Syntax Errors
Errors that occur when you violate the
rules of writing C/C++ syntax are known
as syntax errors. This compiler error
indicates something that must be fixed
before the code can be compiled. All
these errors are detected by compiler and
thus are known as compile-time errors.
Most frequent syntax errors are:
• Missing Parenthesis (})
• Printing the value of variable without
declaring it
• Missing semicolon like this:
// C++ program to illustrate
// syntax error
#include <iostream>
using namespace std;
void main()
{
int x = 10;
int y = 15;
cout << " "<< (x, y) // semicolon missed
}
15
// C++ program to illustrate
// syntax error
#include <iostream>
using namespace std;
Syntax Errors
int main(void)
{
while(.) // while() cannot contain "." as an argument.
{
cout <<"hello";
}
return 0;
}
16
Run-time Errors
Errors which occur during program
execution(run-time) after successful
compilation are called run-time errors.
One of the most common run-time error
is division by zero also known as Division
error. These types of error are hard to
find as the compiler doesn’t point to the
line at which the error occurs.
// C++ program to illustrate
// run-time error
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void main()
{
int n = 9, div = 0;
// wrong logic
// number is divided by 0,
// so this program abnormally terminates
div = n/0;
cout << "result = "<< div;
}
17
Linker Errors
// C++ program to illustrate
// linker error
#include <bits/stdc++.h>
These are errors generated when the
executable of the program cannot be
generated. This may be due to wrong
function prototyping, incorrect header
files. One of the most common linker
error is writing Main() instead of main().
using namespace std;
void Main() // Here Main() should be main()
{
int a = 10;
cout << " "<< a;
}
18
Logical Errors
On compilation and execution of a
program, desired output is not obtained
when certain input values are given.
These types of errors which provide
incorrect output but appears to be error
free are called logical errors. These are
one of the most common errors done by
beginners of programming.
// C++ program to illustrate
// logical error
int main()
{
int i = 0;
// logical error : a semicolon after loop
for(i = 0; i < 3; i++);
{
cout << "loop ";
continue;
}
return 0;
}
19
Semantic Errors
This error occurs when the statements
written in the program are not
meaningful to the compiler.
// C++ program to illustrate
// semantic error
int main()
{
int a, b, c;
a + b = c; //semantic error
}
20
Acknowledgment
• Content of these slides are taken from:
• https://www.geeksforgeeks.org/
• https://www.tutorialspoint.com/
21