0% found this document useful (0 votes)
57 views25 pages

PL - Unit 1

The document provides an overview of programming languages, detailing their characteristics, types, and the role of programming language translators like compilers, assemblers, and interpreters. It discusses low-level, high-level, and middle-level programming languages, along with examples and their advantages and disadvantages. Additionally, it covers elementary data types and the importance of syntax and semantics in programming languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views25 pages

PL - Unit 1

The document provides an overview of programming languages, detailing their characteristics, types, and the role of programming language translators like compilers, assemblers, and interpreters. It discusses low-level, high-level, and middle-level programming languages, along with examples and their advantages and disadvantages. Additionally, it covers elementary data types and the importance of syntax and semantics in programming languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT-1

Introduction: Syntactic and semantic rules of a Programming language, Characteristics of a good


programming language, Programming language translator’s compiler & interpreters, Elementary
data types – data objects, variable & constants, data types, Specification & implementation of
elementary data types, Declarations, type checking & type conversions, Assignment & initialization,
Numeric data types, enumerations, Booleans & characters.

Introduction to Programming Language: -

A programming language is a computer language that is used by programmers (developers) to


communicate with computers. It is a set of instructions written in any specific language ( C, C++, Java,
Python) to perform a specific task.

A programming language is mainly used to develop desktop applications, websites, and mobile
applications.

Some of the key features of programming languages include:

1. Syntax: The specific rules and structure used to write code in a programming language.
2. Data Types: The type of values that can be stored in a program, such as numbers, strings, and
booleans.
3. Variables: Named memory locations that can store values.
4. Operators: Symbols used to perform operations on values, such as addition, subtraction, and
comparison.
5. Control Structures: Statements used to control the flow of a program, such as if-else statements,
loops, and function calls.
6. Libraries and Frameworks: Collections of pre-written code that can be used to perform common
tasks and speed up development.
7. Paradigms: The programming style or philosophy used in the language, such as procedural, object-
oriented, or functional.
Examples of popular programming languages include Python, Java, C++, JavaScript, and Ruby.

Types of programming language

1. Low-level programming language

Low-level language is machine-dependent (0s and 1s) programming language. The processor runs low-
level programs directly without the need of a compiler or interpreter, so the programs written in low-level
language can be run very fast.

Low-level language is further divided into two parts -

Page 1 of 25
i. Machine Language

Machine language is a type of low-level programming language. It is also called as machine code or object
code. Machine language is easier to read because it is normally displayed in binary or hexadecimal form
(base 16) form. It does not require a translator to convert the programs because computers directly
understand the machine language programs.

The advantage of machine language is that it helps the programmer to execute the programs faster than the
high-level programming language.

ii. Assembly Language

Assembly language (ASM) is also a type of low-level programming language that is designed for specific
processors. It represents the set of instructions in a symbolic and human-understandable form. It uses
an assembler to convert the assembly language to machine language.

The advantage of assembly language is that it requires less memory and less execution time to execute a
program.

2. High-level programming language

High-level programming language (HLL) is designed for developing user-friendly software programs
and websites. This programming language requires a compiler or interpreter to translate the program into
machine language (execute the program).

The main advantage of a high-level language is that it is easy to read, write, and maintain.

High-level programming language includes Python, Java, JavaScript, PHP, C#, C++, Objective C,
Cobol, Perl, Pascal, LISP, FORTRAN, and Swift programming language.

A high-level language is further divided into three parts -

i. Procedural Oriented programming language

Procedural Oriented Programming (POP) language is derived from structured programming and based upon
the procedure call concept. It divides a program into small procedures called routines or functions.

Procedural Oriented programming language is used by a software programmer to create a program that can
be accomplished by using a programming editor like IDE, Adobe Dreamweaver, or Microsoft Visual
Studio.

The advantage of POP language is that it helps programmers to easily track the program flow and code can
be reused in different parts of the program.

Example: C, FORTRAN, Basic, Pascal, etc.

ii. Object-Oriented Programming language

Object-Oriented Programming (OOP) language is based upon the objects. In this programming
language, programs are divided into small parts called objects. It is used to implement real-world
entities like inheritance, polymorphism, abstraction, etc in the program to makes the program resusable,
efficient, and easy-to-use.

Page 2 of 25
The main advantage of object-oriented programming is that OOP is faster and easier to execute, maintain,
modify, as well as debug.

Example: C++, Java, Python, C#, etc.

3. Middle-level programming language

Middle-level programming language lies between the low-level programming language and high-level
programming language. It is also known as the intermediate programming language and pseudo-language.

A middle-level programming language's advantages are that it supports the features of high-level
programming, it is a user-friendly language, and closely related to machine language and human language.

Example: C, C++, language

Most commonly used Programming Language

As we all know, the programming language makes our life simpler. Currently, all sectors (like education,
hospitals, banks, automobiles, and more ) completely depend upon the programming language.

There are dozens of programming languages used by the industries. Some most widely used programming
languages are given below -

1. Python

Python is one of the most widely used user-friendly programming languages. It is an open-source and easy
to learn programming language developed in the 1990s. It is mostly used in Machine learning, Artificial
intelligence, Big Data, GUI based desktop applications, and Robotics.

Advantages

o Python is easy to read, easy to understand, and easy to write.


o It integrates with other programming languages like C, C++, and Java.
o Python executes code line-by-line, so it is easy for the programmer to find the error that occurred
in the code.
o Python is platform-independent means you can write code once and run it anywhere.

Disadvantages

o Python is not suitable for developing mobile applications and games.


o Python works with the interpreter. That's why it is slower than other programming languages like
C and C++.

2. Java

Java is a simple, secure, platform-independent, reliable, architecture-neutral high-level programming


language developed by Sun Microsystems in 1995. Now, Java is owned by Oracle. It is mainly used to

Page 3 of 25
develop bank, retail, information technology, android, big data, research community, web, and desktop
applications.

Advantages

o Java is easy to write, compile, learn, and debug as compared to other programming languages.
o It provides an ability to run the same program on different platforms.
o It is a highly secured programming language because in java, there is no concept of explicit pointers.
o It is capable of performing multiple tasks at the same time.

Disadvantages

o Java consumes more memory and slower than other programming languages like C or C++.
o It does not provide a backup facility.

3. C

C is a popular, simple, and flexible general-purpose computer programming language. Dennis M


Ritchie develops it in 1972 at AT&T. It is a combination of both low-level programming language as well
as a high-level programming language. It is used to design applications like Text Editors, Compilers,
Network devices, and many more.

Advantages

o C language is easy to learn.


o It is fast, efficient, portable, easy to extend, powerful, and flexible programming language.
o It is used to perform complex calculations and operations such as MATLAB.
o It provides dynamic memory allocation to allocate memory at the run time.

Disadvantages

o In the C programming language, it is very difficult to find the errors.


o C does not support the concepts of constructors, destructors, abstraction, polymorphism,
encapsulation, and namespace like OOPs.

4. C++

C++ is one of the thousands of programming languages that we use to develop software. C++ programming
language is developed by Bjarne Stroustrup in 1980. It is similar to the C programming language but also
includes some additional features such as exception handling, object-oriented programming, type
checking, etc.

Advantages

o C++ is a simple and portable structured programming language.

Page 4 of 25
o It supports OOPs features such as Abstraction, Inheritance, Encapsulation.
o It provides high-level abstraction and useful for a low-level programming language, and more
efficient for general-purpose.
o C++ is more compatible with the C language.

Disadvantages

o C++ programming language is not secured as compared to other programming languages like Java
or Python.
o C++ can not support garbage collection.
o It is difficult to debug large as well as complex web applications.

Advantages of programming languages:

1. Increased Productivity: Programming languages provide a set of abstractions that allow


developers to write code more quickly and efficiently.
2. Portability: Programs written in a high-level programming language can run on many different
operating systems and platforms.
3. Readability: Well-designed programming languages can make code more readable and easier to
understand for both the original author and other developers.
4. Large Community: Many programming languages have large communities of users and
developers, which can provide support, libraries, and tools.

Disadvantages of programming languages:

1. Complexity: Some programming languages can be complex and difficult to learn, especially for
beginners.
2. Performance: Programs written in high-level programming languages can run slower than
programs written in lower-level languages.
3. Limited Functionality: Some programming languages may not have built-in support for certain
types of tasks or may require additional libraries to perform certain functions.
4. Fragmentation: There are many different programming languages, which can lead to
fragmentation and make it difficult to share code and collaborate with other developers.

Syntactic and semantic rules of a Programming language

 It is a set of rules that defines what sequences of symbols are considered to be valid expression in
the language.
 A valid representation of syntax
Page 5 of 25
X=Y+Z

 Invalid representation may be

XY+-

2+3 *4 text will be interpreted this expression as having value 14 and not 20. That is, expression is
interpreted as if written 2 + ( 3 * 4 ) and it is not interpreted as if written (2+3)*4.

Points:

 It refers the rules and regulations for writing any statement in a programming language.
 A statement is syntactically valid if it follows all the rules.
 It is a grammar and structure of any programming Language.
 Ex, In C language the output is presented using printf ( ) statement whose syntax is:

printf(“ This is our first program”);

 Semantic is the meaning of an expression (program) in a programming language.


 In C, 10 elements array A of integer has declaration:

int A [10];

In Pascal,

type temperature = array [ -10…..50] of real;

var day_temp, night temp = temperature;

 Both creates similar data objects at run – time, their syntax is very different.

Page 6 of 25
Characteristics of a good programming Language

 Simplicity: the language must offer clear and simple concepts that are easy to understand,
facilitating learning and application. But simplicity can be a difficult balance to strike without
compromising the overall capability of the language.
 Capability: apart from being easy to use, the language must be well-equipped with a robust set of
features to perform a wide range of tasks. If a programming language was designed to be used in a
specific area, it must provide the necessary means (operators, structures, and syntax) to achieve
ideal results.
 Abstraction: it is the language’s ability to define and use complicated structures or operations while
ignoring certain low level details.
 Efficiency: programming languages that can be translated and executed efficiently help avoid the
excessive consumption of memory and time.
 Structuring: the language allows programmers to write their code according to structured
programming concepts to avoid creating errors.
 Compactness: a language with this characteristic can express operations concisely without having
to write too many details.
 Principle of Locality: also known as the locality of reference, this phenomenon describes a
computer program’s preference for continually accessing the same areas of memory over a short
span of time. By enabling the usage of loops and subroutines, a programming language can exploit
the principle of locality for optimising the overall performance of an application.

Programming Language Translators

A program written in high-level language is called as source code. To convert the source code into machine
code, translators are needed. A translator takes a program written in source language as input and converts
it into a program in target language as output. It also detects and reports the error during translation.

Roles of translator are:

• Translating the high-level language program input into an equivalent machine language program.

• Providing diagnostic messages wherever the programmer violates specification of the high-level
language program.

1. Compiler
The language processor that reads the complete source program written in high-level language as a whole
in one go and translates it into an equivalent program in machine language is called a
Compiler. Example: C, C++, C#.

Page 7 of 25
In a compiler, the source code is translated to object code successfully if it is free of errors. The compiler
specifies the errors at the end of the compilation with line numbers when there are any errors in the source
code. The errors must be removed before the compiler can successfully recompile the source code again
the object program can be executed number of times without translating it again.

2. Assembler
The Assembler is used to translate the program written in Assembly language into machine code. The
source program is an input of an assembler that contains assembly language instructions. The output
generated by the assembler is the object code or machine code understandable by the computer.
Assembler is basically the 1st interface that is able to communicate humans with the machine. We need
an assembler to fill the gap between human and machine so that they can communicate with each other.
code written in assembly language is some sort of mnemonics(instructions) like ADD, MUL, MUX, SUB,
DIV, MOV and so on. and the assembler is basically able to convert these mnemonics in binary code.
Here, these mnemonics also depend upon the architecture of the machine.
For example, the architecture of intel 8085 and intel 8086 are different.

3. Interpreter
The translation of a single statement of the source program into machine code is done by a language
processor and executes immediately before moving on to the next line is called an interpreter. If there is
an error in the statement, the interpreter terminates its translating process at that statement and displays
an error message. The interpreter moves on to the next line for execution only after the removal of the
error. An Interpreter directly executes instructions written in a programming or scripting
language without previously converting them to an object code or machine code. An interpreter translates
one line at a time and then executes it.

Page 8 of 25
Example: Perl, Python and Matlab.

Elementary Data Types-Data Objects


An elementary data object includes a single data value and a class of elementary data objects with a
set of operations for creating and manipulating them is represented as an elementary data type.
Elementary data types, also known as primitive data types, are the basic building blocks of data in
programming languages. They represent the simplest and most fundamental types of data objects that
can be manipulated by a programming language. The specific elementary data types can vary between
programming languages, but here are some common examples:
Integer Data Types

Integers represent whole numbers without any fractional or decimal parts. In C++, there are several
integer data types available, varying in size and range. Some commonly used integer data types are:

 int: This is the most commonly used integer type, typically represented by 4 bytes, capable of storing
values from -2,147,483,648 to 2,147,483,647.

Page 9 of 25
 short: Also known as a short integer, it is typically represented by 2 bytes and has a smaller range
compared to int.
 long: A long integer is represented by 4 bytes, similar to int, but with a larger range. It can store
values from -2,147,483,648 to 2,147,483,647.
 long long: This data type is represented by 8 bytes and has the largest range among the integer types,
capable of storing values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
These elementary data types provide the basic foundation for storing and manipulating different kinds
of data in programming languages. They form the basis for more complex data structures and user-
defined data types.

Floating-Point Data Types

Floating-point data types in C++ are used to represent numbers with decimal points or fractions.
These data types are ideal for handling scientific calculations, financial applications, and any
situation that requires precise decimal representation. The two main floating-point data types in C++
are:

 float: This data type represents single-precision floating-point numbers, typically occupying 4 bytes
in memory.
 double: The double data type is for double-precision floating-point numbers, which offer higher
precision compared to float. It is usually represented by 8 bytes.
Character Data Type

The character data type in C++ is used to store individual characters such as letters, digits, or symbols.
It allows programmers to work with textual data and perform operations like string manipulation.
The character data type is denoted by the keyword char and occupies 1 byte of memory.

Boolean Data Type

The boolean data type is a fundamental type in C++ used to represent logical values. It can have one
of two values: true or false. Boolean variables are commonly used in conditional statements and
logical operations to make decisions and control the flow of a program.

Data Objects

Data objects are fundamental entities in programming that store and represent data. They serve as
containers for information and enable the manipulation and processing of that data within a program.
Data objects can range from simple variables that hold individual values, such as integers or strings,
to more complex structures like arrays or objects. These objects may have multiple attributes or
properties that provide a way to organize and access related data. Data objects are used to store and
manage various types of information, including user input, application state, configuration settings,
Page 10 of 25
and more. They play a crucial role in data storage, representation, input/output operations, data
processing, and sharing within a program. By utilizing data objects effectively, programmers can
organize and manipulate data to achieve desired outcomes and build robust and functional software
systems.

Declaring Data Objects

To create a data object in C++, you need to declare a variable and specify its data type. For example,
to declare an integer data object named age, you would use the following syntax:

int age;

Similarly, you can declare data objects of other types, such as float, char, or custom class types. The
declaration informs the compiler about the existence of the variable and reserves memory space for
it.

Initializing Data Objects

Initializing a data object involves assigning an initial value to the variable when it is declared. By
initializing data objects, you can ensure they have meaningful values from the start. Here’s an
example of initializing the age variable with the value 25:

int age = 25;

You can also initialize data objects using constructor functions or other initialization methods
depending on the type of object.

Modifying Data Objects

Once a data object is declared and initialized, you can modify its value as needed. To change the
value of a data object, you simply assign a new value to it using the assignment operator ( =). For
instance, to update the age variable to 30, you would write:

age = 30;

Modifying data objects is essential for updating variables during program execution, capturing user
input, or performing calculations based on other data objects.

Accessing Data Objects

To utilize the value stored in a data object, you can access it by referencing the variable name. For
example, to use the value of the age variable in a program’s logic or calculations, you can refer to it
directly:

Page 11 of 25
Accessing data objects allows you to retrieve and work with the stored data throughout your
program’s execution.

Variables

Variables are names given to computer memory locations in order to store data in a program. This data can be
known or unknown based on the assignment of value to the variables.

Variables can also be considered as ‘containers’ which are used to hold more than one value. Their sole
purpose is to label and store data in the memory and then this Variable can be accessed throughout the program
whenever needed. Generally, the variable name is used as a reference to access the stored value.

Creating Variables

The process of creating a variable is also known as Declaration of Variable. Every programming language has
its own way of declaring a variable. Declaration of a variable does 2 things :

1. It tells the compiler what the variable name is.

2. It specifies what type of data the variable will hold.

In general, the declaration consists of 3 parts – the variable type, variable label/name, and semi-colon to end
the declaration.

Syntax – data_type variable_name = value ;

For example –

int speed; float sum = 0;

There are a few conventions which are needed to be followed while declaring Variables –

Page 12 of 25
1. Variable names must begin with a letter, underscore, non-number character. Each language has its own
conventions.

2. Few programming languages like PHP, Python, Perl, etc. do not require to specify data type at the start.

3. Do not use a comma with numbers.

4. Once a data type is defined for the variable, then only that type of data can be stored in it. For example,
if the variable is declared as Int, then it can only store integer values.

5. A variable name once defined can only be used once in the program. You cannot define it again to store
other types of value.

6. Built-in Data types are frequently used to declare the data type of the Variable.

Assigning Values to Variables

 We use the = sign to assign values to the declared variables. The variable name and type will come on
the left side, while the value to be stored will be on the right side.

For example – int score = 45;

 You can also get the value to be stored in the variable through user input methods.

Types of Variables

There are a total of 5 different variable types in the programming language. Those are as follows:

1. Constant Variables

Constant Variables are used to store data which is not needed to be changed throughout the program. The data
cannot be modified in such a type of variable.

2. Global Variables

Global variables are declared outside of a function and can be accessed from any part of the program until the
execution. In Ruby, the $ sign is used before the variable name to make it global whereas in Python, the
‘global’ keyword is used.

3. Class Variables

Page 13 of 25
These variables are accessed within the class only. Class variables must be declared at the class level. They are
also called Static Variables as there can only be one copy of each class variable per class.

4. Instance Variables

Instance variables are declared inside the class, but outside a method.

5. Local Variables

They are the most commonly used Variables that are declared in methods, class, instances. These variables can
be accessed from within a block/part of the code.

Constants

Constant is an entity that refers to a fixed value of data and which cannot be modified. During
execution/computation of programming, the value of a constant cannot be altered, it remains constant.
The most common example which can be considered to understand the concept of a constant is “PI”
PI = 3.1415927 whose value is universal and does not change.

Methods of Defining a Constant in Program


The keyword used to define a constant in a program is – const
For example –
const int i = 10; // i is a constant with value 10 which cannot be modified throughout the execution life
cycle of a program.
const float PI = 3.1415927; //PI value defined as a constant
Another method of defining a constant is by using the #define preprocessor directive.
Syntax – #define identifiername value //where identifiername = name given to constant
For example –
#define val 10
#define char ‘G’

Page 14 of 25
Datatype
A data type is a class of data objects with a set of operations for creating and manipulating them.
Examples of elementary data types:
integer, real, character, Boolean, enumeration, pointer.

Specification of elementary data types

Attributes are the particular data type for any data object that distinguish data objects of that type
Data type of the object
stored in a descriptor and used during the program execution.
used only to determine the storage representation, not used explicitly during execution.
int, char Boolean are the attributes for a particular data object of the define data types.
Values that data object of that type may have
Determined by the type of the object.
Attributes of every data type are associated with particular value which can be assigned to
them at run time or at compile time.
These values are not fixed.
Usually an ordered set, i.e. it has a least and a greatest value.

Operations that define the possible manipulations of data objects of that type.
Primitive - specified as part of the language definition
Programmer-defined (as subprograms, or class methods)
An operation is defined by:

Domain - set of possible input arguments


Range - set of possible results
Action - how the result is produced
The domain and the range are specified by the operation signature
the number, order, and data types of the arguments in the domain,

Page 15 of 25
the number, order, and data type of the resulting range
mathematical notation for the specification:
op name: arg type x arg type x … x arg type ® result type

The action is specified in the operation implementation

Subtypes: A data type that is part of a larger class.


Examples: in C, C++ int, short, long and char are variations of integers. The operations available to the larger class
are available to the subtype. This can be implemented using inheritance.
Implementation of a data type
Storage representation
 Influenced by the hardware
 Described in terms of:
Size of the memory blocks required
Layout of attributes and data values within the block

Two methods to treat attributes:

 determined by the compiler and not stored in descriptors during execution – C.


 stored in a descriptor as part of the data object at run time - LISP Prolog.

Implementation of operations

 Directly as a hardware operation. E.g. integer addition


 Subprogram/function, e.g. square root operation
 In-line code. Instead of using a subprogram, the code is copied into the program at the point where the
subprogram would have been invoked.

Declarations
Declarations provide information about the name and type of data objects
needed during program execution.
 Explicit – programmer defined
 Implicit – system defined
E.g. in FORTRAN - the first letter in the name of the variable determines the type
Perl - the variable is declared by assigning a value
$abc = 'a string' $abc is a string variable
$abc = 7 $abc is an integer variable

Operation declarations: prototypes of the functions or subroutines that are programmer-defined.

Examples:
Page 16 of 25
declaration: float Sub(int, float)
signature: Sub: int x float --> float

Purpose of declaration

 Choice of storage representation


 Storage management
 Declaration determines the lifetime of a variable, and allows for more efficient memory usage.
 Specifying polymorphic operations.
Depending on the data types operations having same name may have different meaning, e.g. integer
addition and float addition
In most language +, -. *, / are overloaded
Ada - aloows the programmer to overload subprograms
ML - full polymorphism
Note: Declarations provide for static type checking.

Types of Checking

 Type checking is used to check the correctness of the program before its execution.
 Type checking is the process of verifying and enforcing constraints of types in values.
 A compiler must check that the source program should follow the syntactic and semantic conventions
of the source language.
 The main purpose of type-checking is to check the correctness and data type assignments and type-
casting of the data types, whether it is syntactically correct or not before their execution.
 It should also check the type rules of the language.

The design of the type-checker depends on:


1. Syntactic Structure of language constructs.
2. The Expressions of languages.
3. The rules for assigning types to constructs (semantic rules).
Types of Type Checking:
There are two kinds of type checking:
1. Static Type Checking.
2. Dynamic Type Checking.

1. Static Type Checking:


 Static type checking is defined as type checking performed at compile time.

Page 17 of 25
 It checks the type variables at compile-time, which means the type of the variable is known at the
compile time.
 It generally examines the program text during the translation of the program.
 Using the type rules of a system, a compiler can infer from the source text that a function (fun) will
be applied to an operand (a) of the right type each time the expression fun(a) is evaluated.
 Languages like Pascal and C have static type checking.
Static Type-Checking is also used to determine the amount of memory needed to store the variable.
Examples of Static checks include:
 Type-checks: A compiler should report an error if an operator is applied to an incompatible operand.
For example, if an array variable and function variable are added together.
 The flow of control checks: Statements that cause the flow of control to leave a construct must have
someplace to which to transfer the flow of control. For example, a break statement in C causes control
to leave the smallest enclosing while, for, or switch statement, an error occurs if such an enclosing
statement does not exist.
 Uniqueness checks: There are situations in which an object must be defined only once. For example,
in Pascal an identifier must be declared uniquely, labels in a case statement must be distinct, and else
a statement in a scaler type may not be represented.
 Name-related checks: Sometimes the same name may appear two or more times. For example, in
Ada, a loop may have a name that appears at the beginning and end of the construct. The compiler
must check that the same name is used at both places.
Benefits of Static Type Checking:
1. Runtime Error Protection.
2. It catches syntactic errors like spurious words or extra punctuation.
3. It catches wrong names like Math and Predefined Naming.
4. Detects incorrect argument types.
5. It catches the wrong number of arguments.
6. It catches wrong return types, like return “70”, from a function that’s declared to return an int.
Disadvantages:
1. Difficult to debug.
2. Type information must be kept during execution
3. Software implementation required as most hardware does not provide support
2. Dynamic Type Checking:
 Dynamic Type Checking is defined as the type checking being done at run time.
 In Dynamic Type Checking, types are associated with values, not variables.
 Implementations of dynamically type-checked languages runtime objects are generally associated
with each other through a type tag, which is a reference to a type containing its type information.
Page 18 of 25
 Dynamic typing is more flexible.
 A static type system always restricts what can be conveniently expressed. Dynamic typing results in
more compact programs since it is more flexible and does not require types to be spelled out.
 Programming with a static type system often requires more design and implementation effort.
Type Conversions

 Type conversion is the process of converting one data type to another.


 The type conversion is only performed to those data types where conversion is possible.
 Type conversion is performed by a compiler.
 In type conversion, the destination data type can’t be smaller than the source data type.
 Type conversion is done at compile time and it is also called widening conversion because the
destination data type can’t be smaller than the source data type.
There are two types of Conversion:
1. Implicit Type Conversion
2. Explicit type Conversion

1. Implicit Type Conversion


 It is also known as ‘automatic type conversion’.
 Done by the compiler on its own, without any external trigger from the user.
 It takes place when in an expression more than one data type is present. In such conditions type
conversion (type promotion) takes place to avoid loss of data.

Example
int main ( )
{
int x = 10; // integer x
char y = 'a'; // character c
x = x + y; // y implicitly converted to int. ASCII value of 'a' is 97
float z = x + 1.0; // x is implicitly converted to float
printf("x = %d, z = %f", x, z);
return 0;
}
Output

x = 107, z = 108.000000

2. Explicit Type Conversion

Page 19 of 25
 This process is also called type casting and it is user-defined.
 The user can typecast the result to make it of a particular data type.

Syntax in C Programming

(type) expression

Type indicated the data type to which the final result is converted.

Example

int main ()
{
double x = 1.2;
int sum = (int)x + 1; // Explicit conversion from double to int
printf("sum = %d", sum);
return 0;
}
Output
sum = 2

Numeric data Types

Data Types

Programming uses a number of different data types. A data type determines what type of value an object
can have and what operations can be performed.

Numeric Data Types

The second most important data type is numeric data. There are several different ones.

An integer is a numeric value without a decimal. Integers are whole numbers and can be positive or
negative. Sometimes a distinction is made between short and long integers, referring to how much data
storage is used for the number. A short integer is typically stored using 16 bits, which means you can
store up to 2^16, or 65,536, unique values. For any number larger than that, you would need to use a long
integer, which uses 32 bits or more.

A number with a decimal is referred to as a decimal, a float or a double. The terminology varies
somewhat with the programming language being used. The term 'float' comes from floating point, which
means you can control where the decimal point is located. The term 'double' refers to using double the

Page 20 of 25
amount of storage relative to a float. Working with numbers in code is a little bit like using a calculator.
Here is an example of how numeric values are used in code, in this case using a multiplication character:

result = 3 * 117.89

The value stored in the 'result' would be 353.67.

Boolean Data

The Boolean data type can only represent two values: true or false. Typically, a 1 is used to represent
True, and a 0 is used to represent False. Consider the following example where a user inputs two values
and the program determines whether the first one is smaller than the second one or not.

In this example the first value is in fact not smaller than the second one, and the program therefore results
in a Boolean value of False. The Boolean type is the primary results of conditional statements, which are
used to control workflow in program. For example, if a particular condition is true, then do this - if the
condition is false, then do something else.

Strings

One of the most widely used data types is a string. A string consists of one or more characters, which can
include letters, numbers, and other types of characters. You can think of a string as plain text.

A string represents alphanumeric data. This means that a string can contain many different characters,
but they are all considered as if they were text, even if the characters are numbers. A string can also
contain spaces. This presents a bit of an issue. How are you going to distinguish between the value of a
string and the actual code of the program? The solution is to mark the beginning and end of a string with
a special character, typically a quote. For example, the following code is used to print text to the screen:

print 'Hello World!'

The use of quotes ensures that the text 'Hello World!' is recognized as a string and not as two separate
words that may have some special meaning in the programming language. The use of quotes also makes
it possible to use numeric characters as part of a string. For example, the following code is used to store
a street address:

Page 21 of 25
address = '123 Central Avenue'

Without the quotes, the numeric characters 123 would be interpreted as a number, but with the quotes, it
is recognized as being part of a string that represents a street address.

Assignment and Initialization

An Assignment statement is a statement that is used to set a value to the variable name in a program.
Assignment statement allows a variable to hold different types of values during its program lifespan. Another
way of understanding an assignment statement is, it stores a value in the memory location which is denoted by
a variable name.

Syntax

The symbol used in an assignment statement is called as an operator. The symbol is ‘=’.
Note: The Assignment Operator should never be used for Equality purpose which is double equal sign ‘==’.
The Basic Syntax of Assignment Statement in a programming language is :
variable = expression ;
where,
variable = variable name
expression = it could be either a direct value or a math expression/formula or a function call
Few programming languages such as Java, C, C++ require data type to be specified for the variable, so that
it is easy to allocate memory space and store those values during program execution.
data_type variable_name = value ;
Example –

int a = 50 ;
float b ;
a = 25 ;
b = 34.25f ;
In the above-given examples, Variable ‘a’ is assigned a value in the same statement as per its defined data type.
A data type is only declared for Variable ‘b’. In the 3rd line of code, Variable ‘a’ is reassigned the value 25. The
4th line of code assigns the value for Variable ‘b’.
Initialization

 Initialization is the process of assigning a value to the Variable. Every programming language has its
own method of initializing the variable. If the value is not assigned to the Variable, then the process is
only called a Declaration.

Page 22 of 25
Basic Syntax

The basic form of declaring a variable is:

type identifier [= value] [, identifier [= value]]…];

OR

data_type variable_name = value;

where,

type = Data type of the variable

identifier = Variable name

value = Data to be stored in the variable (Optional field)

Note 1: The Data type and the Value used to store in the Variable must match.

Note 2: All declaration statements must end with a semi-colon (;)

int a, b, c; // declare 3 variables.

int z = 35; // declare and initialize variable z


with value 35.

double pi = 3.14159; // declare an approximation of pi.

char x, y = ‘y’, z; // declare 3 variables, initialize char y


with value ‘y’.

char str[30]; // declare an array named ‘str’ which


holds 30 characters.

Enumerations

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral
constants, the names make a program easy to read and maintain.

Page 23 of 25
enum State {Working = 1, Failed = 0};

The keyword ‘enum’ is used to declare new enumeration types in C and C++. Following is an example
of enum declaration.

// The name of enumeration is "flag" and the constants are the values of the flag. By default, the values
of the constants are as follows:

// constant1 = 0, constant2 = 1, constant3 = 2 and so on.

enum flag{constant1, constant2, constant3, ....... };

Variables of type enum can also be defined. They can be defined in two ways:

// In both of the below cases, "day" is

// defined as the variable of type week.

enum week{Mon, Tue, Wed};

enum week day;

// Or
enum week{Mon, Tue, Wed}day;
Example
// An example program to demonstrate working of enum in C
#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}

Page 24 of 25
Output:
2
In the above example, we declared “day” as the variable and the value of “Wed” is allocated to day,
which is 2. So as a result, 2 is printed.
Booleans and Characters
The boolean data type is a type of data that stores only two types of values i.e. True or False. The
Boolean data type is used to store logic values i.e. truth values which are true or false. It takes only 1
byte of space to store logic values. Here, true means 1, and false means 0. In the Boolean data type any
value other than ‘0’ is considered as ‘true’. Boolean values are most commonly used in data structures
to decide the flow of control of a program and decision statements. In programming languages, we have
various data types to store different types of data.
Example of Declaration of Boolean Data Type
In C Language
#include <stdio.h>
#include <stdbool.h>

int main() {

bool a = true;
bool b = false;
bool c = 5;
printf("a: %d\n", a);
printf("c: %d\n", b);
printf("c: %d", c);
return 0;
}

In C++
#include <iostream>
using namespace std;

int main() {

bool a = true;
bool b = false;
bool c = 'yes';
cout<<"a: "<<a<<endl;
cout<<"b: "<<b<<endl;
cout<<"c: "<<c<<endl;
return 0;
}

Page 25 of 25

You might also like