Remaining Notes
Remaining Notes
Unit – 1
1. Scope and lifetime :
The scope of a declaration is the part of the program for which the declaration
is in effect.
The lifetime of a variable or object is the time period in which the
variable/object has valid memory. Lifetime is also called "allocation method"
or "storage duration."
Scope :
Local scope: "visible" within function or statement block from point of
declaration until the end of the block.
Class scope: "seen" by class members.
Namespace scope: visible within namespace block.
File scope: visible within current text file.
Global scope: visible everywhere unless "hidden".
Lifetime :
Static: A static variable is stored in the data segment of the "object file" of
a program. Its lifetime is the entire duration of the program's execution.
Automatic: An automatic variable has a lifetime that begins when program
execution enters the function or statement block or compound and ends
when execution leaves the block. Automatic variables are stored in a
"function call stack".
Dynamic: The lifetime of a dynamic object begins when memory is
allocated for the object (e.g., by a call to malloc() or using new) and ends
when memory is deallocated (e.g., by a call to free() or using delete).
Dynamic objects are stored in "the heap".
2. Constants :
A constant is a value or variable that can't be changed in the program, for
example: 10, 20, 'a', 3.4, "c programming" etc.
Constant Example
1) C const keyword:
The const keyword is used to define constant in C programming.
Example:
#include<stdio.h>
int main()
{
const float PI=3.14;
PI=4.5;
printf("The value of PI is: %f",PI);
return 0;
}
Output : Compilation error
2) C #define :
The #define preprocessor directive is used to define constant or micro
substitution. It can use any basic data type.
Example :
#include <stdio.h>
#define PI 3.14
void main()
{
printf("%f",PI);
}
Output : 3.140000
3. References :
Example :
#include <iostream>
using namespace std;
int main()
{
int x = 10;
// ref is a reference to x.
int& ref = x;
// Value of x is now changed to 20
ref = 20;
cout << "x = " << x << '\n';
// Value of x is now changed to 30
x = 30;
cout << "ref = " << ref << '\n';
return 0;
}
Output :
x = 20
ref = 30
Applications :
1) Modify the passed parameters in a function: If a function receives
a reference to a variable, it can modify the value of the variable.
#include<stdio.h>
1) string.h
Function Use
2) math.h
Function Description Example
sqrt(4.0) is 2.0
sqrt(x) square root of x
sqrt(10.0) is 3.162278
exp(1.0) is 2.718282
exp(x) exponential (ex)
exp(4.0) is 54.598150
log(2.0) is 0.693147
log(x) natural logarithm of x (base e)
log(4.0) is 1.386294
fabs(2.0) is 2.0
fabs(x) absolute value of x
fabs(-2.0) is 2.0
3) stdlib.h
Function Description
Example :
#include <iostream>
using namespace std;
int main(int argc, char** argv) // Command line arguments
{
cout << "You have entered " << argc<< " arguments:" << "\n";
for (int i = 0; i < argc; ++i)
cout << argv[i] << "\n";
return 0;
}