0% found this document useful (0 votes)
29 views

CPP-Lecture-05 ..

This document provides an overview of programming fundamentals related to variables and data types in C++. It discusses what variables are, how they are declared and initialized, and the different data types including integral, floating point, character, boolean, and string types. For each data type, it provides examples of declarations and valid value assignments. It also shows how to output the values of different data types using cout and discusses the size of each type when reserved in memory.

Uploaded by

jiamah2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

CPP-Lecture-05 ..

This document provides an overview of programming fundamentals related to variables and data types in C++. It discusses what variables are, how they are declared and initialized, and the different data types including integral, floating point, character, boolean, and string types. For each data type, it provides examples of declarations and valid value assignments. It also shows how to output the values of different data types using cout and discusses the size of each type when reserved in memory.

Uploaded by

jiamah2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Programming Fundamentals

(Using C++)
Huma Israr
Department Of Information Engineering Technology
National Skills University, Islamabad.
[email protected]
Fall 2023, BS-CS
2

Week 04

Lecture – 01
Lecture – 02
Variables and Data-types

Semester Calendar
3

Class Schedule

Lab Hours (Practice)

Monday (11:00 to 01:00)


What is a Variable?

 A variable is a memory address where data can be stored and


changed.
 Declaring a variable means specifying both its name and its
data type.
int num;

 What Does a Variable Declaration Do?


 A declaration tells the compiler to allocate enough memory to
hold a value of this data type, and to associate the identifier
with this location. int ageOfDog; 
char middleInitial; 
float taxRate; 
Variables:
 All variables must declared before use.
 At the top of the program
 Just before use.
 Commas are used to separate identifiers of the same type.
int count, age;
(Can declare several variables of same type in one declaration)

 Variables can be initialized to a starting value when they are


declared
int count = 0;
int age, count = 0;
What is an Identifier?

 An identifier is the name to denote labels, types,


variables, constants or functions, in a C++ program.
 C++ is a case-sensitive language.
Work is not work
 Identifiers should be descriptive
 Using meaningful identifiers is a good programming
practice
 Identifiers must be unique
Identifiers:
 Identifiers cannot be reserved words (keywords)
e.g. int double main return cout
 Identifier must start with a letter or underscore, and be
followed by zero or more letters (A-Z, a-z), digits (0-9), or
underscores
 VALID
abc age_of_dog _taxRateY2K
X PrintHeading ageOfHorse
 NOT VALID
age# 2000TaxRate Age-Of-Dog main
Data-type
 All variables use data-type during declaration to restrict the
type of data to be stored.
 Therefore, we can say that data types are used to tell the
variables the type of data it can store.
 Whenever a variable is defined in C++, the compiler allocates
some memory for that variable based on the data-type with
which it is declared.
 Every data type requires a different amount of memory.
C++ Data Types

Simple Structured

integral enum floating array struct union class

char short int long bool

Address
float double long double

pointer reference
C++ Primitive Data Types:
Primitives data types are built-in or predefined data types and can be
used directly by the user to declare variables.

Primitive types

integral floating

char short int long bool float double long double

unsigned
Primitive Data Types in C++
 Integral Types int sample values
4578 -4578 0
 represent whole numbers and their negatives
 declared as int, short, or long

 Character Types char sample values


 represent single characters ‘B’‘d’ ‘4’ ‘?’ ‘*’
 declared as char
 Stored by ASCII values

 Boolean Type bool values


 declared as bool true false

 has only 2 values true/false


 will not print out directly

 Floating Types float sample values


95.274 95.0 .265
 represent real numbers with a decimal point
 declared as float, or double
 Scientific notation where e (or E) stand for “times 10 to the ” (.55-e6)
Data Types in C++
 The data type specifies the size and type of information the
variable will store
C++ Data Types Example
 int myNum = 5; // Integer (whole number)
 float myFloatNum = 5.99; // Floating point number
 double myDoubleNum = 9.98; // Floating point number
 char myLetter = 'D'; // Character
 bool myBoolean = true; // Boolean
 string myText = "Hello"; // String
Size reserved by each data type

//cin>> Size of each data type


#include <iostream>
using namespace std;
int main() {
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
return 0;
}
Integral Data types
 long b = 4523232;
 long int c = 2345342;
 long double d = 233434.56343;
 short d = 3434233; // Error! out of range
 unsigned int a = -5; // Error! can only store positive numbers or 0
 float f1 = 35e3; //A floating point number can also be a scientific

 double d1 = 12E4; //number with an "e" to indicate the power of 10:


If Ans is Floating point If Ans is Floating point

int main () { int main () {


int a, b; // Variable declaration int a, b, c; // Variable declaration
int c, d; float d;
a = 5; // Actual initialization a = 5; // Actual initialization
b = 2; b = 2;
c = a + b; c = a + b;
cout << c << endl ; cout << c << endl ;
d = a/b; d= a/b;
cout << d << endl ; cout << d << endl ;
return 0; } return 0; }
Bool Type
 A boolean data type is declared with the bool keyword and can
only take the values true or false. When the value is returned,
true = 1 and false = 0.
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun; // Outputs 1 (true)
cout << isFishTasty; // Outputs 0 (false)
Char Type
 The char data type is used to store a single character. The
character must be surrounded by single quotes, like 'A' or 'c':
char myGrade = 'B';
cout << myGrade;
Guess the Output

{
char a = 65, b = 66, c = 67;
cout << a;
cout << b;
cout << c; }
String Type
 The string type is used to store a sequence of characters
(text). This is not a built-in type, but it behaves like one in its
most basic usage. String values must be surrounded by double
quotes
string greeting = "Hello";
cout << greeting;

Note:To use strings, you must include an additional header file in the source
code, the <string> library
Any Question

You might also like