HRISHI COMPUTER EDUCATION C Programming
©Reserved by HrishiOnlineBuddhi
Visit Our Terms & Condition for content usage Policy
HRISHI COMPUTER EDUCATION C Programming
Assignment No. 3
Topic: Token, Data-Type, Expression
1.Explain Increment and Decrement operator.
2.Explain Conditional and Bitwise operator.
3.Explain Special operators in C++.
4.Explain Scope resolution operator.
5.Explain member dereferencing operator.
6.Explain memory management operator (new and delete)
7.Explain operator precedence and their associativity.
8.Explain Automatic Conversion (Implicit Conversion)
9.Explain Caste conversions (Explicit Conversion) / Type Cast Operator
10.Explain List Expression and their types.
1. Explain Increment and Decrement Operator
These are unary operators used to increase or decrease a value by 1.
● Increment (++): Adds 1
● Decrement (--): Subtracts 1
They can be used in two ways:
● Prefix: ++a / --a → changes value first, then uses it
● Postfix: a++ / a-- → uses value first, then changes it
Example:
cpp
CopyEdit
int a = 5;
cout << ++a; // 6 (prefix)
cout << a++; // 6 then a becomes 7 (postfix)
2. Explain Conditional and Bitwise Operator
Conditional operator (?:): A ternary operator used for decision-making.
Syntax:
cpp
CopyEdit
(condition) ? true_value : false_value;
Example:
cpp
CopyEdit
int max = (a > b) ? a : b;
Bitwise operators: Used for bit-level operations.
Operator Description
& AND
` `
^ XOR
~ NOT
<< Left shift
>> Right shift
3. Explain Special Operators in C++
Special operators in C++ include:
● sizeof: Returns size of data type or variable
● typeid: Used to get type information at runtime
● Comma (,) operator: Separates multiple expressions
● Pointer (*) and address-of (&)
● Scope resolution operator (::)
● Member access operator (. and ->)
4. Explain Scope Resolution Operator (::)
The :: operator is used to define or access variables/functions outside their current scope.
Uses:
● To access a global variable when a local variable has the same name
● To define a function outside the class
Example:
cpp
CopyEdit
int x = 10;
int main() {
int x = 5;
cout << ::x; // accesses global x
}
5. Explain Member Dereferencing Operator
Used with pointers to objects or structures.
● * (asterisk): Dereferences the pointer
● -> (arrow): Accesses members through a pointer
Example:
cpp
CopyEdit
struct Student {
int id;
};
Student s1, *ptr = &s1;
ptr->id = 101; // using -> operator
(*ptr).id = 101; // using * and . operator
6. Explain Memory Management Operator (new and delete)
Used for dynamic memory allocation and deallocation.
● new: Allocates memory on the heap
● delete: Frees the memory allocated by new
Example:
cpp
CopyEdit
int* ptr = new int; // allocates memory
*ptr = 50;
delete ptr; // frees memory
For arrays:
cpp
CopyEdit
int* arr = new int[5];
delete[] arr;
7. Explain Operator Precedence and Their Associativity
Operator precedence determines the order in which operators are evaluated in an
expression.
Associativity determines the direction (left-to-right or right-to-left) of operation if operators
have the same precedence.
Example:
cpp
CopyEdit
int a = 10 + 5 * 2; // * has higher precedence, so evaluated
first
Precedence Level Operators Associativity
Highest ++, --, Right to Left
()
Medium *, /, % Left to Right
Lower +, - Left to Right
Lowest =, +=, -= Right to Left
8. Explain Automatic Conversion (Implicit Conversion)
Implicit conversion is done automatically by the compiler when two different types of data
are used in an expression.
Example:
cpp
CopyEdit
int a = 10;
float b = 2.5;
float c = a + b; // int 'a' is converted to float
Rules:
● Lower to higher type
● No data loss
9. Explain Type Cast Operator (Explicit Conversion)
Explicit conversion is done manually using type casting.
Syntax:
cpp
CopyEdit
(type) expression
Example:
cpp
CopyEdit
int a = 10, b = 3;
float result = (float)a / b; // result = 3.33
Types:
● C-style: (float)a
● C++ style: static_cast<float>(a)
10. Explain List Expression and Their Types
A list expression is a sequence of values or variables separated by commas.
Used in:
● Multiple variable assignments
● For loops
● Function argument lists
Example:
cpp
CopyEdit
int a, b, c;
a = (b = 10, c = 20, b + c); // a = 30
Types:
● Expression list in function calls
● Initializer list in array or struct
● Comma operator expressions
©Reserved by HrishiOnlineBuddhi
Visit Our Terms & Condition for content usage Policy