Variables, Data types
and Input/output
constructs
Lecture # 2
Course Instructor:
Dr. Afshan Jamil
Outline
Layout of
simple ++
program
Variables Identifiers
Assignment
statements
Uninitialized
variables
Output
using cout
Input using
cin
Escape
sequences
Data types Integers
Floating
point
Type char
Class stringType bool
Arithmetic
operators and
expressions
Comments
Naming
constants
Layout of a simple C++ program
Sample Program
•// This is a simple C++ program.
#include <iostream>
using namespace std;
int main()
{
cout<<“Welcome to Computer Programming
course”;
return 0;
}
Names: Identifiers
•Identifiersareusedasnamesforvariablesand
otheritemsinaC++program.
•Tomakeyourprogrameasytounderstand,you
shouldalwaysusemeaningfulnamesforvariables.
•Rulesfornamingvariables:
–You cannot use a C++ keyword (reserved word)
as a variable name.
–Variable names in C++ can range from 1 to 255
characters.
–All variable names must begin with a letter of
the alphabet (a-z, A-Z) or an underscore( _ ).
CONTD…
•After the first initial letter, variable names
can also contain letters and numbers.
•No spaces or special characters allowed.
•C++ is case Sensitive. Uppercase characters
are distinct from lowercase characters.
•Examples:
–A,a_1,x123(legal)
–1ab,da%,1-2,(notacceptable)
–Test,test,TEST(case-sensitive)
CONTD…
Name Escape
sequence
Description
New line
\n
Cursor moves to next line
Horizontal tab
\t
Cursor moves to next tab stop
Beep
\a
Computer generates a beep
Backslash
\\
Backslash is printed
Single quote
\’
Single quotation mark is printed
Double quote
\”
Double quotation mark is printed
Return
\r
Cursor moves to beginning of
current line
Backspace
\b
Cursor moves one position left
Data Types
•Datatypesareusedtotellthevariablesthetype
ofdataitcanstore.
•WheneveravariableisdefinedinC++,the
compilerallocatessomememoryforthatvariable
basedonthedata-typewithwhichitisdeclared.
•Everydatatyperequiresadifferentamountof
memory.
Floating point types
•The floating-point family of data types represents
number values with fractional parts.
•They are technically stored as two integer values:
amantissaand anexponent.
•They are always signed.
•A floating_pointnumber can also be a scientific
number with an "e" to indicate the power of 10:
Type bool
•ExpressionsoftypeboolarecalledBoolean
aftertheEnglishmathematicianGeorgeBoole,
whoformulatedrulesformathematicallogic.
•Booleanexpressionsevaluatetooneofthetwo
values,trueorfalse.
•Booleanexpressionsareusedinbranchingand
loopingstatements.
Example
#include <iostream>
using namespace std;
intmain() {
bool isCodingFun= true;
int i=100;
float f=23.6;
char ch=‘h’;
doubled =12E4;
strings=“Hello”;
cout << isCodingFun<< endl;
cout << “value of int=“<<i<<endl;
CONTD…
cout << “value of float=“<<f<<endl;
cout << “value of double=“<<d<<endl;
cout << “value of char=“<<ch<<endl;
cout << “value of string=“<<s<<endl;
cout<<“ASCII value of
char=“<<int(ch)<<endl;
cout<<“Integer to char=<<“char(i)<<endl;
return0;
}
CONTD…
•OUTPUT:
1
value of int=100
value of float=23.6
value of ouble=1.2e+013
value of char=h
value of string=hello
ASCII value of char=104
Integer to char=d
sizeof() Function
•Thesizeofisakeyword,itisacompile-time
operatorthatdeterminesthesize,inbytes,ofa
variableordatatype.
•Thesizeofoperatorcanbeusedtogetthesize
primitiveaswellasuserdefineddatatypes.
•Thesyntaxofusingsizeofisasfollows:
•sizeof(datatype)
Example
#include<iostream>
usingnamespacestd;
intmain()
{
cout <<"Size of char : "<<sizeof(char)<<endl;
cout <<"Size of int : "<<sizeof(int)<<endl;
cout <<"Size of short "<<sizeof(shortint)<<endl;
cout <<"Size of long int : "<<sizeof(longint)<<endl;
cout <<"Size of long long: "<<sizeof(long long)<<endl;
cout <<"Size of float : "<<sizeof(float)<<endl;
cout <<"Size of double : "<<sizeof(double)<<endl;
return0;
}