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

Programming 3

Data types define how data is stored and used in a programming language. The basic data types include integers, floats, characters, strings, and booleans. Variables are used to store and label data in memory during a program's execution. Variables are declared with a data type and can be assigned values which may change. Common types of variables include local, global, and static variables.

Uploaded by

toobazufiqar81
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
20 views25 pages

Programming 3

Data types define how data is stored and used in a programming language. The basic data types include integers, floats, characters, strings, and booleans. Variables are used to store and label data in memory during a program's execution. Variables are declared with a data type and can be assigned values which may change. Common types of variables include local, global, and static variables.

Uploaded by

toobazufiqar81
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 25

Data Types

Lec # 03
Introduction

• Data Types are one of the fundamental concepts of programming that you
need to know! Some programming languages do the job of using the correct
data type for you.
• For example, you have a function that works with numbers, but you pass it a
string. But, how should the program know what to do with the value?
Definition

• A data type is a classification of data which tells the compiler or interpreter


how the programmer intends to use the data. Most programming languages
support various types of data, including integer, character or string, and
Boolean.
• your name – a string of characters
• your age – usually an integer
Standard Data Type

• A data type that is predefined in the language that is called Standard Data
Type.
• E.g:
Int , float , long (64 bit) ,double , Char
User-defined Data Type

• C++ also allows the user to defines his own data types known as user defined
data type.
• Can only be used in that program in which it has been defined.
Data Types
Basic Data Type
Representation Examples
Integers Whole numbers -5 , 0 , 123
Fraction numbers -87.5 , 0.0 , 3.1435
Float Point
A sequence of Characters “Hello World”
String
Logical True or false True , false
Boolean
Basic Data Types

Integer (int):
• A commonly used numeric data type is the Integer. It stores whole numbers,
for example, 1, 90, or 1999, with a maximum and minimum value determined
by the number of bits!
• byte: 8 bit (-128..127)
• short: 16bit (-32768..32767)
• int: 32 bit (-2147483648 ..2147483647)
Bits & Bytes

Bit
• Bit is the smallest unit of the computer. It is usually represented with digits 0
and 1.
Byte
• Byte is made of 8 bits. It is used in determining the system storage. Byte is
the most common term used in computing.
Float

Floats are the next numeric datatype on this list, but they contain floating-point
numbers in contrast to integers. A floating-point value, for example, is the number:
1,5.
Floating-point data types represent fractional numbers in programming. There are
two main floating-point data types, which vary depending on the number of
allowable values in the string:
• Float: A data type that typically allows up to seven points after a decimal.
• Double: A data type that allows up to 15 points after a decimal.
Character (char)

• The character is a super basic data type that stores precisely one letter, a
digit, or other symbols. It has the size of 1 byte and thus uses 8 bits.
• It is used to store a single letter, digit, punctuation mark, symbol, or blank
space.
String

• Strings are sequences of characters and they store text. As the character
data type, the string can also store digits, but in the form of text.
• A simple example of a string is: “hello world”
Boolean (bool)

• Booleans represent a single statement, true or false. In most modern


programming languages, you can assign and use the value exactly like that.
In older languages, for example, in C, booleans were stored as 0 and 1.
Array
• The Array is a list of values of a certain data type. In C, for example, strings
were stored as arrays of characters. That means that the string “hello” was
stored as the array [‘h’,’e’,’l’,’l’,o].
• In this example, we have a simple form, and you have to assign the proper
data types into the given fields.
Data Types in C/ c++

• Primitive data types are the most basic data types that are used for
Types representing simple values such as integers, float, characters, etc.
• Primitive Data Types • The user-defined data types are defined by the user himself.
• User Defined Data Types • The data types that are derived from the primitive or built-in datatypes
are referred to as Derived Data Types.
• Derived Types
Variables in Programming
Language

• In a program, the data is stored in 2 ways – Either the data is already stored
in the program, or the data comes from the user. The data is stored in both
ways. The program stores this data in the form of Variables. Every variable
has its own data type, a name, and a value assigned to it. The value of that
variable might change as time goes by, and hence the name Variable.
Definition

• 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.
Constant / variable

• A constant is a data item whose value cannot change during the program’s
execution. Thus, as its name implies – the value is constant.
• A variable is a data item whose value can change during the program’s
execution. Thus, as its name implies – the value can vary.
Creating Variable

• 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 :
• It tells the compiler what the variable name is.
• 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.
Continued……..

• data_type: Type of data that a variable can store.


• variable_name: Name of the variable given by the user.
• value: value assigned to the variable by the user.
Syntax

data_type variable_name = value ;


• For example –
• int speed;
• float sum = 0;
Steps for variables

• Variable names must begin with a letter, underscore, non-number character. Each
language has its own conventions.
• Do not use a comma with numbers.
• 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.
• A variable name once defined can only be used once in the program. You cannot
define it again to store other types of value.
Types of Variables

• Local Variables
A Local variable in C is a variable that is declared inside a function or a block of code.
• Global Variables
Global variable in C is a variable that is declared outside the function or a block of code.

• Static Variables
A static variable in C is a variable that is defined using the static keyword. It can be
defined only once in a C program and its scope depends upon the region where it is declared (can be global
or local).
void function()
{
int x = 10; // local variable
printf("%d", x); int x = 20; // global variable
}
void function1() { printf("Function 1: %d\n", x); }

void function2() { printf("Function 2: %d\n", x); }

static data_type variable_name = initial_value;


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.

You might also like