C++ Data Types and Variables
C++ Data Types and Variables
Data Type
C++ and C languages provides several different types of numeric variables. You need different
types of variables because different numeric values have varying memory storage requirements
and differ in the ease with which certain mathematical operations can be performed on them. Small
integers (for example, 1, 199, and -8) require less memory to store, and your computer can perform
mathematical operations (addition, multiplication, and so on) with such numbers very quickly. In
contrast, large integers and floating-point values (123,000,000 or 0.000000871256, for example)
require more storage space and more time for mathematical operations. By using the appropriate
variable types, you ensure that your program runs as efficiently as possible.
Variable Type Keyword (size)Bytes Required Range
char data type stores characters e.g. alphabets (a-z, A-Z), digit (0-9)- but can’t be used in
expression i.e. are considered non-values in this case, or special literals e.g. ?,-,*,{.
Int data type stores whole numbers (or integers). An int type variable occupies four bytes.
1
float data type stores numbers with fraction parts. Float occupies four bytes.
Double data type stores numbers with fractional parts just like float. but can store a very large
value e.g. trillion for example, if you try to input 1,000,000,000,000 into a float type variable,
you will get errors unlike when you input into a double type variable.
long keyword it is used with int or double. The data type long int, can store very large integer,
while long double can store even large numbers with fraction parts than double data type.
Short keyword halves the storage space allocated for an int-type variable. You can for example
declare a variable of type short int, and this is so as to spare the memory.
VARIABLES, CONSTANTS AND DATA TYPES
a. Variables
A variable is a programming concept of a location in the memory for storing a value that
may change from time to time. However, the values that can be stored in a variable must
be of the same data type. A program must store the values it is using for computation in
variables or other advanced storage locations.
b. Declaration of variables
Declaring a variable instructs the computer to allocate a memory space for storing a value
of that type. A variable must be declared many times in the same section of the program
(in the main ()function or in any other function).
The syntax for declaring a variable is Type Name;
Example
The following statement instructs the computer to declare a variable named x, for storing
an integer (whole number)
int x;
2
float b;
float c;
(c). Initialization of variables
You can give a variable some initial value during declaration (also known as initializing a
variable). For example to initialize floats a, b, and c to zero during declaration, we write
Float a=0, b=0, c=0;
To initialize only b, we write
Float a, b=0, c;
To initialize a character-type variable, we use single quotes to enclose the initial content.
For example,
Char t= ‘y’;
Declares a character variable named t and initializes it with contents ‘y’.
To initialize a string, we use the “” quotes. For example
char name [30] = “john”;
Initializes the variable name with the string “john”;
char name [30] = “”;
Initializes the variable name with empty string.
Variable rules /naming conventions
1. A variable must be declared before being used.
2. A variable can not be declared more than once
3. A variable can be declared anywhere in the program I.e. it’s not a must to declare it at
the beginning of main(). We can declare it at the middle of other statements.
4. A library key word must not be used when declaring a variable e.g. printf, scanf ,main
etc.
Constants
A constant just like a variable, is used to store values in memory. A constant’s value
however, may not change. Constants are used to store values that do not change in any run
of the program including the pi of a circle, the tax of a payroll processing program, the pass
mark in an examination processing program, etc.
Declaration of constants
3
Just like a variable, a constant’s declaration instructs the computer to allocate a space for
storing the value. Two syntax for declaring a constant are;
(i). #define Name Value
(ii)const Type Name = Value;
Example
Each of the following two lines declares a constant named pi for storing value 3.14
#define pi 3.14;
const double pi = 3.14
Operators in C programming.
An operator is used in a program to manipulate data types values. It combines two operands in an
expression.
i.e. z = x + y
Unary operator takes only one operand while Binary operator takes two operands. e.g. +, - are
Binary while ++, -- are Unary operators.
Evaluation of an expression.
Types of Operators.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
4
(a) Arithmetic Operators.
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Relational operators are used in Boolean expression to compare the values of the two
operands which produce a value of either True or False. A Boolean expression is an
expression whose value can either be True or False.
Operator Meaning
5
Logical operators are used to combine two Boolean expressions into a compound Boolean
expression. The truth table rules are used to test the Truth or False of a compound expression
depending on the truth and false of each expression in the compound expression.
Operator Meaning
&& And
// Or
! Not(Negation)
Truth Table
Input Output
A B A&&B A//B !A !B
F F F F T T
T F F T F T
F T F T T F
T T T T F F
The assignment operator is used to assign the variable on the left hand side the value of
expression which is on the right hand side.
Examples
6
(e) Short-cut Operators
C has shortcuts for writting some expressions. The shortcut operators include the following +
+,- -,+ =, * =, / =. These operators will be explained by using examples below.
Examples;
a=b; Means ”Take the value of b and Store it in a Variable a”
x=x+1; Means”Take the Value of x, add one to it, and store results back to a variable x;
x=x-1; Means”Take the Value of x, subtract one to it, and store results back to a variable x;
Expression Equivalent to
x+ + x=x+1
x- - x=x-1
x+=y x=x+y
x-=y x=x-y
x*=y x=x*y
7
1. Assignment Operator (=)
Gives a value to a variable or gives a value of a variable to another variable
X=5
Y =7
Z=X
2. Arithmetic operators
+ Addition operator
- Subtraction operator
* Multiplication operator
/ Division operator
% Modulo operator It returns the remainder value of a division operation
For example
6 % 2 returns 0 means that 6 is divisible 2
7 % 4 returns 3 means that 7 is NOT divisible by 4
14 % 5 returns 3 means that 14 is NOT divisible by 5
14 % 7 returns 0 means that 14 is divisible by 7
8 % 2 returns 0 means that 8 is divisible by 2 and 8 is an even number
11 % 2 returns 1 means that 11 is NOT divisible by 2 and 11 is an ODD number
3. Relational Operators
> Greater than 4>2 True , 3>5 False
< Less than 5<4 True, 6<7 False
>= Greater or Equal to 7>=7 True, 9>=15 False
<= Less or Equal to 6<=6 True, 8<=11 True, 4<=3 False
!= Not Equal to 5 != 7 True, 5 != 5 False
4. Logical operators
&& Called the AND operator Return true if and only two operands are true
|| Called the OR operator Returns true if one of the operands is true
8
! Called the NOT operator Returns the opposite truth value of a condition. It negates
the truth value
X=5
Y=6
(Y>X) && (X>2) Returns true
(Y>X) && (X>8) Returns false
EXAMPLES
Checking if a number is EVEN or ODD
cout<<"Enter a number";
cin>>num;
9
rem = num % 2;
if (rem == 0) {
cout<<"The number you entered is an EVEN number";
} else {
cout<<"The number you entered is an ODD number";
}
return 0;
}
Checking Divisibility
if (rem == 0) {
10
cout<<"The first number is divisble by the second number\n\n";
} else {
cout<<"The first number is NOT divisble by the second number\n\n";
}
return 0;
}
11