Professional Documents
Culture Documents
Structured Programming Slide Week2
Structured Programming Slide Week2
Structured Programming Slide Week2
2/19/2013
Data
All C++ programs consist of commands and
data
2/19/2013
Description
Size
char
Character
1 byte
short
Short integer
2 bytes
int
Integer
4 bytes
long
Long integer
4 bytes
bool
Boolean values
1 byte
True or false
float
Floating point
number
4 bytes
double
Double precision
floating pt no.
8 bytes
long
double
Variable Declaration
Example
2/19/2013
variable = expression
variable = expression
2 valid ways
2 valid ways
1) int x=7;
2) int x (7);
What is a string?
A variable that can store non-numerical values
more than 1 single character
requires string.h file and access to namespace
std
can be assigned with string value
i.e. to assign the words I am a C++ beginner to
string type data myword
1) int x=7;
2) int x (7);
Examples
//to assign numeric values to variables
//to assign string values to variables
#include <iostream>
using namespace std;
#include <iostream>
#include <string>
int main ()
using namespace std;
{
int x=10;
int main ()
{
int y(3);
int sum;
string Mysentence;
Mysentence=This is my first sentence
x=x+5;
cout << Mysentence << endl;
sum=x-y;
cout<< sum;
Mysentence=This is my second sentence
cout << Mysentence << \n;
return 0;
}
return 0;
}
2/19/2013
Literals
Integer Numerals
1) integer numerals
2) floating point numerals
3) characters
4) strings
5) Boolean values
non-numerical literals
i.e X, g
String enclosed in double quotation marks
i.e. How are you?,this is my first C++ program
Quotation marks ( , ) are to distinguish from
variable identifier, keywords and reserved
keywords
2/19/2013
ASCII Characters
memory
Character literals include:
All alphabets i.e A
All numbers i.e 10
Special characters i.e ASCII characters /x7A
Escape Character/Sequence
Character
Indication
\a
\b
Backspace
\n
Newline
\r
Carriage retun
\t
Tab
\v
Vertical tab
\f
\024
Octal numbers
\x6c
Hexadecimal numbers
#include <iostream>
using namespace std;
main ()
{
char f;
f = Q + 3;
cout << f;
return 0;
}
#include <iostream>
using namespace std;
main ()
{
int i;
i= K - 5;
cout << i;
return 0;
}
2/19/2013
String Literals
Boolean Literals
1. True
Defined Constant
Declared Constant
using preprocessor
directive #define
placed before the
function
does not require
semicolon (;) at the end
#include <iostream>
#include <cmath>
using namespace std;
#define EXP 2.718
#define NewAlarm \a
int main ()
{
float p=pow(10, EXP);
cout << p<<NewAlarm;
return 0;
}
2. False
Expressed in C++ as values of type bool
2/19/2013
input/output library
several typedefs
several typedefs
several typedefs
2/19/2013
arrays of values
<random> random number generators and distributions
<numeric> numeric operations on values in containers
<ratio> (since C++11) compile-time rational arithmetic
<cfenv> (since C++11) floating-point environment access
functions
Utilities Library
Utilities Library