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

Lec 2 - Memory and Variables

The document provides an introduction to programming in C++, covering basic constructs, data types, and memory management. It explains various data types such as character, integer, floating-point, and boolean, along with variable declaration and initialization. Additionally, it includes examples of user input and output using cin and cout, as well as simple C++ program structures.

Uploaded by

shaikham2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lec 2 - Memory and Variables

The document provides an introduction to programming in C++, covering basic constructs, data types, and memory management. It explains various data types such as character, integer, floating-point, and boolean, along with variable declaration and initialization. Additionally, it includes examples of user input and output using cin and cout, as well as simple C++ program structures.

Uploaded by

shaikham2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Data Types

Introduction to Programming C++


Objectives
• Understand and use the basic programming constructs of C/C+
+
• Manipulate various C/C++ data types, such as arrays, strings,
and pointers

Introduction to Programming C++


2
C++ Get Started
• To start using C++, you need two things:
• A text editor, like Notepad, to write C++ code
• A compiler, like GCC, to translate the C++ code into a language
that the computer will understand

Introduction to Programming C++


• OR online C++ compiler:
www.onlinegdb.com

3
Memory
• Imagine a memory as a block with different slots
• Each slot has a size of 1 byte
• 1 byte= 8 bits
• Each memory location has an address (0001,0002,0003, etc..)

Introduction to Programming C++


Memory
Memory location with
******** address 0001
8 bits = 1 byte Memory location with
address 0002
Memory location with
address 0003

4
Variables
• Variables are a portion of memory to store a value
• Each variable has a specified size (1 byte, 4 bytes, etc…)

• Types of Variables:

Introduction to Programming C++


1. Character Type
2. Numerical Integer Types
3. Floating- Point Types
4. Boolean Type

5
Character Type:
• C++ syntax  char
• Size= 1 Byte of the memory
• It can save values such as letters and symbols,
example: ‘A’, ‘$’, etc…

Introduction to Programming C++


Numerical Integer Type:
• Example: C++ syntax  int
• Size= 4 Bytes of the memory
• It can save integer numerical values, example: 3, 8
6
Floating- Point Types:
• Example: C++ syntax  double
• Size= 8 Bytes of the memory
• It can save decimal numerical values, example: 3.01,
8.01

Introduction to Programming C++


Boolean Type:
• Example: C++ syntax  bool
• It only saves the values true and false

7
Binary and ASCII
• Numbers gets converted from decimal to binary (we covered
that last lecture)
• What about characters? How are they saved in the memory?
• There is an ASCII table which gives every symbol and letter a
binary number.

Introduction to Programming C++


8
Variables
• A variable in C++ needs a type and a name:
• Syntax: TYPE Name;
• The name of a variable can’t start with a number:
Ex: 1lab X

Introduction to Programming C++


• The name can’t include a space, ! Or a symbol:
Ex: Lab 1, Lab!, Lab$ X
• An _ can be used:
Ex: Lab _1 Accepted
• C++ is case sensitive:
Ex: RESULT is NOT the same as result
9
Variables
• Example of variables:
int hello;

TYPE NAME

Introduction to Programming C++


Size: 4 Bytes of Memory Memory

4 Bytes are reserved with


the name hello

10
Variables
• Example of variables:
char cool;

TYPE NAME

Introduction to Programming C++


Size: 1 Byte of Memory Memory

4 Bytes are reserved with


the name hello

1 Byte is reserved with the


name cool 11
Initializing a Variable
• To initialize a value for a variable one of the following sytax
can be used:
Memory
• Type Name = Value;
00000101
• Ex: int hello = 5;
4 Bytes are reserved with 00000000
• Type Name (Value);

Introduction to Programming C++


the name hello & has a value 00000000
• Ex: int hello (5); of 5 00000000
• Type Name {Value}; 1 Byte is reserved with the 01101101
• Ex: int hello {5}; name cool & has a value of
• Ex: char cool = ‘m’; m

12
Inserting Values and Printing
Messages
• To allow the user to input values that will be saved in the
memory:
1- Define a variable (type and name)/ Ex: int number;
2- Write the syntax cin>> variable name;/ Ex:

Introduction to Programming C++


cin>>number;

• To allow the program to print messages for the user:


• Write the syntax: cout<<“message”;/ Ex: cout<<“welcome to C++”;

13
C++ Program Main Block
#include <iostream>
using namespace std;
int main ()
{

Introduction to Programming C++


return 0;
}
14
• #include <iostream>

• is a header file library that lets us work with input and output
objects, Header files add functionality to C++ programs

Introduction to Programming C++


15
• using namespace std;

• means that we can use names for objects and variables from
the standard library

Introduction to Programming C++


16
• int main()

• This is called a function. Any code inside its curly


brackets {} will be executed.

Introduction to Programming C++


17
C++ Output (Print Text)
• The cout object, together with the << operator, is used to
output values/print text:

Introduction to Programming C++


18
C++ Output (Print Text)

Introduction to Programming C++


19
C++ New Lines
• To insert a new line, you can use the \n character:

Introduction to Programming C++


20
C++ New Lines
• Another way to insert a new line, is with the endl manipulator:

Introduction to Programming C++


21
C++ Comments
• Comments can be used to explain C++ code, and to make it
more readable. Comments can be singled-lined or multi-lined:

Introduction to Programming C++


22
C++ Variables
• Variables are containers for storing data values.
• In C++, there are different types of variables (defined with
different keywords), for example:

Introduction to Programming C++


23
C++ Variables
• int - stores integers (whole numbers), without decimals, such
as 123 or -123
• double - stores floating point numbers, with decimals, such as
19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values

Introduction to Programming C++


are surrounded by single quotes
• string - stores text, such as "Hello World". String values are
surrounded by double quotes
• bool - stores values with two states: true or false

24
C++ Variables

Introduction to Programming C++


25
C++ Variables

Introduction to Programming C++


26
C++ Variables

Introduction to Programming C++


27
C++ Variables

Introduction to Programming C++


28
C++ Variables

Introduction to Programming C++


29
C++ Variables

Introduction to Programming C++


30
C++ Variables

Introduction to Programming C++


31
Introduction to Programming C++
32
Introduction to Programming C++
33
C++ User Input
• You have already learned that cout is used to
output (print) values. Now we will use cin to get
user input.
• cin is a predefined variable that reads data from

Introduction to Programming C++


the keyboard with the extraction operator (>>).
• In the following example, the user can input a
number, which is stored in the variable x. Then
we print the value of x:

34
C++ User Input

Introduction to Programming C++


35
Introduction to Programming C++
36
Introduction to Programming C++
37
Introduction to Programming C++
38
Introduction to Programming C++
39
Example
• #include <iostream>
using namespace std;

int main() {
cout << "Hello World!";

Introduction to Programming C++


return 0;
}

40
Find the sum of 2 numbers
#include <iostream>
using namespace std;
int main ()
{

Introduction to Programming C++


int num1, num2, sum;
cout <<“Enter number 1\n”;
cin>> num1;
cout <<“Enter number 2\n”;
cin>>num2;
sum= num1 + num2;
cout<<“The sum is”<<sum;

41
return 0;
}
Find the average of 3 numbers
#include <iostream>
using namespace std;
int main ()
{

Introduction to Programming C++


int num_1, num_2, num_3;
double average;
cout <<“Enter number 1 and number 2 and number 3\n”;
cin>> num_1>>num_2>>num_3;
average= (num_1+num_2+num_3)/3;
cout<<average;

42
return 0;
}

You might also like