PPL Unit2
PPL Unit2
1) Scope:
A variable's scope is the region of a program where it can be
accessed, and its lifetime is the duration of time it exists in
memory:
Scope
The scope of a variable is determined by where it is declared in
the file. A variable's scope can be local or global.
A local variable is only accessible within the function or code
where it is declared.
A global variable is accessible throughout the entire program.
Lifetime
The lifetime of a variable is the amount of time it exists in
memory during program execution.
A variable's lifetime starts when it is allocated in memory and
ends when it is deallocated or the program terminates.
Local Scope of Variable:
#include <stdio.h>
int main()
{
int my_num = 7;
{
my_num = my_num +10;
printf("my_num is %d",my_num);
}
Explanation:
• The main() function has an integer variable my_num that's
initialized to 7 in the outer block.
• There's an inner block that tries to add 10 to the
variable my_num.
Output:
my_num is 17
Syntax:
int *ptr;
Example:
4) Arithmetic Expression:
1.Infix,Postfix,Prefix
Relational Expression:
Boolean Expression:
5) Data Types:
6) Control Structures:
9) Operator overloading
It is a programming technique that allows operators to have different
meanings depending on the arguments they are used with.
This is a type of polymorphism, and is also known as operator ad hoc
polymorphism.
Operator that cannot be overloaded are as follows:
o Scope operator (::)
o Sizeof()
o ternary operator(?:)
Function Overloading
Function Overloading is defined as the process of having two or more
function with the same name, but different in parameters is known as function
overloading in C++.
In function overloading, the function is redefined by using either
different types of arguments or a different number of arguments.
10) Type Checking
Type checking is the process of verifying that the types of all
variables, expressions, and functions are according to the rules of the
programming language.
It means operations operate on compatible data types, which
helps in preventing errors and hence ensures correctness of the
program.
Example:
#include <stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
printf("x = %d ", x);
return 0;
}
Output:
107
Explicit Type: