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

Variables: A Variable Is A Placeholder For A Value

The document discusses variables in C++. It defines a variable as a named memory location that is used to store and access a value. It describes how variables are declared with a data type and name, and must be declared before use. It also covers variable naming conventions, basic data types like int and float, literals vs constants, and common operators in C++.

Uploaded by

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

Variables: A Variable Is A Placeholder For A Value

The document discusses variables in C++. It defines a variable as a named memory location that is used to store and access a value. It describes how variables are declared with a data type and name, and must be declared before use. It also covers variable naming conventions, basic data types like int and float, literals vs constants, and common operators in C++.

Uploaded by

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

Variables

A variable is a placeholder for a value. It is a

named memory location where that value is


stored.
Use the name of a variable to access or update
the value.
View memory as a numbered sequence of
bytes, where the number is the address of
each byte.

Later, we'll see how you can access the value


through its address.

Variables
The syntax for declaring a variable is:
data_type variable_name ;
or, for several variables of the same type:

data_type variable_name1, variable_name2 ;

Variables may be declared at any point in the

program, but they must always be declared


before they are used.
The name of a variable is a sequence of
letters, digits and underscores that does not
begin with a digit.
C++ is case sensitive
2

Readability
Pick meaningful variable names that describe

the entity they refer to.


Things to avoid:

the letter l (el) which can be confused with the digit 1


(one)
single-letter variables (there's one exception)
variable names starting with one or two underscores
(many internal C++ names follow this convention)
extremely long names
names similar to C++ reserved or predefined words
names similar to one another
3

Basic types
int for signed integers (4 bytes*)

range: -231..+231

unsigned int for unsigned integers (4 bytes*)

range: 0..+232

float and double for single- and double-precision

floating point numbers (4 and 8 bytes respectively)

range: ~10-44.85 to ~1038.53 for floats


range: ~10-323.3 to ~10308.3 for doubles

char for characters (1 byte)

range: -128..+127 but ASCII uses only 0..127

bool for boolean values (1 byte)

range: 0-1

* for our architecture


4

Literals
#include<iostream>
using namespace std;
int main () {
integer literal
cout << 10
<< "USD = "
<< 8.2
double literal
<< "EUR\n";
return 0;
string literals
}

Literals
Major disadvantages:
a literal cannot be reused (you have to type it in
every time)
it is easy to make a typo
if you want to change it, you have to make sure you
change all of its occurrences inside the program.
Good idea:
Use #define (C++ preprocessor directive) to define
Look ma, no semicolon!
#define NUM_DOLLARS
10
a literal

#include<iostream>
using namespace std;
int main () {
cout << NUM_DOLLARS;
return 0;
}

#defined constants
Location: with other directives (e.g. #include)
Syntax: #define
UNIQUE_NAME
some_value
Before compiling, pre-processor does 'find &

replace' on your file: every occurrence of


UNIQUE_NAME is replaced by some_value.
Convention: UNIQUE_NAME is always in UPPERCASE.
Major advantages:

One single, central location for fixed values


prevents scattered, hard-to-find errors in literals
makes it easy to modify the value
Works for ANY repeated text, not just numbers
string literals, even executable text statements
7

named constants
Declared like variables BUT
preceded with the keyword const
initialized at declaration
their value CANNOT change afterwards.
#include<iostream>
using namespace std;
int main () {
const int num_dollars = 10;
cout << num_dollars;
// num_dollars = 20; ILLEGAL!
return 0;
}
8

Operators
Assignment operator:

=
Arithmetic operators: +, , *, /, %
Relational operators: <, >, <=, >=, ==, !=
Logical operators: &&, ||, !

Operators
()
! Unary

higher precedence

* / %
+
< <= >= >
==

!=

&&
||
=

lower precedence

Associativity: left-to-right (except = and unary operators)

10

You might also like