You are on page 1of 63

Lesson 3

Data Types and Variables


Lesson 3
Objectives:
At the end of this module students should be able:

1. To know and be familiarize with the basic data


types of C/C++ language as well as the basic input
and output functions.
2. To define and choose variable correctly in a
program.
3. To solve machine problems using the basic
functions, operators, data types and variables.
Lesson 3
BASIC DATA TYPES

C/C++ language has four basic data types:


integer, float, double, character.
Lesson 3
BASIC DATA TYPES

Integer
These are whole numbers, both positive and
negative. Unsigned integers (positive values only)
are supported. In addition, there are short and long
integers.

The keyword used to define integers is, int


Lesson 3
BASIC DATA TYPES
An example of an integer value is 20. An
example of declaring an integer variable called
sum is,

int sum;
sum = 20;

Assignment Operator – use in assigning value


in a variable.
Lesson 3
BASIC DATA TYPES
Float
These are numbers which contain fractional
parts, both positive and negative. The keyword
used to define float variables is,
float
An example of a float value is 34.12. An
example of declaring a float variable called
money is,
float money;
money = 34.12;
Lesson 3
BASIC DATA TYPES
Double
These are exponential numbers, both positive and
negative. The keyword used to define double variables is,
double
An example of a double value is 3.12E7. An example
of declaring a double variable called big is,
double big;
big = 3.12E+7;
Lesson 3
BASIC DATA TYPES
Character
These are single characters. The keyword used
to define character variables is,
char
An example of a character value is the letter A.
An example of declaring a character variable called
letter is,
char letter;
letter = ‘A’;
Lesson 3
BASIC DATA TYPES

Note : The assignment of the character


A to the variable letter is done by
enclosing the value in single quotes.

Remember the golden rule: Single


character - Use single quotes.
Lesson 3
Variable
-- is a named location in memory into which a
program can put data and from which it can
retrieve data.
-- a data-name or identifier is the name used to
identify the area of memory reserved for the
variable.
-- variables must be described in terms of their data
type and sizes.
-- a valid variable in C should always start with an
alphabet followed by alphanumeric.
Lesson 3
User defined variables must be
declared before they can be used in a
program. Get into the habit of declaring
variables using lowercase characters.
Remember that C is case sensitive, so
even though the two variables listed below
have the same name, they are considered
different variables in C.

sum != Sum
Lesson 3
Examples:
Given the following variables, which are
VALID and INVALID.

1. num1
2. 123name
3. 2age
4. status
5. salary
Lesson 3

The declaration of variables is done after


the opening brace of void main() as local
variables and before the void main() as
global variables. In C++, anywhere in the
program you can define/ declare a
variable.
Lesson 3
Local Variables

Syntax:

File Inclusion
void main(){
data_type identifier_lists;
Program Statements;
}
Lesson 3
Global Variables

Syntax:

File Inclusion
data_type identifier_lists;
void main(){
Program Statements;
}
Lesson 3
Where:
data_type can be one of the four basic data types:
int, float, double, or char.
identifier_lists is the lists of valid variable names.

Examples:
int sum;  int is the data type and sum is
the variable name.
float amount;  float is the data type and
amount is the variable name.
Lesson 3
Which is Correct?

#include <stdio.h> #include <stdio.h>


#include <conio.h> #include <conio.h>
void main(){ void main(){
int n1; int n1, n2;
int n2; char letter;
char letter; }
}
Lesson 3
#include <stdio.h>
#include <conio.h>
float money, salary; Global Variables
void main(){
int product, sum; Local Variables
sum = 10 + 6;
cprintf(“The sum is 16”);
getche();
}
Lesson 3

Remarks: Local variables can only be


used within the main program, while
globally variables can be used within the
program as well as all the user defined
functions
Lesson 3
The C/C++ Formatters
The % sign is a special character in C/C++. It
is used to display the value of variables. Example
a d character that follows the % indicates that a
decimal integer is expected. Some of the
formatters for printf() are,
Lesson 3
The C/C++ Formatters

Variable Formatters

%d decimal integer
%c character
%s string or character array
%f float
%e double
Lesson 3
C/C++ Operators and Input
Functions
C/C++ has three groups of operators:
Arithmetic, Relational and Logical
operators. It has also some predefined
input functions such as scanf() in stdio.h
and cin inside iostream.h header file.
Lesson 3
The C/C++ Operators

Arithmetic Operators
*  Multiplication
/  Division
+  Addition
-  Subtraction
%  Modulo (Integer Remainder)
Lesson 3
The C/C++ Operators
Additional
++  Increment
--  Decrement

Precedence Rule:
( ), *, /, %  Higher Priority
+, -  Lower Priority
Lesson 3
Examples:

Evaluate the following expressions:


1. 3+2*4/2-4
2. 3 * 5 + ( 10 - 4 ) / 3
3. 4-3/2+5*3
4. (3+4)*(7/2)/ 2+3
5. 7/4 - 2 +3 *4
Lesson 3
Sample Programs
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main(){
int sum;
sum = 500 + 15;
printf("The sum of 500 and 15 is %d\n", sum);
Lesson 3
Screen Output:

The sum of 500 and 15 is 515


_
Lesson 3
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main(){
int sum, value;
sum = 10;
value = 15;
printf("Values : %d\t%d\n", sum, value);
Lesson 3
Screen Output:

Values : 10 15
_
Lesson 3
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main(){
int sum, value;
sum = 5;
value = 10;
printf("Value 1 : %d\nValue 2 : %d\n", sum, value);
}
Lesson 3

Screen Output:

Value 1 : 5
Value 2 : 10
_
Lesson 3
Relational Operators

>  Greater Than


<  Less Than
==  Equal To
>=  Greater Than or Equal
<=  Less Than or Equal
!=  Not Equal
Lesson 3

Logical Operators

&&  Logical AND


||  Logical OR
!  Logical NOT
Lesson 3
Truth Table for the Logical Operators
AND
P Q P && Q
0 0 0
0 1 0
1 0 0
1 1 1

Note: 0 is False, 1 is True.


Lesson 3
Truth Table for the Logical Operators
OR
P Q P || Q
0 0 0
0 1 1
1 0 1
1 1 1 NOT
P !P
0 1
1 0
Lesson 3
Example Program 1: Screen Output:

#include <stdio.h> The sum is 25_


#include <conio.h>
#include <iostream>
using namespace std;
main(){
int n1, n2;
n1 = 10;
n2 = 15;
printf(“The sum is %d”, n1 + n2);
}
Lesson 3
Example Program 2: Screen Output:
The product is 150_
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main(){
int n1= 10, n2 = 15;
printf(“The product is %d”, n1 * n2);
}
Example Program 3:
#include <stdio.h> Output:
#include<conio.h> Number 1 is 10
#include<iostream> Number 2 is 15 The product is 150
using namespace std;
main(){
int n1 = 10, n2 = 15, res;
res = n1 * n2;
printf(“Number 1 is %d\n”,n1 );
cout<<“Number 2 is ”<<n2;
printf(“ The product is %d”, res);
}
Lesson 3

EXERCISES
Simulate/ trace the following programs
and find the screen output.
Lesson 3
Program 1

#include <stdio.h>
#include <conio.h>
#include <iostream>
Using namespace std;
void main(){
int a = 20, b = 5;
cprintf(“Series 1\n”);
a = a + b;
printf(“%d %d %d”, a, b, a * b);
}
Lesson 3
Program 2
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main(){
char l = ‘c’;
int X = 12, Y = X + 3;
Y = Y + 5;
printf(“Series 2\n”);
printf(“Values : %d and %d”, X * X, Y );
}
Lesson 3
Program 3
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main(){
int c= 2, d = 4, e= 5;
cprintf(“Series %d”,d);
d = c + e; c = d + c;
cprintf(“Lists : %d %d %d”, c, d, e);
}
Lesson 3
The Input Functions
The scanf() function
-- it is a function in C language used to accept input
from the user.
Syntax:
scanf(“<formatter>”,&variable_name);
Example:
scanf(“%d”,&num); scanf(“%d”,&x);

Note: Before you use a variable in a scanf() function, it


should be define first.
Lesson 3

Example Program 1:

A program will accept a number and it


will display on the screen the number
input.
Lesson 3
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int a;
printf(“Input a number : “);
scanf(“%d”,&a);
printf(“The number is %d”, a);
getche();
}
Example Program 2:

A Program that will accept a number


and will compute the square of the input
number.
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int x;
printf(“Input a number : “);
scanf(“%d”,&x);
printf(“The square of %d is %d”, x, x*x);
getche();
}
The <iostream.h>
• Inclusion of this file in a program able to
use the basic command such as cout<<“ ”;
and the cin>>; functions and some other
functions.
The cout function
• It is another way to display text and values
of variables aside from cprintf() and printf()
functions. Before using this function
iostream.h header file should be include in
the file inclusion section of the program.
Syntax:
cout<<“<text>”;
cout<<variable_name;

Examples:
cout<<“Hello World!”;
cout<<x;
Example Program:
#include <stdio.h>
#include<conio.h>
#include<iostream.h>
void main(){
clrscr();
cout<<“Welcome to C/C++”<<endl;
cout<<“\”Programming is easy”<<endl;
cout<<“but it needs patience and”<<endl;
cout<<“determination...\””;
}
cout<<“Welcome to C/C++”<<endl;
cout<<“\”Programming is easy”<<endl;
cout<<“but it needs patience and”<<endl;
cout<<“determination...\””;

Output:
Welcome to C/C++
“Programming is easy
but it needs patience and
determination…”
The cin function
It is another way to accept input from
a user aside from the scanf() function.
cin function is inside the iostream.h
header file, therefore before using it
make sure that iostream.h header file
is included in the program.
Syntax:

cin>>variable_lists;

In this case, modifiers will not be used


unlike the scanf() funstion.

Examples:
cin>>x;
cin>>n1>>n2;
Example Program 1:

Write a Program in C/C++ that will


accept a number and it will display on the
screen the number input. Use the
functions cout and cin.
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main(){
clrscr();
int a;
cout<<“Input a number : “;
cin>>a;
cout<<“The number is “<<a;
getche();
}
Example Program 2:

Write a Program in C/C++ that will


accept a number and will compute the
square of the input number. Use the
functions cout and cin.
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main(){
clrscr();
int x;
cout<<“Input a number : “;
cin>>x;
cout<<“The square of “ <<x<<“is ”<<x*x;
getche();
}
Lesson 3

LABORATORY
EXERCISES
Solve the following machine problems.
Save your programs in a USB following
the filenames indicated in each of the
problems.
Lesson 3

1. Create a program that accept two numbers and will


compute and display the sum, the product and its
difference.

2. Create a program that converts a given inches to its


equivalent foot. Note: 12 inches is equal to 1 foot.

3. Create a program that computes the area of a triangle.


Inputs of the program is the base and the height.

Formula: Area of Triangle = ½ Base Height


Lesson 3
1. Create a program that accept two numbers and will compute and
display the sum, the product, the difference, and its Quotient.

SAMPLE OUTPUT:

Input 1st Number : 20


Input 2nd Number : 5
SUM: 25
PRODUCT: 100
DIFFERENCE: 15
QUOTIENT: 4.00
EX1.CPP
Lesson 3
2. Create a program that converts a given inches to its equivalent foot. Note: 12
inches is equal to 1 foot.

SAMPLE OUTPUT:

INCHES TO FOOT CONVERSION PROGRAM

Input value for inches : 24


24 inches is 2 ft.

EX2.CPP
Lesson 3
3. Create a program that computes the area of a triangle. Inputs of the program
is the base and the height.
Formula: Area of Triangle = ½ Base Height

SAMPLE OUTPUT:
COMPUTATION OF AREA OF TRIANGLE

Input the Base : 5


Input the Height : 10
AREA : 25 sq. units

EX3.CPP

You might also like