Professional Documents
Culture Documents
Cpds RBT 2mrks - Unit 2
Cpds RBT 2mrks - Unit 2
U CO1
Structure is a user-defined datatype in C language which allows us to combine
data of different types together. Structure helps to construct a complex data type
which is more meaningful.
In structure, data is stored in form of records.
2.How to define a Structure? U
struct keyword is used to define a structure. struct defines a new data type
which is a collection of primary and derived data types.
Syntax
struct [structure_tag]
{
//member variable 1
//member variable 2
//member variable 3
...
}[structure_variables];
3.What is Union?
A union is a special data type available in C that allows to store different data
types in the same memory location. U
You can define a union with many members, but only one member can contain
a value at any given time. Unions provide an efficient way of using the same
memory location for multiple purpose.
4.Give the syntax for creating a union.
union [union name]
{
member
definition;
member
definition;
...
member definition; U
};
5.Difference between Structure and Union.
Structure Union
The Keyword struct is used to The Keyword union is used to define the
define the Structure Union
Structure allocates storage space Union allocates one storage space for all
for all its members seperately. its members. U
Structure occupies high memory Union occupies low memory space when
space compared to Structure
Arrays Pointers
U
Array allocates space automatically. Pointer is explicitly assigned to point to
an allocated space.
U
13. What are the steps in writing a function in a program?
Function Declaration (Prototype declaration): Every user-defined function has
to be declared before the main().
Function Calling: The user-defined functions can be called inside any
functions like main(), user-defined function, etc.
Function Definition: The function definition block is used to define the
user- defined functions with statements.
14. What is a use of ‘return’ Keyword? U
The ‘return’ Keyword is used only when a function returns a value.
15. What is the purpose of the function main()?
The function main () invokes other functions within it. It is the first function
to be called when the program starts execution.
Features of Main method U
o It is the starting function.
o It returns an int value to the environment that called the program.
o Recursive call is allowed for main () also.
o It is a user-defined function.
o Program execution ends when the closing brace of the function main()
is reached.
o It has two arguments (a) argument count and (b)argument vector
(represents strings passed.)
16. Compare between Array and Structure
Arrays Structures
U
An array is a collection of data items A structure is a collection of data items
of same data type. of different data types.
Arrays can only be declared. There is Structures can be declared and defined.
no keyword for arrays. The Keyword for structures is struct.
PART-B-15 Marks
int main() {
// Declare a variable of type "struct Student"
struct Student student1;
// Initialize the members of the "student1" structure
student1.studentID = 101;
strcpy(student1.name, "John Doe");
student1.age = 20;
student1.gpa = 3.8;
// Display the student information
printf("Student ID: %d\n", student1.studentID);
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("GPA: %.2f\n", student1.gpa);
return 0;
}
OUTPUT:
Student ID: 101
Name: John Doe
Age: 20
GPA: 3.80
int main() {
int num1 = 5, num2 = 10;
printf("After swapping (call by value): num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Output:
Before swapping: num1 = 5, num2 = 10
After swapping (call by value): num1 = 5, num2 = 10
Call by Reference:
In the call by reference method, a function receives references (memory addresses) to the
original variables. Any changes made to the parameters within the function affect the
original variables. In C, this can be achieved using pointers.
int main() {
int num1 = 5, num2 = 10;
return 0;
}
Output:
Before swapping: num1 = 5, num2 = 10
After swapping (call by reference): num1 = 10, num2 = 5
Writing to a File:
To write data to a file, you can use functions like fprintf or fputs.
FILE *filePointer;
filePointer = fopen("output.txt", "w"); // Opens the file in write mode
if (filePointer == NULL) {
printf("File could not be opened.\n");
} else {
fprintf(filePointer, "Hello, World!\n");
fclose(filePointer);
}
This code opens "output.txt" in write mode and writes the string "Hello, World!" to
it.
Closing a File:
Always remember to close the file when you're done using it with the fclose
function. Failing to close a file can lead to data loss or other issues.
FILE *filePointer;
filePointer = fopen("example.txt", "r");
// File operations go here
fclose(filePointer);
Error Handling:
It's essential to check if the file operations were successful. You can use conditional
statements to handle errors gracefully.
FILE *filePointer;
filePointer = fopen("example.txt", "r");
if (filePointer == NULL) {
printf("File could not be opened.\n");
} else {
// File operations go here
fclose(filePointer);
}
return 0;
}
Output:
Value of x: 10
Value of x via pointer: 10
In this program, we declare a pointer ptr and assign it the address of the integer variable x
using the & operator. We can access the value of x through the pointer using the * operator,
which is called dereferencing.
2. Pointers and Arrays:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr; // 'ptr' points to the first element of the array
return 0;
}
Output:
Element 0: 1
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
In this example, we declare an integer array arr and a pointer ptr. The pointer initially points to
the first element of the array. We use pointer arithmetic (ptr++) to traverse the elements of the
array.
3. Pointers and Functions:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
return 0;
}
Output:
Before swap: x = 5, y = 10
After swap: x = 10, y = 5