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

ST - 3 Tutorial 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

ST - 3 Tutorial 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Q.1 Explain the storage classes used for the storage of the data in C programming.

Ans: Storage Classes


Storage classes in the C language are used for determining the visibility, lifetime, initial value, and
memory location of any given variable. The storage classes define the visibility (scope) and the
lifetime of any function/ variable within a C program. These classes precede the type that they are
going to modify.

Use of Storage Class in C


A variable given in a C program will have two of the properties: storage class and type. Here, type
refers to any given variable’s data type, while the storage class determines that very variable’s
lifetime, visibility, and also its scope.

Types of Storage Classes in C


There are four different types of storage classes that we use in the C language:

 Automatic Storage Class


 External Storage Class
 Static Storage Class
 Register Storage Class
 There are four storage classes in C:
 Auto: This is the default storage class for all local variables. Variables declared within a
function without any storage class specifier automatically allocate memory on the stack and
have a local scope. Their lifetime is limited to the duration of the function call.
 Register: Variables declared with the register storage class are stored in CPU registers. This
can improve the program’s performance, as access to registers is faster than memory access.
However, the number of registers is limited, so the register should be used judiciously.
Variables declared with register have the same scope and lifetime as auto variables.
 Static: Variables declared with the static storage class are allocated memory in the data
segment of the program and retain their value between function calls. They have a local
scope, but their lifetime is the entire duration of the program. Functions can also be
declared as static, and these functions can only be called within the same source code file
where it is defined.
 Extern: Variables declared with the extern storage class are defined in one source code file
and can be used in other source code files. They are typically used to share global variables
between multiple source code files. The variable needs to be defined (not just declared) in
one source file with the keyword extern and in other files where they’re used.
Q.2 Differentiate between local and global variable with example.
Parameter Local Variables Global Variables
Defined outside of all
Definition Defined inside a function or block.
functions or blocks.
Accessible only within the function/block where Accessible throughout the
Scope it’s defined. entire program.
Remains in memory for
Lifetime Exists only during the function’s execution. the duration of the
program.
Can be accessed and
Accessibility Cannot be accessed outside its function. modified by any function
in the program.
Use the global keyword
Keyword for Modification No special keyword is required. to modify it inside a
function.
Stored in the data segment
Memory Storage Stored in the stack.
of memory.
Risk of Unintended Higher, as any function
Low, as it’s confined to its function.
Modification can modify it.

Q.3 Explain array of structure. Differentiate between structure and union.

Ans: An array whose elements are of type structure is called array of structure. It is generally
useful when we need multiple structure variables in our program.

Q.4 Write a C program to create a record of 10 students that includes student name, roll no.
and marks in 3 subjects. Display total marks of each student along with other details.
Ans

#include<stdio.h>

struct student

int rno;

char name[15];

int sub[3];
};

void main()

struct student s[10];

int i,j,k=0,total[3];

for(i=0;i<=2;i++)

printf("Enter Roll no:");

scanf("%d",&s[i].rno);

printf("Enter Name:");

scanf("%s",s[i].name);

printf("\nEnter Marks in Three Subjects = ");

total[k]=0;

for(j=0;j<3;j++)

scanf("%d",&s[i].sub[j]);

total[k]+=s[i].sub[j];

k=k+1;

k=0;

for(i=0;i<=2;i++)

printf("\nRoll no: %d",s[i].rno);


printf("\nName: %s",s[i].name);

printf("\n Marks in Three Subjects = ");

for(j=0;j<3;j++)

printf("%d",s[i].sub[j]);

printf("Total : %d",total[k]);

k++;

getch();

Q.5 Differentiate between actual and formal argument with example.


Actual Parameters Formal Parameters

The Actual parameters are the variables that are The Formal Parameters are the values determined by the
transferred to the function when it is requested. function that accepts values when the function is declared.

In actual parameters, only the variable is mentioned, In formal parameters, the data type is required.
not the data types.

Actual parameters are the values referenced in the Formal parameters are the values referenced in the parameter
parameter index of a subprogram call. index of a subprogram.

In actual parameters, there is no requirement to In formal parameters, it is mandatory to define the datatype
define datatype. of the receiving value.

These can be variables, constants, and expressions, Formal parameters are variables with the data type.
without data types.

Those parameters which are addressed in a function Parameters addressed in the function description are called
call are called actual parameters. formal parameters

Q6 Differentiate between call by value and call by reference. Write a C program to swap
values of two variables using call by value and call by reference.

Call By Value in C
In call by value method of parameter passing, the values of actual parameters are copied to the
function’s formal parameters.
There are two copies of parameters stored in different memory locations.
One is the original copy and the other is the function copy.
Any changes made inside functions are not reflected in the actual parameters of the caller.
// C program to illustrate call by value
#include <stdio.h>
// Function Prototype
void swapx(int x, int y);
// Main function
int main()
{
int a = 10, b = 20;
// Pass by Values
swapx(a, b); // Actual Parameters
printf("In the Caller:\na = %d b = %d\n", a, b);
return 0;
}

// Swap functions that swaps


// two values
void swapx(int x, int y) // Formal Parameters
{
int t;
t = x;
x = y;
y = t;
printf("Inside Function:\nx = %d y = %d\n", x, y);
}

Call by Reference in C
In call by reference method of parameter passing, the address of the actual parameters is
passed to the function as the formal parameters. In C, we use pointers to achieve call-by-
reference.
 Both the actual and formal parameters refer to the same locations.
 Any changes made inside the function are actually reflected in the actual parameters
of the caller.
 // C program to illustrate Call by Reference
#include <stdio.h>
// Function Prototype
void swapx(int*, int*);

// Main function
int main()
{
int a = 10, b = 20;

// Pass reference
swapx(&a, &b); // Actual Parameters

printf("Inside the Caller:\na = %d b = %d\n", a, b);

return 0;
}

// Function to swap two variables


// by references
void swapx(int* x, int* y) // Formal Parameters
{
int t;

t = *x;
*x = *y;
*y = t;

printf("Inside the Function:\nx = %d y = %d\n", *x, *y);


}

Q.7 Define recursion. Write a C program to print Fibonacci series up to nth term using
recursion.

Ans: Recursion: A function is recursive if a statement in the body of the function calls
itself. Recursion is the process of defining something in terms of itself. For a computer
language to be recursive, a function must be able to call itself.

For example,

fun()
{
fun();
}

void main()
{
fun();
}
C program to print Fibonacci series up to nth term using recursion

#include<stdio.h>
void printFibonacci(int m){
static int m1=0,m2=1,m3;
if(m>0){
m3 = m1 + m2;
m1 = m2;
m2 = m3;
printf(“%d “,m3);
printFibonacci(m-1);
}
}
int main(){
int m;
printf(“Please enter your preferred number of elements here: “);
scanf(“%d”,&m);
printf(“The Fibonacci Series will be: “);
printf(“%d %d “,0,1);
printFibonacci(m-2); //We have used m-2 because we have 2 numbers already printed here
return 0;
}

Q.8. Write a short note on pointer arithmetic. Write a C program to perform bubble sort using
pointers

We can perform arithmetic operations on the pointers like addition, subtraction, etc.
However, as we know that pointer contains the address, the result of an arithmetic operation
performed on the pointer will also be a pointer if the other operand is of type integer. In
pointer-from-pointer subtraction, the result will be an integer value. Following arithmetic
operations are possible on the pointer in C language:

o Increment
o Decrement
o Addition
o Subtraction
o Comparison
Incrementing Pointer in C

If we increment a pointer by 1, the pointer will start pointing to the immediate next location.
This is somewhat different from the general arithmetic since the value of the pointer will get
increased by the size of the data type to which the pointer is pointing.

The Rule to increment the pointer is given below:

new_address= current_address + i * size_of(data type)

Decrementing Pointer in C

Like increment, we can decrement a pointer variable. If we decrement a pointer, it will start
pointing to the previous location. The formula of decrementing the pointer is given below:

new_address= current_address - i * size_of(data type)

C Pointer Addition

We can add a value to the pointer variable. The formula of adding value to pointer is given
below:

new_address= current_address + (number * size_of(data type))

C Pointer Subtraction

Like pointer addition, we can subtract a value from the pointer variable. Subtracting any
number from a pointer will give an address. The formula of subtracting value from the
pointer variable is given below:

new_address= current_address - (number * size_of(data type))


#include <stdio.h>

int main() {
int n = 5,*p;
int arr[5] = {44, 33, 11, 22, 55};
//array of size 5
P=&a[0];

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - i - 1; j++) {
if (*(p+j) > *(p+j + 1)) {
//checking and swapping adjacent elements when left_elem > right_elem
int temp = *(p+j)
*(p+j) = *(p+j + 1);
*(p+j)= temp;
}
}
}
printf("Array after implementing Bubble sort: ");
for (int i = 0; i < n; i++) {
printf("%d ",*(p+i));
}
retur

Q.9 Explain five file handling functions with syntax. Write a C program to count number of
characters in a file.

There are many functions in the C library to open, read, write, search and close the file. A list
of file functions are given below:

No. Function Description

1 fopen() opens new or existing file

2 fprintf() write data into the file

3 fscanf() reads data from the file

4 fputc() writes a character into the file

5 fgetc() reads a character from file

6 fclose() closes the file

ptr = fopen("fileopen","mode");
fclose(fptr);
fscanf(fptr,"%d", &num);
fprintf(fptr,"%d", num);
putc(ch,fp);

1. #include<stdio.h>
2. #include<conio.h>
3. void main() {
4. char ch;
5. int count=0;
6. FILE *fptr1,*fptr2;
7. clrscr();
8. fptr1=fopen("text.txt","r");
9. if(fptr1==NULL) {
10. printf("File can't be created\a");
11. getch();
12. exit(0);
13. }
14.
15. Fptr2=fopen("text.txt","w");
16. printf("\ncopy Contents of the File is:");
17. while((ch=fgetc(fptr1))!=EOF) {
18. count++;
19. putc(ch,fptr2);
20. }
21. fclose(fptr1);
22. Fclose(fptr2);
23. printf("\nThe number of characters present in file is: %d",count);
24. getch();
25. }

Q.10 Differentiate between static and dynamic memory allocation. Explain malloc( ) function
in detail.
No Static Memory Allocation Dynamic Memory Allocation

1 When the allocation of memory performs at When the memory allocation is done at the
the compile time, then it is known as static execution or run time, then it is called dynamic
memory. memory allocation.

2 The memory is allocated at the compile time. The memory is allocated at the runtime.

3 In static memory allocation, while executing a In dynamic memory allocation, while executing a
program, the memory cannot be changed. program, the memory can be changed.

4 Static memory allocation is preferred in an Dynamic memory allocation is preferred in the


array. linked list.

5 It saves running time as it is fast. It is slower than static memory allocation.

6 Static memory allocation allots memory from Dynamic memory allocation allots memory from the
the stack. heap.

7 Once the memory is allotted, it will remain Here, the memory can be alloted at any time in the
from the beginning to end of the program. program.

8 Static memory allocation is less efficient as Dynamic memory allocation is more efficient as
compared to Dynamic memory allocation. compared to the Static memory allocation.

malloc()- A malloc is used to allocate a specified size of memory block at the run time of a
program. It means it creates a dynamic memory allocation at the run time when the
user/programmer does not know the amount of memory space is needed in the program.

Therefore, it inputs the memory size (in bytes) at the run time to reserve a contiguous
memory block that returns a pointer of type void, which is cast into a pointer of any form.
The dynamic memory is created using the malloc does not initialize the memory at execution
time, and hence the memory block contains some default garbage value. The malloc function
is defined inside the stdlib.h header file. So, we need to use <stdlib.h> header file while using
the malloc function in our program.

Syntax

ptr = ( cast_ type *) malloc (byte_size);

Program to allocate memory dynamically:

#include<stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
printf("Enter number of elements:");
scanf("%d",&n);
printf("\nAllocate memory for %d elements\n", n);

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


if (ptr == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
printf("Memory successfully allocated using malloc.\n");
printf("\n\nEnter %d elements",n);
for (i = 0; i < n; ++i)
{
ptr[i] = i + 1;
}

printf("\n\nThe elements of the array are: ");


for (i = 0; i < n; ++i)
{
printf("%d, ", ptr[i]);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy