Programming 3
Programming 3
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 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)
• 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
• 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
• 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); }
• 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.