Faizan Ali
contact:- 03235356409
email:- ali.faizan0009@gmail.com
Naming Variables:
We can use any combination of letters and numbers as a variable name but it must start with a letter.we can use underscore(_) as a letter in variable name and can begin with it.But identifiers(any user defined name given to the program element) beginning with an underscore are reserved.And identifiers beginning with an underscore followed by a lower case letter are reserved for file scope identifiers.Therefore using underscore as a starting letter is not desirable.
Example:
Asim and asim are different identifiers because upper and lower case letters are treated as different identifiers.
Rules:
The rules which are required for naming variables are:
A variable name can only contain Alphabets and numbers.
A variable name cannot contain spaces.
Identifier name cannot start with a digit.
Keywords cannot be used as a name.
Upper case and lower case letters are treated different.
Special Characters are not allowed except underscore(_).
A variable name must start with an alphabet or underscore(_).
Global Identifier cannot be used as “Identifier”.
Variable Types:
There are many built-in data types in C/C++.There are following basic data types of variables in C++ .
Type Description
bool Stores either value true or false.
char Typically a single octet(one byte). This is an integer type.
int The most natural size of integer for the machine.
float A single-precision floating point value.
double A double-precision floating point value.
void Represents the absence of type.
wchar_t A wide character type.
Constants:
constant is a data item whose value does not change.To change the constant value, we have to make changes in the program.
Types af Constants:
a. Numeric Constants
i. Integer Constant
1. Decimal Integer constant
2. Octal integer constant
3. Hexadecimal Integer constant
ii. Real or floating point Constant
b. Non Numeric Constants:
i. Character Constant
ii. String Constant
a. Numeric constant:
Numeric constants consists of numbers as indicative from name.There are two types of numeric constants namely Integer Constant and Real or floating point Constants.
i. Integer constant:
Any whole number value is an integer.An integer constant refers to a sequence of digits without a decimal point.An integer having minus sign is considered as negative constant.
Examples: 0
-33 (negative constant)
32767
Integer constants are further divided into three types namely,
a) Decimal integer constant
b) Octal integer constant
c) Hexadecimal integer constant
a) Decimal Integer constant (base 10)
It consists of any combinations of digits taken from the set 0 through 9, preceded by an optional – or + sign.
The first digit must be other than 0.Embedded spaces, commas, and non-digit characters are not allowed between digits.
For Example:
0
32767
-9999
-23
All above examples are valid.
On the other hand.
12,245 // Illegal character (,)
10 20 30 // Illegal character (blank space)
The above both examples are invalid and reason is mentioned above.
b) Octal Integer Constant (base 8)
It consists of any combinations of digits taken from the set 0 through 7.If a constant contains two or more digits, the first digit must be 0.In programming, octal numbers are used.
For Example:
037
0
0435
All above examples are valid.
On the other hand.
0786 // Illegal digit 8
123 // Does not begin with zero
01.2 // Illegal character (.)
The above both examples are invalid and reason is mentioned above in comments.
c) Hexadecimal integer constant:
It consists of any combinations of digits taken from the set 0 to 7 and also a to f (either uppercase or lowercase).The letters a to f (or A to F) represent the decimal quantities from 10 to 15 respectively.This constant must begin with either 0x or 0X.In programming, hexadecimal numbers are used.
For Example:
0x (valid)
0X1 (valid)
0x7F (valid)
while,
0xefg invalid because illegal character 'g'.
123 invalid because Does not begin with 0x.
Rules for constructing Integer constants:
An integer constant must have at least one digit.
It must not have a decimal point
It can be either positive or negative.
If no sign is present, it is assumed to be positive.
Commas or blanks are not allowed within an integer constant.
ii. Real or Floating Constant:
The second type of numeric constant is real or floating constant.Any number containing a decimal point will be consideres as a floating-point number. Note that you have to put at least one digit after the decimal point:
For Example:
2.0
3.75
-12.6112
.
Exponential Real Constant:
Numbers with fractional part are called real constants. These are often called as floating point constants. Real constants are generally represented in two forms are fractional form and exponential form. Fractional form is the general form of representing a floating point value, exponential or scientific form is used when the value is so big or so small. Exponential form has two parts. The part before appearing to E is called mantissa and the part following E is called exponent. Here the mantissa must be multiplied by 10^exponent for example 345.25E-4 is equal to 345.25*10-4 and 4.56E4 is equal to 4.56*104.
Real constants in C/C++ language:
Rules for real constants in fractional form:
It must be a number
It can be either +ve or –ve but by default it is a positive number
It must have one decimal point.
No commas and spaces are used in a number
Examples:
+234.45
-34.02
12.0
0.0
Rules for real constants in exponential form:
It must be a number
The mantissa and exponential parts must be separated with letter e Or E
Both mantissa and exponential parts may be positive or negative but by default these are positive
It may have only a single decimal point.
Examples:
4.56E4
-5.6E-6
-0.4E7
-10.6E-2
b) Non Numeric Constant:
Non Numeric Constants are constants that does not consist of numerics i.e:
i. Character constant:
Character constant consist of characters.They are enclosed in single quotes(apostrophes).It can be a plain character.It occupies one byte.Every character constant does not end up with a NULL character.
e.g:
'v','f','x' etc.
it may be an escape sequence.
e.g:
'\t' ,'\n' ,'\b' etc.
ii. String Constants:
They are enclosed in double quotes. A single string constant occupies two bytes.Every string constant ends up with a NULL character which is automatically assigned (before the closing double quotation mark) by the compiler.
e.g:
"hello" ,"I am student" etc.
C/C++ Operators:
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides the following types of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Arithmetic Operators:
There are following arithmetic operators supported by C++ language:
Operator Description Example:if a=10 and b=20 then
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0
++ Increment operator, increases integer value by one A++ will give 11
-- Decrement operator, decreases integer value by one A-- will give 9
Relational Operators:
There are following relational operators supported by C++ language
Operator Description Example:if a=10 and b=20 == Sign of 'equality' Checks if the values of two operands
are equal or not,
if yes then condition becomes true. (A == B) is not true.
!= Sign of 'not equal'Checks if the values of two operands are
equal or not,if values are not equal then condition become (A != B) is true.
true.
> Checks if the value of left operand is greater than the (A > B) is not true.
value of right operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the
value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or
equal to the value of right operand, if yes then condition (A >= B) is not true.
becomes true. (A >= B)
<= Checks if the value of left operand is less than or equal
to the value of right operand, if yes then condition (A <= B) is true.
Logical Operators:
There are following logical operators supported by C++ language.
Operator Description Example:if a=1 and b=0
&& Called Logical AND operator. If both the operands are
non-zero, then condition becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operand
is non-zero, then condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical
state of its operand. If a condition is true, then Logical !(A && B) is true.
NOT operator will make false.
Bitwise Operators:
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ are as follows:
p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The Bitwise operators supported by C++ language are listed in the following table. Assume variable A holds 60 and variable B holds 13, then:
Operator Description
& Binary AND Operator copies a bit to the result if it exists in both operands.
| Binary OR Operator copies a bit if it exists in either operand.
^ Binary XOR Operator copies the bit if it is set in one operand but not both.
~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
<< Binary Left Shift Operator.The left operands value is moved left by the number of bits specified by the right operand.
>> Binary Right Shift Operator.The left operands value is moved right by the number of bits specified by the right operand.
Assignment Operators:
There are following assignment operators supported by C++ language:
Show Examples
Operator Description
= Simple assignment operator, Assigns values from right side operands to left side operand.
+= Add AND assignment operator, It adds right operand to the left operand
and assign the result to left operand.
-= Subtract AND assignment operator, It subtracts right operand from the
left operand and assign the result to left operand.
*= Multiply AND assignment operator, It multiplies right operand with the
left operand and assign the result to left.
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left.
%= Modulus AND assignment operator, It takes modulus using two operands and
assign the result to left operand.
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2
Misc Operators:
There are few other operators supported by C++ Language.
Operator Description
sizeof sizeof operator returns the size of a variable. For example, sizeof(a), where a is integer, will return 4.
Condition ? X : Y Conditional operator. If Condition is true ? then it returns value X : otherwise value Y
, Comma operator causes a sequence of operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list.
. (dot) and -> (arrow) Member operators are used to reference individual members of classes, structures, and unions.
Cast Casting operators convert one data type to another. For example, int(2.2000) would return 2.
& Pointer operator & returns the address of an variable. For example &a; will give actual address of the variable.
* Pointer operator * is pointer to a variable. For example *var; will pointer to a variable var.
C/C++ Statements:
C++ statements are the program elements that control how and in what order objects are manipulated.
Categories of Statements
Expression statements:
These statements evaluate an expression for its side effects or for its return value.
Null statements:
These statements can be provided where a statement is required by the C++ syntax but where no action is to be taken.
Compound statements:
These statements are groups of statements enclosed in curly braces ({ }). They can be used wherever a single statement may be used.
Selection statement:
These statements perform a test; they then execute one section of code if the test evaluates to true (nonzero). They may execute another section of code if the test evaluates to false.
Iteration statements:
These statements provide for repeated execution of a block of code until a specified termination criterion is met.
Jump statements:
These statements either transfer control immediately to another location in the function or return control from the function.
Declaration statements:
Declarations introduce a name into a program.Declarations provides more detailed information about declarations.
C/C++ Expressions:
An expression is "a sequence of operators and operands that specifies a computation" (that's the definition given in the C++ standard).
Example:
6 * 7
a + b * 3
sin(3) + 7
a > b
a ? 1 : 0
func()
if(a > b)
while(true)
These all are the examples of expressions.Actually expressions are made is part of a statement, OR a statement itself.
int x; is a statement and expression.