You are on page 1of 8

COSC-211

Introduction to Computer Program


and its Applications

LABORATORY MANUAL 2

Variables
A variable is a named location in memory that is used to hold a value that
may be modified by the program. All variables must be declared before they
can be used. The general form of a declaration is

type variable_list;

Here, type must be a valid data type plus any modifiers, and variable_list
may consist of one or more identifier names separated by commas. Here are
some declarations:
int i,j,l;
short int si;
unsigned int ui;
double balance, profit, loss;

Data Types
Data type defines the the size and the type of data that is going to store in
it. There are Five basic data types in c++ and these are int, float, double,
char, and void. The last data type usually used in return types to say that
no value is going to be returned. Void also means valueless. The below table
shows these basic data types with four modifiers signed, unsigned, short,
and long.

Type Typical Size in Minimal Range Examples


Bits
char 8 −127 to 127 char x=’y’;
unsigned char 8 0 to 255 unsigned char x;
signed char 8 −127 to 127 signed char y;
int 16 or 32 −32,767 to 32,767 int x=10;
unsigned int 16 or 32 0 to 65,535
signed int 16 or 32 same as int
short int 16 −32,767 to 32,767
unsigned short int 16 0 to 65,535
signed short int 16 same as short int
long int 32 −2,147,483647 to
2,147,483647
signed long int 32 same as long int
unsigned long int 32 0 to 4,294,967295
float 32 Six digits of precision float x=0.30;
double 64 Ten digits of precision double
y=0.3405679848
long double 80 Ten digits of precision
bool 1 0 or 1 bool x=true;
or
bool x=1;
Identifier Names

While mathematicians are content with giving their variables one-letter


names like x, programmers should use longer, more descriptive variable
names. Names such as altitude, sum, and user_name are much better than
the equally permissible a, s, and u. A variable’s name should be related to its
purpose within the program. Good variable names make programs more
readable by humans. C++ has strict rules for variable names.
A variable name is one example of an identifier. An identifier is a word used
to name things. One of the things an identifier can name is a variable. We
will see in later that identifiers name other things such as functions and
classes. Identifiers have the following form:

• Identifiers must contain at least one character.


• The first character must be an alphabetic letter (upper or lower case) or
the underscore
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_
• The remaining characters (if any) may be alphabetic characters (upper or
lower case), the underscore,
or a digit
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456
789
• No other characters (including spaces) are permitted in identifiers.
• A reserved word cannot be used as an identifier.

Here are some examples of valid and invalid identifiers:


• All of the following words are valid identifiers and so qualify as variable
names: x, x2, total, port_22, and FLAG.
• None of the following words are valid identifiers: sub-total (dash is not a
legal symbol in an identifier), first entry (space is not a legal symbol in an
identifier), 4all (begins with a digit), #2 (pound sign is not a legal symbol in
an identifier), and class (class is a reserved word).

Reserved Words

C++ reserves a number of words for special use that could otherwise be used
as identifiers. Called reserved words or keywords, these words are special
and are used to define the structure of C++ programs and statements.
The size of operator
The sizeof operator returns the size of any data type.

#include <iostream>

using namespace std;

int main() {

cout << "Size of char : " << sizeof(char) << endl;

cout << "Size of int : " << sizeof(int) << endl;

cout << "Size of short int : " << sizeof(short int) << endl;

cout << "Size of long int : " << sizeof(long int) << endl;

cout << "Size of float : " << sizeof(float) << endl;


cout << "Size of double : " << sizeof(double) << endl;

return 0;

Task 1: Write code to check if 7777777777.30303030 can be stored in float or double type
variable. Also print this number on console. Write code and output below.

Task 2: Write code to make a variable xyz of char data type and assign value -120. Print this
variable on console and check which character gets printed. Write code and output both below.

#include <iostream>
#include <conio.h>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input
loop */

int main(int argc, char** argv) {


char xyz=-120;
cout<<xyz;
return 0;
}
Task 3: Write code and make a variable test-var of type int and check what happens. Write the
reason in case there is some error.

#include <iostream>
#include <conio.h>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input
loop */

int main(int argc, char** argv) {


int test-var;
return 0;
}

Reason;
[Error] expected initializer before '-' token
Putting a dash is not a symbol in an identifier.

Task 4: Write code to make a variable with name c of type char. Try to assign value out of its
value range of values which is -128. Run the code and check what will happen.
#include <iostream>
#include <conio.h>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input
loop */

int main(int argc, char** argv) {


char c= -129;
cout<<c
return 0;
}
[Warning] overflow in implicit constant conversion [-Woverflow]
[Error] expected ';' before 'return'