0% found this document useful (0 votes)
16 views

C Notes Sem 1 (3rd File)

Uploaded by

sujaymandalslg09
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

C Notes Sem 1 (3rd File)

Uploaded by

sujaymandalslg09
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Q ) what is inline function ?

Ans :- Inline Function in C

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>

inline int mul (int a, int b) //inline function declaration

return (a * b);

int main ()

int p;

p = mul(3 , 6);

printf (“ The product of the numbers = %d \n”, p);

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>

// variable with file scope


int x = 10;

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 :

static int count = 0;

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 :

int *ptr = (int *)malloc(sizeof(int));

Q ) Discuss array in c.

Ans :- An array is a variable that can store multiple values.It is homogeneous in


nature i.e it can store same kind of data.

How to declare an array?

dataType arrayName[arraySize];

For example,

float mark[5];

Here, we declared an array, mark , of floating-point type. And its size is 5.


Meaning, it can hold 5 floating-point values.

Access Array Elements


we can access elements of an array by indices.

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

How to initialize an array?


It is possible to initialize an array during declaration. For example,

int mark[5] = {19, 10, 8, 17, 9};

we can also initialize an array like this.

int mark[] = {19, 10, 8, 17, 9};

give a simple program related to array done in batch

Q ) Pointers in c with advantages and disadvantages .

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.

How to declare a Pointer?


The asterisk symbol( * ) can be used to declare a pointer in the C language (asterisk symbol). It
is sometimes referred to as an indirection pointer because it is used to dereference a pointer.

Here below example:


int *a;//pointer to int
char *c;//pointer to char
write any program related to pointer in batch 1

Advantages Of Pointer:

• It can be used to allocate memory dynamically .


• Using the pointer, we can return several values from a function.
• It allows you to access any memory address on the computer’s memory.

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.

Q ) explain calloc , malloc , realloc and free functions in c .


Ans :-
C provides some functions to achieve dynamic allocation and deallocation of
memory. The functions are defined under <stdlib.h> header file. They are:
1. malloc()
2. calloc()
3. free()
4. realloc()

C malloc() method

The “malloc” or “memory allocation” method in C is used to dynamically allocate a


single large block of memory with the specified size.
Syntax:
ptr = (cast-type*) malloc(byte-size);
For Example:
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory
C calloc() method

1. “calloc” or “contiguous allocation” method in C is used to dynamically


allocate the specified number of blocks of memory of the specified type. it
is very much similar to malloc() but has two different points and these are:
2. It initializes each block with a default value ‘0’.
3. It has two parameters or arguments as compare to malloc().
Syntax:
ptr = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-size is the size of each
element.
For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the
size of the float.
C free() method

“free” method in C is used to dynamically de-allocate the memory. The memory


allocated using functions malloc() and calloc() is not de-allocated on their own.

Syntax:
free(ptr);

C realloc() method

“realloc” or “re-allocation” method in C is used to dynamically change the memory


allocation of a previously allocated memory. In other words, if the memory
previously allocated with the help of malloc or calloc is insufficient, realloc can be
used to dynamically re-allocate memory. Syntax:
ptr = realloc(ptr, newSize);

where ptr is reallocated with new size 'newSize'.

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

Functions in File Operations:

C provides a number of build-in function to perform basic file


operations:
• fopen() - create a new file or open a existing file
• fclose() - close a file
• getc() - reads a character from a file
• putc() - writes a character to a file
• fseek() - set the position to desire point
• ftell() - gives current position in the file

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:

• r - open a file in read mode


• w - opens or create a text file in write mode
• a - opens a file in append mode
• r+ - opens a file in both read and write mode
• a+ - opens a file in both read and write mode
• w+ - opens a file in both read and write mode
Here’s an example of reading data from a file and writing to it:

#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.

There are 4 Main Types of Preprocessor Directives:


1. Macros
2. File Inclusion
3. Conditional Compilation.

1. Macros

• Macros are pieces of code in a program that is given some name.


Whenever this name is encountered by the compiler, the compiler
replaces the name with the actual piece of code. The ‘#define’ directive
is used to define a macro
#include <stdio.h>

// 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

Conditional Compilation directives are a type of directive that helps to compile a


specific portion of the program or to skip the compilation of some specific part of
the program based on some conditions. This can be done with the help of the two
preprocessing commands ‘ifdef‘ and ‘endif‘.
Syntax:
#ifdef macro_name
statement1;
statement2;
statement3;
.
.
.
statementN;
#endif

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) .

2) Mid-level programming language


Although, C is intended to do low-level programming. It is used to develop system
applications such as kernel, driver, etc. It also supports the features of a high-level
language. That is why it is known as mid-level language.

Apple Delays Child Protection Features Over Concerns About User Privacy

3) Structured programming language


C is a structured programming language in the sense that we can break the program
into parts using functions. So, it is easy to understand and modify. Functions also
provide code reusability.

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.

You might also like