Structures & Pointers-1
Structures & Pointers-1
STRUCTURES
& POINTERS
Part 1
Contents
Structures
Variable declaration and memory allocation
Variable initialisation
Accessing elements of structure
Nested structure
Array Vs Structure
Structures
Structures
Structure is a user-defined data type of C++ to represent a collection
of logically related data items, which may be of different types, under a
common name. Arrays allow to define type of variables that can hold
several data items of the same kind. Similarly structure is another user
defined data type available in C++ that allows to combine data items of
different kinds.
-
Defining a Structure
To define a structure, you must use the struct statement. The struct
statement defines a new data type, with more than one member. The
format of the struct statement is as follows −
data_type variable;
- -
……………………………..
-
-
……………………………..
data_type variable;
-
-
int adm_no;
char name[20];
-
-
char group[20];
- -
float fee;
-
};
-
-
Struct date
{
int dd;
int mm;
int yy;
};
Struct date
{
int day;
char month[10];
int year;
};
Variable declaration and memory allocation
Structure variable can be declared by the syntax
-
(in
Student >;
Struct
Eg:
--
E Struct student s;
-
-
int admno Student s;
, -
float fee;
admng
.
(in >
-
int mark
y CInDS . admno
3; cinS .
fee
CkDS Mosk
Student S
;
.
-
-
date dob, today; struct date dob, today;
OR
strdate adm_date, join_date;
Memory allocation
of structure variable
A c++ compiler allocate separate memory location for each and every
structure members.
int -
3
struct student
S
{ Chri =
12
-
Int roll_no;
&
-
-
Char name[20];
-
-
fFloat mark;
-
-
=
-
I float =
Ya
}; L
- -
-
20
Total 28 bytes
Roll_no 4 byte t
Name 20*1=20 byte
Mark 4 byte &
= 28 bat
=
day month[10] year
Eg:
structure_variable.element_name
- -
s.roll_no=5;
s.name=“abc”;
today.dd = 10;
strcpy(adm_date.month, "June");
cin >> s1.adm_no;
cout << c1.real + c2.real;
Write a c++ programme to read and print student details using structure.
Nested structure
float fee; }; -
};
student s = {4325, "Vishal", {10, 11, 1997}, 575};
cout<<s.adm_no<<s.name;
cout<<s.dt_adm.day<<"/"<<s.dt_adm.month<<"/"<<s.dt_adm.year;
outer_structure_varaiable.inner_structure_variable.element
Arrays Structures
-
It is a derived data type. It is a user-defined data type
T -
STRUCTURES
& POINTERS
Part 2
Contents
Pointer
Benefits of pointers
Declaring pointer variable
Methods Of Memory Allocation
Dynamic operators
Memory leak
Pointers
Pointer
A pointer is a derived data type.
It contain memory address of another variable.
It can be used to access & manipulate data stored in the memory.
Pointer
Pointers are more efficient in handling arrays.
Pointers can be used to return multiple values from a function.
Pointers support dynamic memory management.
Pointers provide an efficient tool for manipulating dynamic data
structures such as data structures linked list , queue.
Pointers reduced length and complexity of a programme.
Pointers increases the execution speed.
int num=25;
num 25
1001 1001 1001 1001
variable is associated with two values:
L-value and R-value,
where L-value is the address of the variable and Rvalue is its content.
Eg:
int *p;
int *ptr1;
float *ptr2;
struct student *ptr3;
The address of a variable can be assigned to pointer variable using address
operator (&)
Eg:
int *p;
int a;
p=&a
& -address of
ptr1 = #
rains at
-
-
O ptr1 1001
1500
-~
num 25
1001
W
cout<< # 1001
-
cout<< num; 25
ptr1 1001
-cout<< *ptr1; 25 1500
=>
O 25
- cout<< *num;
=
num
1001
cout<< # // 1001 (address of num) will be the output
cout<< ptr1; // 1001 (content of ptr1) will be the output
cout<< num; // 25 (content of num) will be the output
cout<< *ptr1;/* 25 (value in the location pointed to by
ptr1) will be the output */
cout<< &ptr1;// 1500 (address of ptr1) will be the output
cout<< *num; // Error!! num is not a pointer
The process of assigning address of a variable is known as initialization.
initialization:- int a;
int *p=&a;
Int x,*p1,**p2;
X=100;
P1=&x;
P2=&p1
COUT<<**p2;
A pointer can be initialized with null or zero.
Multiplication is not possible on pointer variable.
Methods Of Memory Allocation
The memory allocation that takes place before the execution of the
program is known as static memory allocation.
-
-
New : allocate
-
Dynamic operatives
- delete : de-allocate
Dynamic operators - new and delete
The operator new is a keyword in C++ and it triggers the allocation of
memory during run-time (execution).
Int Aj
!
-
P =
new
;
Int
short * si_ptr;
float * fl_ptr;
struct complex * cx_ptr;
si_ptr = new short;
fl_ptr = new float;
cx_ptr = new complex;
si_ptr fl_ptr cx_ptr
real imagenary
1000 1001 1010 1011 1012 1013 1200 1201 1202 1203
Allocated memory locations can also be initialised using the following syntax:
int
The following are valid examples:
P-
new int,
delete si_ptr;
delete fl_ptr, cx_ptr; delete :
Memory leak
Orphaned Memory Block -
If the memory allocated using new operator is not freed using delete,
that memory is said to be an orphaned memory block
-
a block of memory that is left unused, but not released for further
allocation. This memory block is allocated on each execution of the
program and the size of the orphaned block is increased. Thus a part
of the memory seems to disappear on every run of the program, and
eventually the amount of memory consumed has an unfavorable effect.
This situation is known as memory leak.
-
The following are the reasons for memory leak:
- Forgetting to delete the memory that has been allocated dynamically
(using new).
-Failing to execute the delete statement due to poor logic of the program
code.
-
-
Assigning the address returned by new operator to a pointer that was
-
STRUCTURES
& POINTERS
Part 3
Contents
Operations on pointers
Arithmetic operations
on pointers
Relational operations
on pointers
Pointer and array
Dynamic array
Pointer and string
Array of strings
Pointer and structure
Self referential structure
Questions
Operations on pointers
The operators that can be used with pointers and how these
operations are performed.
Arithmetic operations on pointers
si_ptr and fl_ptr
real imagenary
1000 1001 1010 1011 1012 1013 1200 1201 1202 1203
cout << si_ptr + 1;
cout << fl_ptr + 1;
What will be the output? Do you think that it will be 1001 and 1011?
When we add 1 to a short int pointer, the expression returns the address of
the next location of short int type.
si_ptr+1
(1000+(1*2))=1002 (size of short int is 2 bytes)
fl_ptr+1
(1010+(1*4))=1014 (size of float is 4 byte)
si_ptr+5=?
si_ptr+5 = (1000+5×2)=1010
fl_ptr+3 = (1010+3×4)=1022
pointer ht_ptr is initialised with the address of only one location. The memory
locations accessed using pointer arithmetic on ht_ptr are unauthorised, since
these locations are not allocated by the OS.
p==q or p!=q
- - -
Int -
Pointer and array -
=
-
-
O-
Int * Per
; O
1000
1004
O
34
⑧12
-
-
OL
P = 1000
fartoj
-
Ptr = 1008 8 -
-
1012 18
Cout2PEr;
1016
1020
24
38
S Anas abdul
1028 14
cont ** A/34 1032
1036
7
O 9
19
Write C++ statement to display all the 10 elements of this array.
If ptr is an integer pointer
ptr = &ar[0];
int
cout<<(ptr+9); //Displays O
1036, the address of ar[9]
-
cout<<*(ptr+9); //DisplaysO
19, the value of ar[9]
--
=
loots
1x4
1000 +
- ⑩
9X7
+
⑮
-
1000
-
=
1000 + 36 =
-
Can you predict the output of the statement:
cout<<ar;
output will be 1000
Because array-name always contains the base address of the array, and it
cannot be changed.
Dynamic array
Dynamic array is created during run time using the dynamic memory
allocation operator new. -
new in + [5] ;
inf
To find the highest percent of pass in schools
#include <iostream>
using namespace std;
int main()
{
float *pass, max;
int i, n;
cout<<"Enter the number of schools: ";
cin>>n; //To input number of schools
pass new float [n]; //dynamic array having n elements
for (i=0; i<n; i++)
{
cout<<"Percent of pass by school "<<i+1<<": ";
cin>>pass[i]; //Concept of subscripted variable
}
max=pass[0];
for (i=1; i<n; i++) if (pass[i]>max) max = (pass+i);
/* Elements are accessed using subscript and pointer arithmetic operation */
cout<<"Highest percent is '<<max;
return 0;
}
Pointer and string
String data can be referenced by character array
P
SPET
*
Advantages of character pointer
~Since there is no size specification, a string of any number of characters
can be stored. There is no wastage or insufficiency of memory space. But it
-
char *name[7];
This array can contain a maximum of 7 strings, where each string can
contain any number of characters.
struct employee
{
int ecode;
char ename[15];
float salary;
};
employee *eptr;
It is clear that eptr is a pointer that can hold the address of employee
type data. The statement:
structure_variable.element_name
have to use the pointer eptr. The syntax for accessing the elements of a
structure is as follows:
structure_pointer->element_name
Self referential structure -
-
Self referential structure is a structure in which one of the elements is a
pointer to the same structure.
- - -
-
struct node
{
int date1;
char date2;
struct node *link;
}; xyz
Self referential structure is a powerful tool of C and C++ languages
that helps to develop dynamic data structures like linked list, tree, etc
The memory locations are scattered, but there will be a link from one
location to another
Q. Run time allocation of memory is triggered by the operator ……
Ans. new.
Q. Define a structure named ‘Time’ with elements hour, minute and second
a) cout<<*(p+2);
b) cout<<*p+3;
Ans.
a)20
b)13
Q. What are the different memory allocations in C++? Explain
Ans. Different memory allocations are: Static and Dynamic Memory Allocation
Arrays Arrays
i) Takes place before the execution of the
program.
struct
{
int regno ;
char name[20] ;
float mark=100 ;
};
Ans.
.
.1.struct tagname
2.float mark=100;
Q.Structure with in a structure is termed as -----------------.
Ans.
Nested structure
Q. Consider the given structure definition
{
int real;
int imag;
};
(b) c1.real=15;
Q. Write the use of * and & operators used in pointer
Ans.
The dereference operator (*) retrieves the value pointed to by the pointer
Address of operator ( & ), to get the address of a variable
Q. Distinguish between array and structure
Ans:
Arrays Arrays
Derived data type User defined data type
Ans:
Using delete operator
Q. What is the difference between the declaration statements given below ?
( b) int * ptr = new int [10 ] ; This is creation of a dynamic array of size 10.
Q. What is a pointer in C++ ? Declare a pointer and initialize with the name
of your country.
Ans.
Pointer is a variable that can hold the address of a memory location.
It is a primitive since it contains memory address which is atomic in nature.
CONCEPTS OF OBJECT
ORIENTED PROGRAMMING
Part 1
Content
Programming paradigm
Objects
Classes
Data abstraction
Data Encapsulation
Modularity
PROGRAMMING
Programming paradigm
-
The arrangement of local variables, global variables and functions in a
procedural programming paradigm
-
Adding new data element may require modifications to
all/many functions
Since many functions access global data, the way the data is stored is important.
The arrangement of data cannot be changed without modifying all the functions
that access it.
~
Creating new data types is difficult
Computer languages typically have several built-in data types like integer, float,
character and double. Certain programming language allows creation of data
types other than the built-in data type, which means they are extensible.
-
Provides poor real world modelling -
⑰
objects effectively.
A
date
Object-Oriented Programming (OOP) paradigm -
⑳ -
Advantages of using OOP are:
Data Encapsulation
-
Modularity ~
Inheritance -
Polymorphism -
Objects
Anything that we see around us can be treated as an object and all these
objects have properties (also called member/data/state) and behaviour
--
(also called methods/member functions).
-
Student
State
RegNo, Name, Age,
Weight, Height, Mark
Behavior
Register, Change mark,
Change Height Weight Stack
State
Push Pop
Top, Length, Full,
Clock Empty
State
Dial Color, Hour Behavior
Minute Push, Pop
Behavior
Set Time, Showtime
Radio
State
On Off, Current Volume,
Current Station
Behavior
Turn on off, Increase
volume, Decrease volume,
seek, scan, and tune Array
State
Length, Full, Empty,
Student 0 1 2 3 4 Current Index
State Behavior
Name, Current Gear, Insert, Delete, Sort,
Current Speed, Traverse, Merge, Print
Headlight Status
Behavior
Push accelerator,
Change gear, light on off
Dog
State
Name, Color, Breed,
Hungry
Behavior
Barking, Fetching.
Wagging tail Window
State
Top, Left, Name,
Current State
Bike
State Behavior
Speed, Acceleration, Minimise, Maximise,
Current Gear Move, Close
Behavior
Turn the accelerator,
Push the brake, Change
gear
Classes -
A class is a prototype/blue print that defines the specification common to all
-
objects of a particular type.
-
General form of a class
declaration
class class_name
Variables
{
declared inside
private: known as
variable declarations; data member
function declarations;
public:
variable declarations;"
Functions declared
inside the class are function declarations;
known as member }
functions (behavior)
Example for class
class cube class Name
{
private:
int height, color; Data Members
public:
void get_cube_info()
{
cout << "Enter Height: ";
cin >> height; Member/ Function
cout << "Enter Color: "; Method/ Behavior
cin >> color;
}
};
Class: A class in C++ is the building block, that leads to Object-Oriented
programming. It is a user-defined data type, which holds its own data members
and member functions, which can be accessed and used by creating an instance
of that class. A C++ class is like a blueprint for an object.
#include<iostream.h>
#include<conio.h>
Class student
{
Private:
char name[10];
int age;
Public:
void read()
{
cout<<“enter the name”;
cin>>name;
cout<<“enter the age “;
cin>>age;
}
void print()
{
cout<<“name =“<<name;
cout<<“age=“<<age;
}
};
Void main()
{
Clrscr();
Student s;
s.read();
s.print();
Getch();
}
Passing Message.
Objects can communicate with each other by passing message, which is
similar to people passing message with each other. This helps in building
systems that simulate real life. In OOP, calling member function of an object
from another object is called passing message. Message passing involves
specifying the name of object, the name of the member function, and the
information to be sent.
Data abstraction
Data abstraction refers to showing only the essential features of the application
and hiding the details from outside world.
C++ classes provide great level of data abstraction. They provide public methods
/functions to the outside world to use the functionality of an object and to
manipulate object data.
Data Encapsulation
Encapsulation is an OOP concept that binds together the data and functions
-
-
that manipulate the data, and keeps both data and function safe from outside
interference and misuse.
School Software
Student Teacher
CHAPTER 2
CONCEPTS OF OBJECT
ORIENTED PROGRAMMING
Part 2
Content
Basic concepts of OOP
Inheritance
Polymorphism
Basic concepts of OOP
Inheritance
Inheritance is the process by which objects of one class acquire
the properties and functionalities of another class. Inheritance
supports the concept of hierarchical classification and reusability.
Class E
S'B
i S
Vehicle
1. Single Inheritance -
-
2. Multiple Inheritance
-
3. Multilevel Inheritance
-
4. Hierarchical Inheritance
5. Hybrid (Virtual) Inheritance -
1. Single Inheritance: In single inheritance, a class is allowed to
inherit from only one class. i.e. one sub class is inherited by one
base class only.
E
Class A (Base Class)
Class B Class c
Class A L
(Derived Class)
class subclass_name : access_mode base_class1,
access_mode base_class2, ....
{
//body of subclass
};
Class A
{
…………
……..
};
Class B
{
………..
…….
};
Class C: public A, public B
{
……
……
}
3. Multilevel Inheritance:
In this type of inheritance, a derived class is created from another
derived class.
-
Class c (Base Class 2)
Class B Class E
Class E
Class B
Class A Class c
Polymorphism -
The word polymorphism means having many forms. In simple
words, we can define polymorphism as the ability of a message
to be displayed in more than one form.
In C++ polymorphism is mainly divided into two types:
Compile time Polymorphism -
Runtime Polymorphism -
polymorphism
When a function name is overloaded with different jobs it is called Function Overloading.
In Function Overloading “Function” name should be the same and the arguments should
be different.
void print(int i)
{
cout << " Here is int " << i << endl;
}
void print(double f)
{
cout << " Here is float " << f << endl;
}
void print(char const *c)
{
cout << " Here is char* " << c << endl;
}
int main()
{ Out put
print(10); Here is int 10 Here is
float 10.1 Here is
print(10.10); char* ten
print("ten");
return 0;
}
Operator Overloading in C++
2
.
.
103 ↓
-
-
S
Runtime polymorphism
#include<iostream.h>
Class A
{
Public :
void show()
{
cout<<“show base class”;
}
};
Class B: public A Obj.show();
{ Return 0;
public: }
void show()
{ Out put
Show child class
cout<<“show child class”;
};
Int main()
{
B obj;
Function overloading:
same name
with different arguments
Function overriding:
same name
with same arguments
inheritance should be the there if you are function override.
DATA
STRUCTURE
-
Part 1
Definition
Data structure is a particular way of organising
similar or dissimilar logically related data items
-
which can be processed as a single unit.
Data Stuctures
-
-Simple Data Stuctures Compound Data Stuctures -
-
-
Array Structure Linear Non-Linear
-
-
-
Linked List
Tree
Queue
Stack Graph
-
Operations on Data structure
-
1. Traversing : Traversing is an operation in which each
element of a data structure is visited. The travel proceeds
from the first element to the last one in the data structure.
-
2. Searching : Searching, in the literal sense, is the process
of finding the location of a particular element in a data
structure. In other words, searching implies accessing the
values stored in the data structure.
-
3. Inserting :
Insertion is the operation in which a new data is added at a
particular place in a data structure. In some situation, where
the elements in the data structure are in a particular order, the
position may need to be identified first and then the insertion
is to be done.
-
4. Deleting : Deletion is the operation in which a particular
element is removed from the data structure. The deletion is
performed either by mentioning the element itself or by
specifying its position.
-
5. Sorting :
We are familiar with the sorting of an array using two
methods named bubble sort and selection sort. It is
the technique of arranging the elements in a
specified order, i.e., either in ascending or
descending order. Sorting of elements in a data
structure makes searching faster.
-
6. Merging : Merging usually refers to the process of
combining elements of two sorted data structures to
form a new one.
Stack -
-
In Data structure Last element is removed first . This
organising principle is known as Last-In-First-Out
(LIFO).
-
-
Underflow ~
Attempt to remove an item from an empty stack or queue .
1.Push OPERATION STACK
Push operation is the process
of inserting a new data item
into the stack .
OP
; -
=T
T TOP=-
⑮
Start - I
1: If (TOS<N) Then
2: 𝑇𝑂𝑆 = 𝑇𝑂𝑆 + 1 \\ Space avallability checking (Overflow)
TOS = -I
3:
4: Else
STACK[TOS] = VAL
STACKIOS=T
5: Print "Stack Underflow"
Go Of
=
6: End of If STACk[l]
End -I
-
124 Tos = -It -
ocS = 5-1
New New New New New
Item Item Item Item Item
9 9 9 9 9
25 34 18 56
8 8 8 8 8
7 7 7 7 7
New New New New New
Item 6 Item 6 Item 6 Item 6 Item 6
3 0 1 2 3
5 5 5 5 5
4 4 4 4 4
3 3 3 3 TOS 3 56
2 2 2 TOS 2 18 2 18
1 1 TOS 1 34 1 34 1 34
0 TOS 0 25 0 25 0 25 0 25
Stack Stack Stack Stack Stack
TOS
Empty Stack Added 1st Stack Added 2nd Stack Added 3rd Stack Added 4th Stack
s
2
1.POP OPERATION
The process of deleting an
element from the top of a
stack is called Pop operation
= / TOC = 1 1
ros= -
-
Start
1: If (TOS ⟩ − 1) Then P -
2: VAL = STACK [TOS] \\ Empty status checking (Underflow) -
3: TOS = TOS -1 -
4: Else jus = -
⑯
5: Print "Stack Underflow"
6: End of If
End
7 7 7
New New New
Item 6 Item 6 Item 6
2 1 0
5 5 5
4 4 4
3 56 3 3
TOS 2 18 2 18 2
1 34 TOS 1 34 1 34
0 25 0 25 TOS 0 25
Stack Stack STK[10]
After the retrievel of After the retrievel of After the retrievel of
Item TOS becomes 2 Item TOS becomes 1 Item TOS becomes 0
C++ Functions for Stack Operations
The Variables tos and n are assumed as global variables
Front Rear
0 4
QUEQUE 16 38 9 65
0 1 2 3 4 5 6 7 8 9
Operations on queue
1. Insertion -
2. Deletion -
1. Insertion
Insertion is the process of adding a new item into a queue at the
rear end. The value of Rear is incremented first to point to the
next location and the element is inserted at that position.
Algorithm: To perform INSERTION operation in a queue
Consider an array QUEUE[N] that implements a queue, where N is the maximum size of
the array. The variables FRONT and REAR keep track of the front and rear positions of the
queue. A data item available in the variable VAL is to be inserted into the stack. The steps
required for insertion operation are given between the Start and Stop instructions.
Start
1:
2:
If ( REAR = −1) Then itikil
FRONT = REAR = 0 //Empty status checking
,
3: Q [REAR] =VAL
~ 4: Else If ( REAR < N) Then //Space availability checking
5: -
REAR = REAR -
+1
0 1 2 3 4 5 6 7 8 9
New Item
Front 25 Rear
0 0
QUEQUE 25 1st term is
inserted
0 1 2 3 4 5 6 7 8 9
New Item
Front 16 Rear
0 1
QUEQUE 25 16 2st term is
inserted
0 1 2 3 4 5 6 7 8 9
New Item
Front 38 Rear
0 2
QUEQUE 25 16 38 3st term is
0 1 2 3 4 5 6 7 8 9 inserted
2. Deletion
Deletion operation is the removal of the item at the front end. After
the deletion the value of Front is incremented by 1.
Algorithm: To perform DELETION operation in a queue
Start =
1: If (FRONT >-1 AND FRONT < REAR) Then
2: 𝑉𝐴𝐿 = 𝑄[𝐹𝑅𝑂𝑁𝑇] //Empty status checking
3: FRONT = FRONT +1
4: Else // Checking the deletion of last element
Retrieved Item
Front 38 Rear
2 2
QUEQUE 16 38 3st term is
0 1 2 3 4 5 6 7 8 9 retrieved
C++ Functions for Queue Operations
The Variables n, front and rear are assumed as global variables
Front Rear
0 4
-
QUEQUE 87 42 64 71s E
0 1 2 3 4 5 6 7 8 9
which six deletion operations are done. At present, there are only
three elements. Obviously, the value of Front is 6 and that of
Rear is 8.
Even though the first six locations are free, we can insert only one
element according to the insertion algorithm. Imagine the worst
situation that there is only one element at the last position of the
queue (say at 9). If we try an insertion operation in this queue, there
will be overflow situation. This limitation of linear queue can be
overcome by circular queue.
In Circular Queue , it follows FIFO principle and last position is
connected back to the first position to make a circle .
56 29 56 29
3 4 3 4
47 35 47 35
2 5 2 5
1 6 1 6
13
**
0 7 0 7
25
47 35 47 35
2 5 2 5
1 6 1 6
87 87
O0 7
-
0 ↓ 7
64 18 64
-
↑
⑫o
Rear gets its highest value
Rear gets the lower
bound value
Assume that two deletions are performed at this stage.
That is, the first node in the list contains the first data item and
the address of the second node
The second node contains the second data and the address of
the third node; and so on.
The last node at a particular stage contains the last data in the
list and a null pointer
-
struct Node SONU 1200 I -
{
-
1000
2 -
int data;
NIVED 1300
1200
Node *link;
-
SNEHA 1500 3-
}; O 1300
---
S
NIKETH
1500
NULL
-
Eg :
struct Node
{
char data[10];
Node *link;
}
Operations of Linked List
1. Creation of linked list
2. Traversing a linked list
3. Inserting in a linked list
4. Deleting from Linked list
1. Creation of linked list
Step 4: If it is not the first node, store its address in the link part
of the previous node.
Step 1: Get the address of the first node from Start and store it in
Temp.
Step 2: Using the address in Temp, get the data of the first node
and store in Val.
Step 3: Also get the content of the link part of this node (i.e., the
address of the next node) and store it in Temp.
Step 2: Store the data and link part of this node using Temp.
Step 4: Copy the content of Temp (address of the new node) into the link
part of node at position (POS-1), which can be accessed using PreNode.
http://google.co.in:80
-
-
- -
-
-
-
-
- -
- -
- -
~
-
- - -
-
- & -
-
- -
-
- -
-
-
-
- -
-
-
-
- -
- -
-
Cascading Style Sheet
-
Embedded CSS codes are placed within the <head>part of the web
page. -
-
-Linked CSS implementation is done using an external file with the file
extension .css that contains only CSS code and is linked with the web
-
page.
Basic concepts of HTML documents
HTML is the most widely used language to write web pages.
HTML tags
attributes
~
-
- -
--
-
W -
We can use either the upper or lower case or even a mix of the two.
Tags in HTML document
Tags are the commands used in the HTML document that tell
web browsers how to format and organize our web pages to show the
contents.
Every tag consists of a tag name enclosed between the angle
brackets '<' and '>’.
<html> </html>
Container tags and empty tags -
Tags that require opening tag as well as closing tag are known as
-
container tags.
Eg:- <body>………</body>
--
Tags do not require closing tag. Such tags are known as empty tags.
-
Eg:-<BR>,<HR>,<IMG>
-
Attributes of tags
Certain parameters are frequently included within the opening
tag to provide additional information such as colour, measurement,
location, alignment or other appearances to the web browser. These
parameters are called attributes.
“In HTML, the value can be given in single quotes or double quotes
(i.e., attribute='value' or attribute="value").”
Eg:-<BODY Bgcolor=“Yellow’’>………..</BODY>
HTML Elements
A pair of tags and the content enclosed between these tags are
known as an element.
Creating an HTML document
Text editors like Geany, Gedit, TextPad, Notepad, Notepad++, etc. can
be used to create HTML documents.
Extension .html or .htm (for example, sample.html).
-
The main attributes of the <HTML> tag are Dir and Lang.
-
Dir
specifies the direction of the text to be displayed on the web
page. -
--
The value of Bgcolor attribute can be given in two ways.
Color_name - specifies the background colour with a colour name
(like "red", "grey" etc.)
-
Link, Alink and Vlink
-
Link: This attribute specifies the colour of the hyperlinks that are not
visited by the viewer. The default colour for Link attribute is blue.
~
Alink: It specifies the colour of the active hyperlink. The link remains
active only for the moment the mouse is clicked on it. Hence at the
time of selection the colour of the link will be changed to Alink value.
The default Alink colour is green.
-
Vlink: It specifies the colour of the hyperlink which is already visited by
the viewer. The default colour for Vlink is purple.
Leftmargin and Topmargin
The margin refers to the blank area left from the edge of the
page.
Some common tags
Heading tags
<H1></H1>, <H2></H2>, <H3></H3>, <H4></H4>, <H5></H5>,
<H6></H6>.
“The Align attribute sets the alignment of the text in the paragraph
with the values left, right, center or justify.”
- Inserting line break
using tag <br>
creating horizontal line
use tag <HR>
Attributes Size and Width.
<HR>is an empty tag.
Other attributes Align , Noshade and Color
Centering the content
Use the tag <center> and </center>
Text formatting tags
• <B> -Making text bold -
-
• <i>- Italicising the text
• <u>- Underlining the text L-
-
- -
He Suy So <sub > 2
It <subsa < /sub)
/sub>
2
(𝑎 + 𝑏) = a + 2ab+ 𝑏
- -
-
---
- ----
• <BLOCKQUOTE> and <Q>tags - Indenting a quotation
<PRE>- Displaying preformatted text
Suppose we want to display the content as we entered in the
text editor.
Normally the browser delimited the white spaces, new line
characters, the tab spaces, etc.
<ADDRESS>- Displaying the address
<ADDRESS> tag defines the contact information for the
author/owner of a document or an article. The content of this tag can
include name, phone numbers, PIN numbers, e-mail addresses, etc.
Most of the browsers display the texts in italics.
<MARQUEE>- Displaying text in a scrolling Marquee
<MARQUEE> displays a piece of text or image scrolling horizontally
or vertically in the web page.
--
Setting space for the image
attributes are:-
Width and Height
Vspace and Hspace
Align
Setting border around an image
Use Border attributes
Alt attribute
It specify an alternate text for an image.