Unit 1 Elements of Programming
Unit 1 Elements of Programming
Programming Language
What is Language?
1. Introduction
We'll study the roles of the compiler, linker, assembler, and loader modules
in a typical process of generating an executable.
2. Executable Generation
3. Compiler
The assembler Enters the Arena after the compiler has played its part. The
assembler translates our assembly code to the machine code and then
stores the result in an object file. This file contains the binary
representation of our program.
Moving further, the assembler gives a memory location to each object
and instruction in our code. The memory location can be physical as well
as virtual. A virtual location is an offset that is relative to the base address
of the first instruction.
5. Linker
Next, we move to the linker module. The linker spawns to action after the
assembler has done its job. The linker combines all external programs
(such as libraries and other shared components) with our program to
create a final executable. At the end of the linking step, we get the
executable for our program.
So, the linker takes all object files as input, resolves all memory references,
and finally merges these object files to make an executable file.
Thus, there are two prime tasks of the linker. The first is to probe and find
referenced modules or methods, or variables in our program. And the
second is to determine and resolve the absolute memory location where
these codes need to be loaded.
6. Loader
The loader is a specialised operating system module that comes last in the
picture. It loads the final executable code into memory.
Afterward, it creates the program and data stack. Then, it initialises various
registers and finally gives control to the CPU so that it can start executing
the code.
Limitations and Features
1.Machine Level Language
2. High Level Language
Tools and Techniques of Problem Analysis
Algorithm Development and Flowchart
Algorithms and flowcharts both are used when creating new programs. An
algorithm is a step-by-step analysis of the process, whereas the flowchart
explains the steps in a graphical manner.
Algorithm
Before solving a problem, one should know what to do, how to do it, and
what types of steps should be taken. So, an algorithm is a step-by-step
method for solving a problem. An algorithm refers to a set of instructions
that define the execution of work to get the expected results.
Advantages of algorithm
Disadvantages of algorithm
○ Jumping (or goto statements) makes the algorithm hard to trace the
problems.
Now, let's see an example of an algorithm.
Algorithm:
Step 4: If 'x' is less than 20, then go back to step 2. Otherwise, go to the
next step.
Step 5: Exit.
Flowchart In Programming
Example :
Examples
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)
18)
be followed in a program.
understand. understand.
Implementation There are no rules employed Predefined rules are
debug. debug.
represent.
C Syntax
Syntax
You have already seen the following code a couple of times in the
first chapters. Let's break it down to understand it better:
Example
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Example explained
Note: The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}
Example
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
Example
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
C New Lines
New Lines
Example
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
Example
#include <stdio.h>
int main() {
return 0;
Tip: Two \n characters after each other will create a blank line:
Example
#include <stdio.h>
int main() {
printf("Hello World!\n\n");
return 0;
What is \n exactly?
Single-line Comments
Any text between // and the end of the line is ignored by the
compiler (will not be executed).
Example
// This is a comment
printf("Hello World!");
Example
Example
printf("Hello World!");
What is Token in C?
TOKEN is the smallest unit in a ‘C’ program. It is each and every word and
punctuation that you come across in your C program. The compiler breaks
a program into the smallest possible units (Tokens) and proceeds to the
various stages of the compilation. C Token is divided into six different
types, viz, Keywords, Operators, Strings, Constants, Special Characters,
and Identifiers.
Keywords and Identifiers
● In ‘C’ every word can be either a keyword or an identifier.
● Keywords have fixed meanings, and the meaning cannot be changed.
● They act as a building block of a ‘C’ program. There are a total of 32
keywords in ‘C’. Keywords are written in lowercase letters.
do if static while
C Data Types
A variable in C must be a specified data type, and you must use a
format specifier inside the printf() function to display it:
Example
// Create variables
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
Basic Data Types
The data type specifies the size and type of information the
variable will store.
%d or %i int
%f float
%lf double
%c char
Example
Example
C Constants
If you don't want others (or yourself) to change existing variable
values, you can use the const keyword.
Example
Example
Notes On Constants
Like this:
Good Practice
Another thing about constant variables, is that it is considered
good practice to declare them with uppercase. It is not required,
but useful for code readability and common for C programmers:
Example
C Variables
Variables are containers for storing data values, like numbers and
characters.
Syntax
Example
Create a variable called myNum of type int and assign the value 15
to it:
You can also declare a variable without assigning the value, and
assign the value later:
Example
// Declare a variable
int myNum;
myNum = 15;
Output Variables
You learned from the output chapter that you can output
values/print text with the printf() function:
Example
printf("Hello World!");
Example
int x = 5, y = 6, z = 50;
printf("%d", x + y + z);
You can also assign the same value to multiple variables of the
same type:
Example
int x, y, z;
x = y = z = 50;
printf("%d", x + y + z);
Real-Life Example
Example
// Student data
// Print variables
Example
int x = 5;
int y = 2;
int sum = 5 / 2;
To get the right result, you need to know how type conversion
works.
Implicit Conversion
float myFloat = 9;
As you can see, the compiler automatically converts the int value
9 to a float value of 9.000000.
This can be risky, as you might lose control over specific values in
certain situations.
Example
printf("%d", myInt); // 9
Explicit Conversion
Example
int num1 = 5;
int num2 = 2;
Example
int num1 = 5;
int num2 = 2;