Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Variables, Data Types

Exercise

Write a C++ program that outputs the following text on


screen:
Oh what
a happy day!
Oh yes,
what a happy day!
Use the manipulator endl where appropriate.
Defining Variables

 Variables: is a location in the computer’s memory where a


value can be stored for use by a program.
 A variable must be defined before you can use it in a
program.
 Syntax: type name1 ,name2 , ...;
 Example:
char c;
int i, counter;
double x, y, size;
Valid Names

 Rules must apply when creating names for variables and


functions also known as identifiers:
 The name contains a series of letters (ASCII code), numbers,
or underscore characters ( _ ).
 C++ is case sensitive; upper- and lowercase letters are
different.
 The first character must be a letter or underscore.

 There are no restrictions on the length of a name.

 C++ keywords are reserved and cannot be used as names.


Data Types

 A program can use several data to solve a given


problem, for example, characters, integers, or floating-
point numbers.
 The computer uses different methods for processing
and saving data, the data type must be known.
 The type defines:
 the internal representation of the data.
 the amount of memory to allocate.
The char and wchar_t Types

 These types are used for saving character codes.


 Example: 'A', 'a', '0', '*', '+', '$', '&', ' '
 A character code is an integer associated with each
character.
 The letter A is represented by code 65.
The char and wchar_t Types

 The char (character) type is used to store character


codes in one byte (8 bits).
 ASCII code (American Standard Code for Information
Interchange) characters.
 The wchar_t (wide character type) type comprises at
least 2 bytes (16 bits)
 Unicode characters. Unicode is a 16-bit code containing
codes for approximately 35,000 characters in 24
languages.
Integral Types

 The types short, int, and long are available for operations
with integers.
 Example: -6728, -67, 0, 78, 36782, +763
 These types are distinguished by their ranges of values.
 The int (integer) type is tailor-made for computers and adapts
to the length of a register on the computer.
 For 16-bit computers, int is thus equivalent to short.
 For 32-bit computers int will be equivalent to long.
The signed and unsigned Modifiers

 The short, int, and long types are normally interpreted as


signed with the highest bit representing the sign.
 Integral types can be preceded by the keyword unsigned.
 Example unsigned int.
 The amount of memory are the same but the range of values
changes.
 The char type is also normally interpreted as signed, the
signed keyword is available. Thus three types are available:
 char, signed char, and unsigned char.
The Type bool

 The result of a comparison or a logical association using AND or


OR is a boolean value, which can be true or false.
 The bool type represents boolean values.
 The type bool can either be true or false
 true is represented as 1
 false is represented as 0.
Floating-Point Types

 Floating-point numbers is a numbers with a fraction


part or decimal point,
 The following three types are available for calculations
involving floating-point numbers:
 float for simple accuracy
 double for double accuracy
 long double for high accuracy
Input statements

cin >> variable-name;


Meaning: read the value of the variable called
<variable-name> from the user

Example:
cin >> a;
cin >> b >> c;
cin >> x;
cin >> my-character;
Output statements

cout << variable-name;


Meaning: print the value of variable <variable-name> to the user
cout << “any message “;
Meaning: print the message within quotes to the user
cout << endl;
Meaning: print a new line
Example:
cout << a;
cout << b << c;
cout << “This is my character: “ << my_character
<< “ he he he”
<< endl;
#include <iostream>
using namespace std;
int main(){
int feet;
int inches;
cout << "Enter two integers separated by spaces: ";
cin >> feet >> inches;
cout << endl;
cout << "Feet = " << feet << endl;
cout << "Inches = " << inches << endl;
return 0;
}

You might also like