0% found this document useful (0 votes)
5 views18 pages

C++ Variables, Constants, and Data Types

Uploaded by

hambaabebekaggle
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)
5 views18 pages

C++ Variables, Constants, and Data Types

Uploaded by

hambaabebekaggle
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

Chapter 3

Constants, Variables, Data Types and Operators in C++ Tokens


Variables
 A variable is a symbolic name for a memory location in which data can be
stored and subsequently recalled.
 Variables are used for holding data values so that they can be utilized in various
computations in a program.
 All variables have two important attributes:
A type, which is, established when the variable is defined (e.g., integer, float,
character).
Once defined, the type of a C++ variable cannot be changed.
A value, which can be changed by assigning a new value to the variable.
 The kind of values a variable can assume depends on its type.
 For example, an integer variable can only take integer values (e.g., 2, 100, and
-12) not real numbers like 0.123.
Variable Declaration
Declaring a variable means defining (creating) a variable.
 You create or define a variable by stating its type, followed by one or more
spaces, followed by the variable name and a semicolon.
The variable name can be virtually any combination of letters, but cannot
contain spaces and the first character must be a letter or an underscore.
Variable names cannot also be the same as keywords used by C++.
 Legal variable names include x, J23qrsnf, and myAge.
Good variable names tell you what the variables are for; using good names
makes it easier to understand the flow of your program.
The following statement defines an integer variable called myAge:
int myAge;
IMPORTANT- Variables must be declared before used!
Creating More Than One Variable at a Time
You can create more than one variable of the same type in one statement by
writing the type and then the variable names, separated by commas.
 For example:
int myAge, myWeight; // two int variables
long area, width, length; // three longs
As you can see, myAge and myWeight are each declared as integer variables.
The second line declares three individual long variables named area, width,
and length.
However keep in mind that you cannot mix types in one definition statement.
Assigning Values to Your Variables
You assign a value to a variable by using the assignment operator (=).
 Thus, you would assign 5 to Width by writing
int Width;
Width = 5;
You can combine these steps and initialize width when you define it by writing
int Width=5;
A demonstration of using Variables
#include<iostream>
using namespace std;
int main(){ Output
int width=5; Width=5
int length=10; Length=10
Cout<<“Width=“<<width<<“\n”;
Cout<<“Length=“<<length;
return 0;
}
Contants
Constants are expressions with fixed values
It refers to fixed values in the code that you can not change.
There are three types’ constants in C ++.
These are: Literal, defined constants and symbolic (memory constant) constants.
Cont…
[Link] constants
These are values typed directly into the program when they are needed and they
can be numbers, characters or strings.
E.g. int num=35;
char char_var=’a’;
[Link] constants
Although the construction is not popular in C ++, we should note that we can
also specify constants using the preprocessor directive #define.
 Example #define Pi 3.14
Appending the above code at the beginning of your program specifies that the
identifier Pi will be replaced by the text 3.14 throughout the program.
Cont…
Example://defined constants: calculate circumference of circle
#include<iostream.h>
#include<conio.h>
#define PI 3.14
void main ()
{ double r = 5.0; // radius
double circm;
circm = 2*PI*r;
Cout<<” The circumference is: “<<circum;
getch();
}
[Link] (memory) constants Cont…
These are similar to variables in that they are represented by a name and they are data storage locations.
Unlike variables however, the data on constants cannot be changed throughout the program and they must
be initialized when defined.
 These constants are defined using the key word const preceding the data type.
Example
const float PI=3.14;
The key word const ensures that the program will not change the given value, or an error message will be
displayed.
//demonstration of the three types of constant declarations
#include<iostream.h>
#define PI 3.14
void main (){
const double pi = 3.14;
cout<<”Defined constant PI = “<<PI<<endl;
cout<<”Memory constant pi = “<<pi<<endl;
cout<<”Literal constant Pi = “<<3.14<<endl;}
Basic data types
Data type refers to defining the kind of variable
Several data types are built into C++.
The data types supported by C++ can be classified as:
 Basic (fundamental) data types Derived data types and
 User defined data types Empty data types.
Basic (fundamental) data types in C++ can be conveniently divided into numeric and character
types.
 Numeric variables can further be divided into integer variables and floating-point variables.
 Integer variables will hold only integers whereas floating number variables can accommodate
real numbers.
Both the numeric data types offer modifiers that are used to vary the nature of the data to be
stored.
 The modifiers used can be short, long, signed and unsigned.
Cont…

o This table shows the variable type, how much room it takes in memory, and what kinds of values can be
stored in these variables.
o The values that can be stored are determined by the size of the variable types.
Signed, Unsigned, long and short Modifiers
Signed integers are either negative or positive.
It is used to store either negative or positive integer numbers
Unsigned integers are always positive.
Long modifier is used to store large positive numbers
Short modifier is used to store small integer number
Characters
• Character variables (type char) are typically 1 byte, enough to hold 256 values.
• A char can be interpreted as a small number (0-255) or as a member of the ASCII set.
• ASCII stands for the American Standard Code for Information Interchange.
• The ASCII character set and its ISO (International Standards Organization) equivalent are a
way to encode all the letters, numerals, and punctuation marks.
• In the ASCII code, the lowercase letter "a" is assigned the value 97.
• All the lower- and uppercase letters, all the numerals, and all the punctuation marks are
assigned values between 1 and 128.
• Another 128 marks and symbols are reserved for use by the computer maker, although the
Operators
C++ provides operators for composing arithmetic, relational, logical, bitwise,
and conditional expressions.
It also provides operators which produce useful side-effects, such as
assignment, increment, and decrement.
Assignment Operators
The assignment operator is used for storing a value at some memory location
(typically denoted by a variable).
For Example
n=25; // assign 25 to n variable
Arithmetic Operators
C++ provides five basic arithmetic operators.
Except for remainder (%) all other arithmetic operators can accept a mix of
integer and real operands.
Cont…
Relational Operators
• C++ provides six relational operators for comparing numeric quantities.
Cont…
Logical Operators
• C++ provides three logical operators for combining logical expression.
Cont…
Bitwise Operators
C++ provides six bitwise operators for manipulating the individual bits in an
integer quantity.
Cont…
Increment/decrement Operators
The auto increment (++) and auto decrement (--) operators provide a
convenient way of, respectively, adding and subtracting 1 from a numeric
variable.
These are summarized in the following table.
The examples assume the following variable definition:
Precedence of Operators
The order in which operators are evaluated in an expression is significant and is determined
by precedence rules.
These rules divide the C++ operators into a number of precedence levels.
Operators in higher levels take precedence over operators in lower levels.
For example, in
a == b + c * d
c * d is evaluated first because * has a higher precedence than + and ==.
The result is then added to b because + has a higher precedence than ==, and
then == is evaluated.
Precedence rules can be overridden using brackets.
 For example, rewriting the above expression as
a == (b + c) * d
causes + to be evaluated before *.
TH
AN
K
YO
U!
!!

You might also like