C++ Basics: Vks-Learning Hub
C++ Basics: Vks-Learning Hub
C++ Basics: Vks-Learning Hub
C++ Basics
VKS-LEARNING HUB
Creating variable
A variable is a name given to a memory location to store a
value and it represents a value in a program. The value
assigned to the variable name may change during execution
of program. The program can always access the current
value of the variable by referring to its name.
char a1;
char ans, choice, section; Creating a variable is a
char name[30]; statement in C++ and every
char subject[20], country[25]; C++ statement is terminated
by a semi-colon (;).
int roll;
String is not a fundamental
int flatno, number, cellno, phone;
data type but still examples
are given how to create string
float average;
variables. An array of
float area, length, marks;
character is used to create a
string variable. An example is
double temperature;
given below:
double radius, price, rate;
char name[30];
VKS-LEARNING HUB
Assignment Operator
Value is assigned to a variable by using assignment operator.
Using assignment operator, value is stored in a variable when
writing a program. Using assignment operator, value is copied to
a variable.
Rule: cout<<Value;
cout<<Value1<<Value1<<Value3…;
cout<<Value<<endl;
Usage of cout
cout<<"Vinay Ahuja";
cout<<11;
cout<<'A'; Produces output like
cout<<141.23 Vinay Ahuja11A78.5
Usage of cout
cout<<"Vinay Ahuja"<<11<<'A'<<78.5;
VKS-LEARNING HUB
cout<<"Vinay Ahuja"<<11<<'A'<<78.5<<endl;
VKS-LEARNING HUB
VKS-LEARNING HUB
VKS-LEARNING HUB
Token
Building block of a program is called a token. It is also
called program element. Tokens of a C++ program can
be classified as Keyword, Identifier, Constant, Operator,
String and Comment.
VKS-LEARNING HUB
Keyword
It is component of a program which has special meaning
for the C++ compiler.
In Borland C++ editor keyword appear in bold face.
C++ compiler contains list of all the keywords. List of
keywords vary from to compiler to compiler.
A keyword cannot be redefined. List of commonly used
C++ keywords are given below:
VKS-LEARNING HUB
Identifier
Identifier is a component of a program which is identified by a
C++ compiler. There are two broad categories of identifiers:
Constant:
Constant: A constant is a program element whose value
remains same through the program. Examples of different
types of constants are given below:
Constants are of five (5) types:
char constant: Character constant
int constant: Integer constant
float constant: Single precision floating point constant
double constant Double precision floating point constant
String constant
VKS-LEARNING HUB
Operator:
Operators are used in C++ to carry out various functions. Mostly
operators are used in arithmetic calculations and in logical
expressions. But operators may be used for dynamic memory
management. An operator in C++ can be unary, binary and ternary.
Examples of operators are given below
VKS-LEARNING HUB
Logical Operator
Comment:
Non executable statements of a C++ program are called Comments.
Comments are also known as Remarks.
A Comment is completely ignored by a compiler.
No code is generated for a Comment.
Comment is a good tool for Debugging.
C++ supports two types of Comments:
Single line Comment: Single Line Comment starts with pair of forward
slash (//) and till the end of line is considered as a Comment. Examples of
Single Line Comment are given below:
// single line comment
// comment in C++ style
Compiler directive:
Instruction given to the compiler.
Compiler directive is also called Pre-processor.
It is called Pre-Processor because instruction to the
compiler given before the processing starts.
Every Compiler Directive begins with hash (#).
Examples of Compiler Directives are given below: