C Notes Sem 1 (3rd File)
C Notes Sem 1 (3rd File)
Inline functions are the function where definitions are small and substituted when
the function call occurs. The property of the inline function is that the compiler
inserts the entire body of the function in the place where inline function name is
used in the program.
Syntax:
inline function_name ()
//function definition
}
E.g.:
#include <stdio.h>
return (a * b);
int main ()
int p;
p = mul(3 , 6);
return 0;
}
Output:
The product of the numbers = 18
Q ) Explain scope and extent of a variable .
Ans :-
• Scope is defined as the availability of a variable inside a program, scope is basically the
region of code in which a variable is available to use.
• Lifetime or extent of a variable is the time for which the variable is taking up a valid
space in the system's memory .
1. Global Scope :-
File scope or global scope of variables in C is defined as having the availability of the variable
throughout the file/program. It means that the variable has a global scope, and it is available all
around for every function and every block in the program.
Example :
C Program :
#include <stdio.h>
void func() {
// x is available in func() function,
// x now equals 10 + 10 = 20
x += 10;
printf("Value of x is %d\n", x);
}
int main() {
func();
// x is also available in main() function
x += 30; // x now equals 20 + 30 = 50
printf("Value of x is %d", x);
return 0;
}
Output :
Value of x is 20
Value of x is 50
2. Local Scope :-
Block scope of variables in C is defined as when the variable has a limited scope, and the
memory occupied by the variable will be deleted once the execution of the block ends. The
variable is not accessible or available outside the block.
Example :
C Program :
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
function();
return 0;
}
void function()
{
printf(“%d”,a+b);// unable to access local variables a and b
}
Extent of a variable :-
Lifetime of a variable is defined as for how much time period a variable occupies a valid space in
the system's memory or lifetime is the period between when memory is allocated to hold the
variable and when it is freed. A variable in the C programming language can have a
a. Static Lifetime
Objects/Variables having static lifetime will remain in the memory until the execution of the
program finishes. Example :
b. Automatic Lifetime
Objects/Variables declared inside a block have automatic lifetime. Local variables (those defined
within a function) have an automatic lifetime by default: they arise when the function is invoked
and are deleted (together with their values) once the function execution finishes.
Example :
{ // block of code
int auto_lifetime_var = 0;
}
c. Dynamic Lifetime
Objects/Variables which are made during the run-time of a C program using the Dynamic
Memory Allocation
Example :
Q ) Discuss array in c.
dataType arrayName[arraySize];
For example,
float mark[5];
Suppose we declared an array mark as above. The first element is mark[0] , the
second element is mark[1] and so on.
Declare an Array
Ans :- In C programming language, a pointer is a variable that holds the address of another
variable. The type of this variable can be int, char, array, function, or any other pointer.
Consider the following example to create a pointer that stores an integer’s address.
int x = 10;
int *p = &x; //Variable p of type pointer is pointing to the address of the //variable n of type integer.
Advantages Of Pointer:
Disadvantages Of Poin
• If pointers are referenced with incorrect values, then it affects the whole program.
• If dynamically allocated memory is not released, a memory leak develops.
• An uninitialized pointer can cause a segmentation fault.
C malloc() method
Syntax:
free(ptr);
C realloc() method
Q) file handling in C .
Ans :-
A File is a collection of data stored in the secondary memory.
Files are not only used for storing the data, programs are also
stored in files
Types of Files in C
• Text Files
• Binary Files
Text Files: Text files contain data in the form of ASCII characters
Binary Files: Instead of ASCII characters, it is stored in binary format
Opening a file
The fopen() function is used to create a file or open an existing
file:
fp = fopen(const char filename,const char mode);
There are many modes for opening a file:
#include<stdio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen("hello.txt", "w");
printf("Enter data");
while( (ch = getchar()) != ‘^’) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("hello.txt", "r");
while( (ch = getc(fp)! = EOF)
printf("%c",ch);
fclose(fp);
}
Q ) preprocessor directive .
Ans :- Preprocessors are programs that process our source code before
compilation.
1. Macros
// macro definition
#define LIMIT 5
int main()
{
for (int i = 0; i < LIMIT; i++) {
printf(“%d\n”,i);
}
return 0;
}
Output
0
1
2
3
4
2. File Inclusion
This type of preprocessor directive tells the compiler to include a file in the source
code program.
#include< file_name >
3. Conditional Compilation
Q ) properties of c language .
Ans
The main features of c programming language are briefly noted below
1) Simple
C is a simple language in the sense that it provides a structured approach (to break the
problem into parts) .
Apple Delays Child Protection Features Over Concerns About User Privacy
4) Rich Library
C provides a lot of inbuilt functions that make the development fast.
5) Memory Management
It supports the feature of dynamic memory allocation. In C language, we can free the
allocated memory at any time by calling the free() function.
6) Recursion
In C, we can call the function within the function. It provides code reusability for every
function. Recursion enables us to use the approach of backtracking.