Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

1.What is a Structure in C?

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

We can access all Only one member of union can be


members of accessed at a time.
Structure at a time
Altering the value of a Altering the value of a member will
member will not affect other alter other member value in union.
member of a structure

6. What are Enumerated Datatypes?


U
 Enumeration (or enum) is a user defined data type in C. It is mainly used to
assign names to integral constants, the names make a program easy to read and
maintain.
7.What is pointer?
 A pointer is a variable that stores the memory address of another variable as its U
value. A pointer variable points to a data type (like int) of the same type and is
created with the * operator.
8.How addresses are assigned to Pointers? U
Example int* p, a; a= 8;
p = &a;
Here, 8 is assigned to the variable a and the address of a is assigned to the pointer p.

What are the uses of Pointers?


 Pointers are used to return more than one value to the function
 Pointers are more efficient in handling the data in arrays U
 Pointers reduce the length and complexity of the program
 They increase the execution speed
 The pointers saves data storage space in memory.
9.What is the difference between an array and pointer?

Arrays Pointers
U
Array allocates space automatically. Pointer is explicitly assigned to point to
an allocated space.

It cannot be resized. It can be resized using realloc ().


It cannot be reassigned. Pointers can be reassigned.
Sizeof(array name) gives the number of Sizeof(pointer name) returns the number
bytes occupied by the array. of bytes used to store the pointer variable.

10. What is dangling pointer?


 In C, a pointer may be used to hold the address of dynamically allocated
memory. After this memory is freed with the free() function, the pointer itself U
will still contain the address of the released block. This is referred to as a
dangling pointer.
 Using the pointer in this state is a serious programming error. Pointer should be
assigned NULL after freeing memory to avoid this bug.
11. What is ‘C’ functions?
 A function is a self-contained block (or) a sub-program of one or more
statements that performs a special task when called. U
 To perform a task repetitively then it is not necessary to re-write the particular
block of the program again and again. The function defined can be used for any
number of times to perform the task.
12. Differentiate library functions and User-defined functions.

Library Functions User-defined Functions U


The User-defined functions are the
Library functions are pre-defined set of functions defined by the user according to
functions that are defined in C libraries. his/her requirement.
User can only use the function but User can use this type of function.
cannot change (or) modify this function. User can also modify this function.

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.

An array name represents the address A structure name is known as tag. It is a


of the starting element. shorthand notation of the declaration.
An array cannot have bit fields. A structure may contain bit fields.
17. Is it better to use a macro or a function?
 Macros are more efficient (and faster) than function because their corresponding
code is inserted directly at the point where the macro is called. There is no
overhead involved in using a macro like there is in placing a call to a function.
U
 However, macros are generally small and cannot handle large, complex coding
constructs. In cases where large, complex constructs are to handled, functions
are more suited, additionally; macros are expanded inline, which means that the
code is replicated for each occurrence of a macro.
18. List the characteristics of Arrays.
 All elements of an array share the same name, and they are distinguished form
one another with help of an element number.
 Any element of an array can be modified separately without disturbing other
elements.
19. What are the types of Arrays? U
 One-Dimensional Array
 Two-Dimensional Array
 Multi-Dimensional Array
20. What is File Handling in C?
 A file is nothing but a source of storing information permanently in the form of
a sequence of bytes on a disk. The contents of a file are not volatile like the C U
compiler memory. The various operations available like creating a file,
opening a file, reading a file, or manipulating data inside a file is referred to as
file handling.
21. What is the need for File Handling in C?
 Reusability: It helps in preserving the data or information generated after U
running the program.
 Large storage capacity: Using files, you need not worry about the problem
of storing data in bulk.
 Saves time: There are certain programs that require a lot of input from the
user. You can easily access any part of the code with the help of certain
commands.
 Portability: You can easily transfer the contents of a file from one
computer system to another without having to worry about the loss of data.
22. List some of C File Handling Operations. U
 Creating a new file: fopen()
 Opening an existing file in your system: fopen()
 Closing a file: fclose()
 Reading characters from a line: getc()
 Writing characters in a file: putc()
 Reading a set of data from a file: fscanf()
 Writing a set of data in a file: fprintf()
 Reading an integral value from a file: getw()
23. Give the syntax for Opening a Text File U
in C.
Syntax
*fpointer = FILE *fopen(const char *file_name, const char *mode);
 *fpointer is the pointer to the file that establishes a connection between
the file and the program.
 *file_name is the name of the file.
 *mode is the mode in which we want to open our file.
24. How to Read and Write a Text File in C?
 The input/output operations in a file help you read and write in a file. U
 The simplest functions used while performing operations on reading and
writing characters in a file are getc() and putc() respectively.
 In order to read and write a set of data in a file, we use the fscanf() and fprintf()
operators.
25. Write short notes on Preprocessor Directives.
 The C preprocessor is a macro processor that is used automatically by the C U
compiler to transform your program before actual compilation (Preprocessor
directives are executed before compilation.).
 It is called a macro processor because it allows you to define macros, which are
brief abbreviations for longer constructs.
26. What is macro?
 A macro is a segment of code which is replaced by the value of macro. Macro is U
defined by #define directive.
27. List few preprocessor directives in C.
 #include
 Macro's (#define)
 #undef
 #ifdef
 #ifndef
 #if U
 #else

PART-B-15 Marks

1. Explain Structure in C with neat program.


In C, a structure is a composite data type that allows you to group together variables of U
different data types under a single name. Each variable inside a structure is called a "member"
or "field," and they can have different data types. Structures are commonly used to represent
records, objects, or entities with multiple attributes. Here's a simple explanation and an
example program demonstrating the use of structures in C:
#include <stdio.h>
// Define a structure named "Student" to represent student information
struct Student {
int studentID;
char name[50];
int age;
float gpa;
};

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

2. Explain call by value and call by reference with example programs.


Call by Value:
In the call by value method, a function receives a copy of the actual argument's value. Any
changes made to the parameters within the function do not affect the original variables
outside the function. This is the default behavior for most programming languages.
U
Here's an example program illustrating call by value:
#include <stdio.h>

// Function to swap two integers using call by value


void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
}

int main() {
int num1 = 5, num2 = 10;

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

// Call the function with num1 and num2


swapByValue(num1, num2);

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.

Here's an example program illustrating call by reference:


#include <stdio.h>

// Function to swap two integers using call by reference


void swapByReference(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int num1 = 5, num2 = 10;

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

// Call the function with references to num1 and num2


swapByReference(&num1, &num2);
printf("After swapping (call by reference): num1 = %d, num2 = %d\n", num1, num2);

return 0;
}
Output:
Before swapping: num1 = 5, num2 = 10
After swapping (call by reference): num1 = 10, num2 = 5

3. Explain the file handling mechanism in C with programs.


Opening a File:
To work with a file, you first need to open it using the fopen function. The fopen
function returns a pointer to a FILE structure, which is used for subsequent file
operations.
FILE *filePointer;
filePointer = fopen("example.txt", "r"); // Opens the file in read mode
In the above code, "example.txt" is the name of the file you want to open, and "r" is
the mode (read mode in this case). Modes include "r" for reading, "w" for
writing (creates a new file or overwrites an existing one), and "a" for appending
(creates a new file or appends to an existing one), among others.

Reading from a File:


You can use functions like fscanf or fgets to read data from a file.
FILE *filePointer;
filePointer = fopen("example.txt", "r");
if (filePointer == NULL) {
printf("File could not be opened.\n");
} else {
char buffer[100];
while (fgets(buffer, sizeof(buffer), filePointer) != NULL) {
printf("%s", buffer);
}
fclose(filePointer);
}
This code opens "example.txt" in read mode and reads and prints its contents line by
line.

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);
}

4. Explain preprocessor directives with its types and examples.


1. #include Directive (File Inclusion):
This directive is used to include header files in your code. Header files contain declarations of
functions, variables, and macros that your program uses.
Example:
#include <stdio.h> // Includes the standard I/O header file
2. #define Directive (Macro Definition):
This directive is used to create macros, which are symbolic names for expressions, constants,
or code fragments.
Example:
#define MAX_VALUE 100 // Defines a macro MAX_VALUE with the value 100
3. #ifdef, #ifndef, #else, and #endif Directives (Conditional Compilation):
These directives are used to conditionally compile code based on whether a certain macro is
defined (#define) or not (#undef).
Example:
#ifdef DEBUG
// Code to include only if DEBUG macro is defined
#else
// Code to include if DEBUG macro is not defined
#endif
4. #pragma Directive:
This directive is used to provide compiler-specific instructions or control over compiler
behavior.
Example (compiler-specific):
#pragma warning(disable : 4996) // Disable a specific warning in MSVC
5. #error Directive:
This directive is used to generate a compiler error message with a custom error message.
Example:
#ifdef OLD_VERSION
#error "You are using an outdated version of the software."
#endif
6. #line Directive:
This directive allows you to control line numbers and file names reported by the compiler for
debugging purposes.
Example:
#line 42 "mycode.c" // Set the current line number and file name for the compiler
7. #undef Directive:
This directive is used to undefine a previously defined macro.
Example:
#define PI 3.14159265
#undef PI // Undefine the PI macro

5.Explain the concept of pointers with neat programs.


Pointers are a fundamental concept in C and C++ programming that allow you to work with
memory addresses directly. They are variables that store the memory addresses of other
variables, rather than their values. Understanding pointers is crucial for efficient memory
management and working with complex data structures. Here, I'll explain the concept of
pointers with some sample programs.

1. Declaring and Using Pointers:


#include <stdio.h>
int main() {
int x = 10;
int *ptr; // Declare a pointer to an integer

ptr = &x; // Assign the address of 'x' to the pointer 'ptr'

printf("Value of x: %d\n", x);


printf("Value of x via pointer: %d\n", *ptr); // Dereference the pointer to access 'x'

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

for (int i = 0; i < 5; i++) {


printf("Element %d: %d\n", i, *ptr);
ptr++; // Move the pointer to the next element
}

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;

printf("Before swap: x = %d, y = %d\n", x, y);


swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);

return 0;
}
Output:
Before swap: x = 5, y = 10
After swap: x = 10, y = 5

You might also like