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

Lecture4 Week 2

The document provides an introduction to variable storage and data types in C++. It discusses how data is stored in memory locations identified by addresses and describes common data types like int, float, char, and their typical storage sizes in bytes and bits. It also covers variable declarations, assignments, and basic output using cout.

Uploaded by

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

Lecture4 Week 2

The document provides an introduction to variable storage and data types in C++. It discusses how data is stored in memory locations identified by addresses and describes common data types like int, float, char, and their typical storage sizes in bytes and bits. It also covers variable declarations, assignments, and basic output using cout.

Uploaded by

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

Lecture 4 SP-22(CS/SE/AI)

Introduction to C++
User Inputs & Data Types
Instructor : Ms.Ayesha

11/05/2023 1
Data/Variable Storage
Counting in Binary

Data stored in memory 0 0


1 1
 Each memory location has a numerical address
2 10
 Each location can store one byte of information
o One byte is 8 bits 3 11
o One bit can store one binary digit (0 or 1) 4 100
o So, a byte can store 28 unique values (256 values) 5 101
6 110
Number of bytes required to store information 7 111

will depend on the type of data being stored 8 1000


… -
255 11111111
Typical data storage by
type
Type # of bytes/bits Data Storage examples
int 4/32 Memory
Type Name value
short 2/16 Address
long 8/64 int n 1000
1001
float 4/32 1002 10
double 8/64 1003
char 1/8 char a 1004 ‘A’
string N/(N*8) string name 1005 ‘J’
1006 ‘O’
bool 1/8 1007 ‘H’
1008 ‘N’
int n=10; bool maybe 1009 true
float x, pi=3.14159; float pi 1010
char a=’A’; 1011
string name=”John”; 1012 3.14159
bool maybe= true; 1013
Variables “under the hood”
For each variable, compiler needs to
keep track of 4 pieces of information: Memory locations
 Variable name … ?
 Location of data float pi= 3.14159; 0x1000 ?
o Memory address of stored data Variable
0x1001
Name pi
 Variable type 0x1002
o How to interpret data Location 0x1001 3.14159
0x1003
o How many bytes? Type float
0x1004
 Value 0x1005 ?
o Stored at the memory address
… ?
1001

Variables y 12.5 1002


1003
1004
Temperature 32 1005
1006
Letter 'c' 1007
1008
Number - 1009

variable can hold a number or a data of other


types, it always holds something. A variable has a
name
the data held in variable is called value
variables are implemented as memory locations
and assigned certain memory address. The exact
address depends on computer and compiler.
we think as though the memory locations are
actually labeled with variable names 5
Identifiers
name of a variable (or any other item you define in
program) is called identifier
identifier must start with a letter or underscore
symbol (_), the rest of the characters should be letters,
digits or underscores
the following are valid identifiers:
x x1 x_1 _abc sum RateAveragE
the following are not legal identifiers. Why?
13 3X %change data-1 my.identifier
a(3)
C++ is case sensitive:
MyVar and myvar are different identifiers
6
What Are Good Identifiers?
 careful selection of identifiers makes your program clearer
 identifiers should be
 short enough to be reasonable to type (single word is norm)
o Standard abbreviations are fine (but only standard abbreviations)
 long enough to be understandable
 two styles of identifiers
 C-style - terse, use abbreviations and underscores to separate the words, never use
capital letters for variables
 Pascal-style - if multiple words: capitalize, don’t use underscores
o camel Case – variant of Pascal-style with first letter lowercased
 pick style and use consistently
 ex: Pascal-style C-style Camel Case
Min min min
Temperature temperature temperature
CameraAngle camera_angle cameraAngle
CurrentNumberPoints cur_point_nmbr currentNumberPoints

7
Keywords
keywords are identifiers reserved as part of the language
int, return, float, double
 they cannot be used by the programmer to name things
 they consist of lowercase letters only
 they have special meaning to the compiler

8
Keywords (cont.)
asm do if return typedef
auto double inline short typeid
bool dynamic_cast int signed typename
break delete long sizeof union
case else mutable static unsigned
catch enum namespace static_cast using
char explicit new struct virtual
class extern operator switch void
const false private template volatile
const_cast float protected this wchar_t
continue for public throw while
default friend register true union
delete goto reinterpret_cast try unsigned

9
Variable Declarations
 every variable in C++ program needs to be declared
 declaration tells the compiler (and eventually the computer) what kind of data is
going to be stored in the variable
 the kind of data stored in variable is called it’s type
 a variable declaration specifies
 type known list of one or
 name type more identifiers
 declaration syntax:
 two commonly used numeric types are:
type id, id, ..., id;
 int - whole positive or negative numbers:
1,2, -1,0,-288, etc.
 double - positive or negative numbers with fractional part:
1.75, -0.55
 example declarations:
int numberOfBars;
double weight, totalWeight;

10
Where to Declare
 the variables should be declared as close to the place where they are used as possible.
 if the variable will be used in several unrelated locations, declare it at the beginning of the program:
int main() {
 right here
 note that variable contains a value after it is declared. The value is usually arbitrary

11
Assignment
var = value;
 assignment statement is an order to the computer to set the value of the variable on the left hand side of
the equation to what is written on the right hand side
 it looks like a math equation, but it is not
 Example:
numberOfBars = 37;
totalWeight = oneWeight;
totalWeight = oneWeight * numberOfBars;
numberOfBars = numberOfBars + 3;

12
Output
 To do input/output, at the beginning of your program you have to insert
#include <iostream>
using std::cout; using std::endl;
 C++ uses streams for input an output
 stream - is a sequence of data to be read (input stream) or a sequence of data
generated by the program to be output (output stream)
 variable values as well as strings of text can be output to the screen using cout
(console output):
cout << numberOfBars;
cout << ”candy bars”;
cout << endl;
 << is called insertion operator, it inserts data into the output stream, anything within
double quotes will be output literally (without changes) - ”candy bars taste
good”
 note the space before letter “ c” - the computer does not insert space on its own
 keyword endl tells the computer to start the output from the next line

13
More Output

 the data in output can be stacked together:


cout << numberOf_Bars << ”candy bars\n”
 symbol \n at the end of the string serves the same purpose as endl
 arithmetic expressions can be used with the output statement:
cout << “The total cost is $” << (price + tax);

14
Escape Sequences
 certain sequences of symbols make special meaning to the computer.
They are called escape sequences
 escape sequence starts with a backslash (\). It is actually just one special character.
 Useful escape sequences:
o new-line \n
o horizontal tab \t
o alert \a
o backslash \\
o double quote \”
 What does this statement print?
cout << ”\” this is a \t very cryptic \” statement \\ \n”;

15
Input
 cin - (stands for Console INput) - is used to fill the values of variables with the input
from the user of the program
 to use it, you need to add the following to the beginning of your program
using std::cin;
 when the program reaches the input statement it just pauses until the user types
something and presses <Enter> key
 therefore it is beneficial to precede the input statement with some explanatory output
called prompt:
cout << “Enter the number of candy bars
cout << “and weight in ounces.\n”;
cout << “then press return\n”;
cin >> numberOfBars >> oneWeight;
 >> is extraction operator
 dialog – collection of program prompts and user responses
 note how input statements (similar to output statements) can be stacked
 input tokens (numbers in our example) should be separated by (any amount of)
whitespace (spaces, tabs, newlines)
 the values typed are inserted into variables when <Enter> is pressed, if more values
needed - program waits, if extra typed - they are used in next input statements if needed

16
Intro to Visual studio/Dev C++
Installation
Creating a Project
Adding source file
Running the project

11/05/2023 17

You might also like