OOPS Remaining Topics
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.
There are different types of constants in C programming.
List of Constants in C :
Constant Example
Decimal Constant 10, 20, 450 etc.
Real or Floating-point 10.3, 20.2, 450.6 etc.
Constant
Octal Constant 021, 033, 046 etc.
Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.
Character Constant 'a', 'b', 'x' etc.
String Constant "c", "c program", "c in javatpoint" etc.
2 ways to define constant in C:
There are two ways to define constant in C programming.
1. const keyword
2. #define preprocessor
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 :
When a variable is declared as a reference, it becomes an alternative name
for an existing variable. A variable can be declared as a reference by
putting ‘&’ in the declaration.
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.
2) Avoiding a copy of large structures: Imagine a function that has to
receive a large object. If we pass it without reference, a new copy of it
is created which causes wastage of CPU time and memory. We can use
references to avoid this.
4. C Input and Output - printf()/scanf()
Input means to provide the program with some data to be used in it
and Output means to display data on the screen or write the data to a
printer or a file.
The C programming language provides standard library functions to read
any given input and display output on the console.
While dealing with input-output operations in C, we use the following two
streams:
Standard Input (stdin)
Standard Output (stdout)
Standard input or stdin is used for taking input and Standard
output or stdout is used for giving output. The functions used for standard
input and output are present in the stdio.h header file. Hence, to use those
functions, we need to include the stdio.h header file in our program, as
shown below.
#include<stdio.h>
C language offers us several built-in functions for performing input/output
operations. Following are the functions used for standard input and output:
1) printf() function - Show Output
2) scanf() function - Take Input
3) getchar() and putchar() function
4) gets() and puts() function
5. Library Functions:
1) string.h
Function Use
strlen calculates the length of string
strcat Appends one string at the end of another
strncat Appends first n characters of a string at the end of another
strcpy Copies a string into another
strncpy Copies first n characters of one string into another
strcmp Compares two strings
strncmp Compares first n characters of two strings
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
pow(x,y) x raised to power y (xy) pow(2,2) is 4.0
3) stdlib.h
Function Description
abs returns absolute value
div performs division
exit terminates a program
calloc allocates memroy at the runtime
free deallocates the memory
malloc allocates memory at the runtime
realloc reallocates the memory
6. Command Line Arguments :
The most important function of C/C++ is main() function. It is mostly
defined with a return type of int and without parameters :
int main() { /* ... */ }
We can also give command-line arguments in C and C++. Command-line
arguments are given after the name of the program in command-line shell
of Operating Systems.
To pass command line arguments, we typically define main() with two
arguments : first argument is the number of command line arguments and
second is list of command-line arguments.
int main(int argc, char *argv[]) { /* ... */ }
or
int main(int argc, char **argv) { /* ... */ }
argc (ARGument Count) is int and stores number of command-line
arguments passed by the user including the name of the program. So if we
pass a value to a program, value of argc would be 2 (one for argument and
one for program name)
The value of argc should be non negative.
argv(ARGument Vector) is array of character pointers listing all the
arguments.
If argc is greater than zero,the array elements from argv[0] to argv[argc-1]
will contain pointers to strings.
Argv[0] is the name of the program , After that till argv[argc-1] every
element is command -line arguments.
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;
}