0% found this document useful (0 votes)
5 views4 pages

SPL-2021 Section B

The document provides an overview of C programming concepts, including structure definitions, nested structures, and the benefits of using pointers over static arrays. It also discusses memory allocation types, file operations, and the structure of a C program, highlighting modular programming and identifiers. Additionally, it includes code examples for practical understanding of these concepts.

Uploaded by

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

SPL-2021 Section B

The document provides an overview of C programming concepts, including structure definitions, nested structures, and the benefits of using pointers over static arrays. It also discusses memory allocation types, file operations, and the structure of a C program, highlighting modular programming and identifiers. Additionally, it includes code examples for practical understanding of these concepts.

Uploaded by

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

1. (a) Write the general format of a structure definition and declaration.

When is the use of tag


name optional?
Answer:
Structure definition:
struct structName{
dataType member1;
dataType member2;
};
Structure declaration:
struct structName instanceName;
If we use ‘typedef’ to define a new type based on structure, we can declare variables without
explicitly mentioning the ‘struct’ tag name.

(b) What is nested structure?


Answer:
A nested structure in C refers to a structure that is defined within another structure. This means
a structure can contain members that are themselves structures.
struct Address{
char city[50];
char street[50];
};
struct Person{
char name[50];
int age;
struct Address address; // Nested structure
};

(c ) “Two variables of the same structure can be copied/assigned one into another” – show
with an example.
Answer:
#include<stdio.h>
#include<string.h>
struct Person{
char name[50];
int age;
float height;
};
Int main()
{
struct Person person1 = {“John Doe”,22,5.5};
struct Person person2 ;
strcpy(person2.name,person1.name);
person2.age = person1.age;
person2.height = person1.height;
}

2. (a) What is the benefit of using a pointer instead of using a static array? Define pass by value
and pass by reference.
Answer:
There are several benefits of using a pointer instead of using a static array. They are:
i) Dynamic memory allocation: Pointers allow dynamic memory allocation.
ii) Dynamic resizing: Pointers allow us to resize the allocated memory block dynamically
using functions like ‘realloc()’.
iii) Reduce memory consumption: Using pointers can reduce memory consumptions
compared to static arrays.
iv) Data structure implementation: Complex data structures like linked lists, trees, graphs
etc. are implemented using pointers.
(b) Are the following declarations represents the same?
char some[10]; and char *some;
Answer:
In C, ‘char some[10]’ declares an array capable of holding 10 characters, allocating fixed memory
for immediate storage. Conversely, ‘char *some’ declares a character pointer, initially
uninitialized, serving as a reference that can be assigned to pointe to a character or a sequence
of characters dynamically allocated elsewhere in the code.

(c ) What are the basic operations of a file?


Answer:
Some basic operations of a file are:
i) Opening a file: It allows the program to read from or write to the file.
ii) Reading from a file: Reading involves retrieving data from a file.
iii) Writing to a file: Writing operations involve adding data to a file.
iv) Closing a file: After reading from or writing to a file, it is essential to close the file to
release system resources associated with it.
3. (a) Compare static and dynamic memory allocation. How to reallocate the size of an already
allocated memory space?
Answer:
Comparison between static and dynamic memory allocation:
Static Memory Allocation Dynamic Memory Allocation
It refers to allocating memory for variables at It refers to allocating memory at run times.
compile time.
Memory for variables are allocated on the Memory is allocated on the heap section.
stack memory.
The size and the scope of the memory are Allows for flexible memory usage during
fixed and determined before runtime. program execution.
Reallocating Memory Space:

In C, the ‘reallco()’ function is used to reallocate memory for already allocated block of memory.

It takes two arguments: the pointer to the previously allocated memory block and the new size.

It may resize the memory block:

i) If the new size is smaller, it may truncate the block, and the excess data is lost.
ii) If the new size is larger, it may expand the block or move it to a new location if
necessary.

Example:

int* ptr = (int*) malloc(5*sizeof(int)); // allocating memory for the first time

ptr = (int*)reallco(ptr,10*sizeof(int)); // reallocating for the same pointer

(b) What is the difference between the preprocessor directives #include”filename” and
#include<filename>
Answer:
#include”filename”, searches for the file in the current directory and then in system directories,
while #include<filename> looks only in system directories. The former is used for user defined
headers, and the latter is for system or standard library headers.

(C) Write a program to add the elements of an array using dynamic memory allocation.

Answer:

#include<stdio.h>

#include<stdlib.h>

int main()

int n,sum = 0;

printf("Enter the number of element in the array : ");

scanf("%d",&n);

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

printf("Enter the integer numbers: \n");

for(int i = 0;i<n;i++)

{
printf("array[%d] : ",i);

scanf("%d",&array[i]);

sum += array[i];

printf("The summation is : %d\n",sum);

free(array);

4. (a) Write short notes on :

i) Structure of a C program.

Answer:

A C program comprises sections for comments and details, linking directives, global
declarations, the main function as the entry point, and additional functions. It begins
with comments, includes linking directives, declares constants and variables, and
organizes logic within the main function and other subroutines.

iii) Identifires

Answer:

Identifiers in C are names for program elements like variables and functions. They start
with letters or underscores, followed by letters, digits, or underscores. Case-sensitive
and non-keyword, well-chosen identifiers enhance code clarity and understanding.

iv) Features and modular programming

Answer:

Modular programming in C involves breaking down programs into smaller, reusable


modules. It provides abstraction, encapsulation, and reusability, allowing clearer
organization, easy maintenance, and efficient reuse of code components across projects

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