Bca 06
Bca 06
element type 1;
element type 2;
} variable1, variable2, . .;
Operators The operator to access elements in a The operator to declare and access
Structure is the dot operator “ . “ elements in an Array is the square
bracket [ ]
Size Elements in a Structure can be of Elements in an Array are always of the
different sizes. same size.
Keyword The keyword “struct” is used to define Arrays do not require a keyword for
a Structure. declaration.
User-defined A Structure is a user-defined data type. An Array is not user-defined and can be
declared directly.
Q.B.2 Explain about the basic data type in C language with example.
b)Nested Structure
A nested structure in C is a structure within structure. One structure can be declared
inside another structure in the same way structure members are declared inside a structure.
Syntax:
struct name_1
{
member1;
member2;
.
.
membern;
struct name_2
{
member_1;
member_2;
.
.
member_n;
}, var1
} var2;
The member of a nested structure can be accessed using the following syntax:
Variable name of Outer_Structure.Variable name of Nested_Structure.data member to
access
Q.B.5. What is an algorithm? Write an algorithm to find largest number among three
numbers. Also draw flow chart for the same.
Section-C
(Long Answer Questions)
Note: Answer any two questions. You have to delimit your answer maximum up
to 500 words. Each question carries six marks. 2 x 6 = 12
Q.C.1. Differentiate between.
(a) Scanf( ) and gets( )?
The difference can be shown in tabular form as follows:
scanf() gets()
when scanf() is used to read string when gets() is used to read input it stops reading input
input it stops reading when it when it encounters newline or End Of File. It does not
encounters whitespace, newline or stop reading the input on encountering whitespace as it
End Of File considers whitespace as a string.
Syntax:
Formatted output syntax for the printf() function:
Play Video
Here, the argument list comprises the variables that will receive the input, and the format
string describes the format of the input.
Unformatted I/O in C
Unformatted I/O refers to a category of I/O operations that reads or writes data as a stream of
bytes without regard to any format. Unformatted I/O in C is carried out with the aid of
functions like fread() and fwrite(). Without formatting, these operations are used to read and
write data directly to and from files.
Syntax:
Syntax for using the fwrite() function to print unformatted data:
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)Relloc
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. re-allocation of memory maintains the already
present value and new blocks will be initialized with the default garbage value.
Syntax of realloc() in C
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'.
d)Free
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. Hence the free()
method is used, whenever the dynamic memory allocation takes place. It helps to reduce
wastage of memory by freeing it.
Syntax of free() in C
free(ptr);
// Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
// deallocating the memory
free(ptr);
return 0;
}