Вы находитесь на странице: 1из 9

Variable declaration

type variable-name;
Meaning: variable <variable-name> will be a variable of type
<type>
Where type can be:
 int //integer
 double //real number
 char //character
Example:
int a, b, c;
double x;
int sum;
char my-character;
2
Identifiers
A valid identifier is a sequence of one or more letters,
digits or underscore characters (_). Neither spaces nor
punctuation marks or symbols can be part of an
identifier. Only letters, digits and single underscore
characters are valid. In addition, variable identifiers
always have to begin with a letter. They can also begin
with an underline character (_ ), but in some cases
these may be reserved for compiler specific keywords.
C++ keywords
 Keywords appear in blue in Visual C++.
 Each keyword has a predefined purpose in the
language.
 Do not use keywords as variable and constant
names!!
 The complete list of keywords is on page 673 of the
textbook.
 We shall cover the following keywords in this class:
C++ keywords
asm, auto, bool, break, case, catch, char, class, const,
const_cast, continue, default, delete,
do, double, dynamic_cast, else, enum, explicit, export,
extern, false, float, for, friend, goto,
if, inline, int, long, mutable, namespace, new, operator,
private, protected, public, register,
reinterpret_cast, return, short, signed, sizeof, static,
static_cast, struct, switch, template,
this, throw, true, try, typedef, typeid, typename, union,
unsigned, using, virtual, void,
volatile, wchar_t, while
Primitive Data Types
Name Size (bytes) Description Range
char 1 character or eight bit integer signed: -128..127
unsigned: 0..255

short 2 sixteen bit integer signed: -32768..32767


unsigned: 0..65535

long 4 thirty-two bit integer signed: -231 .. 231-1


unsigned: 0 .. 232

int * (4) system dependent, likely four bytes or signed: -32768..32767


thirty-two bits unsigned: 0..65535

float 4 floating point number 3.4e +/- 38


(7 digits)

double 8 double precision floating point 1.7e +/- 308


(15 digits)

long double 10 long double precision floating point 1.2e +/- 4932
(19 digits)

bool 1 boolean value {0,1}


false → 0, true → 1
Scope of variables

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

int total; Global variable


int compute(int number1, int number2){
total = number1+number2;
return total;
}

void main(){
int num1=10;
int num2=9; Local variable
int answer = compute(num1,num2);
cout<<"Answer is ";
cout<< answer;
_getch();
}
Input and Output

#include<iostream> void main(){


#include "conio.h" int num1;
using namespace std; int num2;
cout<<"Input number1: ";
int total; cin>> num1;
cout<<"\n";
int compute(int number1, int number2){ cout<<"Input number2: ";
total = number1+number2; cin>> num2;
return total; cout<<"\n";
} int answer = compute(num1,num2);
cout<<"Answer is ";
cout<< answer;
_getch();
}
Constants
#include <iostream>
#include "conio.h"; Constants are
expressions with a
using namespace std; fixed value.
#define PI 3.14159 Defined constants (#define)
#define NEWLINE '\n' You can define your own
names for constants that you
void main (){ use very often without having
double r=5.0; // radius
to resort to memory
double circle;
consuming variables, simply
circle = 2 * PI * r; by using the #define
cout << circle; preprocessor directive.
cout << NEWLINE;
_getch();
}

Вам также может понравиться