0% found this document useful (0 votes)
10 views11 pages

C++ Data Types and Variables

This document covers data types, variables, constants, and operators in C++. It explains various numeric variable types, their memory requirements, and how to declare and initialize variables, as well as the use of constants. Additionally, it details different types of operators, including arithmetic, relational, logical, and assignment operators, along with examples of their usage.

Uploaded by

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

C++ Data Types and Variables

This document covers data types, variables, constants, and operators in C++. It explains various numeric variable types, their memory requirements, and how to declare and initialize variables, as well as the use of constants. Additionally, it details different types of operators, including arithmetic, relational, logical, and assignment operators, along with examples of their usage.

Uploaded by

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

LESSON FOUR: Data Types, Variables and Operators

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

Character Char 1 -128 to 127


Integer Int 2 -32768 to 32767
Short integer Short 2 -32768 to 32767
Long integer Long 4 -2,147,483,648 to
2,147,438,647
Unsigned character Unsigned char 1 0 to 255
Unsigned integer Unsigned int 2 0 to 65535
Unsigned short Integer unsigned 2 0 to 65535
short
Unsigned long unsigned long 4 0 to 4,294,967,295
integer
Float Float 4 3.4E-38 to 3.4E+38
Double Double 8 1.7E-308 to 1.7E+308
Long double Long double 8 1.7E-308 to 1.7E+308

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;

Declaring many variables


You can declare many variables using the same statement. In this case, you separate
variables with commas. For example, the following statement declares variables named a,
b and c of float type. These variables can store numbers with decimal points.
This is alternative to writing three statements.
float a;

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 and Binary operators.

Unary operator takes only one operand while Binary operator takes two operands. e.g. +, - are
Binary while ++, -- are Unary operators.

Evaluation of an expression.

An expression is evaluated from the left to the right hand side.

Types of Operators.

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators

4
(a) Arithmetic Operators.

An Arithmetic operator operates on two operands in an expression to produce a value


(number). The possible Arithmetic operators are:-

Operator Meaning

+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus

(b) Relational Operators/ Conditional Operators.

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

> Greater than


>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to
!= Not equal to

(c) Logical Operators

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

Where T stands for Truth and F stands for False.

(d) Assignment Operator (=)

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

a = b means “Take the value of b and store it in a variable a”


x = x + 1 means “Take the value of x, i.e. increment the value of x by one or just increment
x”

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

(e) Ternary Operators


This is used in decision making. It takes the following:-
Expression 1 ? Expression 2: Expression 3
Expression 1 is evaluated if it is True, then Expression 2 is evaluated and becomes the value
of the whole expression. Otherwise Expression 3 is evaluated and becomes the value of the
expression.

For Class Session: TYPES OF OPERATORS in C++


1. Assignment operators
2. Arithmetic operators
3. Relational operators
4. Logical operators

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

(Y>X) || (X>2) Returns true


(Y>X) || (X>8) Returns true
(X>9) || (X>8) Returns false

! (5>6) Returns true


! (X>Y) Return true
! (4>7) Returns true
! (9>6) Returns false
! (true) Return false
! (false) Return true

EXAMPLES
Checking if a number is EVEN or ODD

// Checking if a number is EVEN or ODD


#include<iostream>
using namespace std;
int main () {
int num;
int rem;

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

// Checking if the first number is divisble by the second number


#include<iostream>
using namespace std;
int main () {
int num1;
int num2;
int rem;
cout<<"Enter a number";
cin>>num1;
cout<<"Enter the second number";
cin>>num2;

rem = num1 % num2;

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

You might also like