Untitled Document
Untitled Document
_Nested Structures:_
Suppose we want to determine the type of vehicle based on its make and model.
```
switch (make) {
case "Toyota":
switch (model) {
case "Camry":
printf("Vehicle type: Sedan\n");
break;
case "Corolla":
printf("Vehicle type: Compact Car\n");
break;
default:
printf("Unknown model\n");
}
break;
case "Honda":
switch (model) {
case "Civic":
printf("Vehicle type: Compact Car\n");
break;
case "Accord":
printf("Vehicle type: Sedan\n");
break;
default:
printf("Unknown model\n");
}
break;
default:
printf("Unknown make\n");
}
```
In this example, the inner switch statement checks the model of the vehicle, based on the
make. If the make is Toyota, it checks if the model is Camry or Corolla. If the make is Honda,
it checks if the model is Civic or Accord.
3) Describe how to pass structure to function with a example
_Passing a Structure to a Function:_
1. *Passing by Value:* The entire structure is passed to the function, and any changes made
to the structure within the function do not affect the original structure.
2. *Passing by Reference:* A pointer to the structure is passed to the function, and any
changes made to the structure within the function affect the original structure.
// Define a structure
struct Student {
char name[50];
int age;
float marks;
};
int main() {
// Create a structure variable
struct Student student1 = {"John Doe", 20, 85.50};
return 0;
}
```
In this example, the `displayStudent` function takes a `struct Student` as an argument and
displays the student's information. The `student1` structure is passed to the function by
value, which means that a copy of the structure is created and passed to the function.
```
#include <stdio.h>
// Define a structure
struct Student {
char name[50];
int age;
float marks;
};
int main() {
// Create a structure variable
struct Student student1 = {"John Doe", 20, 85.50};
return 0;
}
```
fopen(),fgets(),fput()
Here's an explanation of the file handling functions `fopen()`, `fgets()`, and `fputs()`:
fopen()
The `fopen()` function is used to open a file in a specific mode. It returns a file pointer that
can be used to perform various file operations.
_Return Value:_ If the file is opened successfully, `fopen()` returns a file pointer. Otherwise, it
returns `NULL`.
fgets()
The `fgets()` function is used to read a line of text from a file.
_Return Value:_ If a line of text is read successfully, `fgets()` returns the `str` pointer.
Otherwise, it returns `NULL`.
fputs()
The `fputs()` function is used to write a string of text to a file.
_Return Value:_ If the string of text is written successfully, `fputs()` returns a non-negative
value. Otherwise, it returns `EOF`.
Here's an example code snippet that demonstrates the use of these functions:
```
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w+");
if (file == NULL) {
perror("Error opening file");
return 1;
}
rewind(file);
char buffer[20];
fgets(buffer, sizeof(buffer), file);
printf("Read line: %s\n", buffer);
fclose(file);
return 0;
}
```
This code opens a file, writes a string of text to the file using `fputs()`, reads a line of text
from the file using `fgets()`, and prints the read line to the console.
Command line arguments are values passed to a program when it is executed from the
command line. These arguments can be used to customize the behavior of the program,
provide input data, or specify options.
```
program_name argument1 argument2 ... argumentN
```
In C, command line arguments can be accessed using the `argc` and `argv` parameters of
the `main()` function.
- `argc` (argument count) is an integer that represents the number of command line
arguments passed to the program, including the program name.
- `argv` (argument vector) is an array of character pointers that contains the command line
arguments as strings.
_Example Program:_
```
#include <stdio.h>
return 0;
}
```
_Let's Compile and Run the Program:_
```
#include <stdio.h>
#include <string.h>
int main() {
// Declare an array of 3 Employee structures
struct Employee employees[3];
return 0;
}
```
1. We define a `struct Employee` to store information about each employee, including their
name, age, and salary.
2. We declare an array `employees` to store information about 3 employees.
3. We define two functions: `inputEmployee` to input employee information and
`displayEmployee` to display employee information.
4. We use a loop to input information for each employee using the `inputEmployee` function.
5. We use another loop to display the information for each employee using the
`displayEmployee` function.
When you run this program, it will prompt you to input information for 3 employees and then
display the information for each employee.
.Write a c program to create a new file and perform write and read operations
Here is a simple C program that creates a new file, writes data to it, and then reads the data
back from the file:
```
#include <stdio.h>
#include <stdlib.h>
int main() {
// Create a new file
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
perror("Error creating file");
return 1;
}
return 0;
}
```
Here is a simple C program that copies the content of one file to another file:
```
#include <stdio.h>
#include <stdlib.h>
int main() {
// File names
char sourceFile[100];
char destinationFile[100];
// Close files
fclose(source);
fclose(destination);
printf("File copied successfully!\n");
return 0;
}
```
When you run this program, it will prompt you to enter the names of two files. It will then
copy the content of the first file to the second file.
What is a Structure?*
*Definition of a Structure*
A structure is defined using the `struct` keyword followed by the name of the structure and
the variables that make up the structure. The general syntax is:
```
struct structure_name {
data_type variable1;
data_type variable2;
...
data_type variableN;
};
```
For example:
```
struct Person {
int age;
char name[20];
float height;
};
```
*Initialization of Structure Variables*
1. *Direct Initialization*: You can initialize structure variables directly when you declare them.
```
struct Person person = {25, "John Doe", 175.5};
```
1. *Initialization using Designated Initializers*: You can use designated initializers to initialize
specific members of the structure.
```
struct Person person = {.age = 25, .name = "John Doe", .height = 175.5};
```
1. *Initialization using a Function*: You can write a function that initializes the structure
variables.
```
void initPerson(struct Person *p) {
p->age = 25;
strcpy(p->name, "John Doe");
p->height = 175.5;
}
You can access structure variables using the dot (`.`) operator or the arrow (`->`) operator.
1. *Dot Operator*: Use the dot operator to access structure variables when you have a
structure variable.
```
struct Person person;
person.age = 25;
```
1. *Arrow Operator*: Use the arrow operator to access structure variables when you have a
pointer to a structure.
```
struct Person person;
struct Person *p = &person;
p->age = 25;
```
In summary, structures are a way to group related variables together in C programming. You
can define a structure, initialize its variables, and access them using the dot or arrow
operator.