ST - 3 Tutorial 1
ST - 3 Tutorial 1
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()
int i,j,k=0,total[3];
for(i=0;i<=2;i++)
scanf("%d",&s[i].rno);
printf("Enter Name:");
scanf("%s",s[i].name);
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++)
for(j=0;j<3;j++)
printf("%d",s[i].sub[j]);
printf("Total : %d",total[k]);
k++;
getch();
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;
}
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
return 0;
}
t = *x;
*x = *y;
*y = t;
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.
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:
C Pointer Addition
We can add a value to the pointer variable. The formula of adding value to pointer is given
below:
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:
int main() {
int n = 5,*p;
int arr[5] = {44, 33, 11, 22, 55};
//array of size 5
P=&a[0];
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:
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.
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
#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);