0% found this document useful (0 votes)
4 views

Write a C Program to Implement Nested Structures.-...

The document contains various C programming examples, including the implementation of nested structures, complex number operations, and string handling functions. It also discusses key concepts such as bit fields, enums, typedefs, and command line arguments, alongside differences between structures, unions, and arrays. Additional programs demonstrate functionalities like palindrome checking, word counting, and printing strings in reverse order.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Write a C Program to Implement Nested Structures.-...

The document contains various C programming examples, including the implementation of nested structures, complex number operations, and string handling functions. It also discusses key concepts such as bit fields, enums, typedefs, and command line arguments, alongside differences between structures, unions, and arrays. Additional programs demonstrate functionalities like palindrome checking, word counting, and printing strings in reverse order.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

C Program to Implement Nested Structures:


#include <stdio.h>

struct Date {
int day;
int month;
int year;
};

struct Employee {
char name[50];
int id;
struct Date dob;
};

int main() {
struct Employee emp = {"John Doe", 123, {15, 7, 1990}};

printf("Employee Name: %s\n", emp.name);


printf("Employee ID: %d\n", emp.id);
printf("Date of Birth: %d-%d-%d\n", emp.dob.day, emp.dob.month,
emp.dob.year);

return 0;
}

2. C Program to Implement Addition and Multiplication of Two Complex Numbers:


#include <stdio.h>

struct Complex {
float real;
float imag;
};

struct Complex add(struct Complex a, struct Complex b) {


struct Complex result;
result.real = a.real + b.real;
result.imag = a.imag + b.imag;
return result;
}

struct Complex multiply(struct Complex a, struct Complex b) {


struct Complex result;
result.real = (a.real * b.real) - (a.imag * b.imag);
result.imag = (a.real * b.imag) + (a.imag * b.real);
return result;
}

int main() {
struct Complex num1 = {2.5, 3.0};
struct Complex num2 = {1.5, -2.0};

struct Complex sum = add(num1, num2);


struct Complex product = multiply(num1, num2);

printf("Sum: %.2f + %.2fi\n", sum.real, sum.imag);


printf("Product: %.2f + %.2fi\n", product.real, product.imag);

return 0;
}

3. Short Note on Bit Fields, enum, typedef, Command Line Arguments:


● Bit Fields: Allow you to define structure members with specific bit widths, saving
memory.
● enum: Defines a set of named integer constants.
● typedef: Creates a new name (alias) for an existing data type.
● Command Line Arguments: Values passed to a program when it's executed from the
command line.
4. C Program to Implement Array of Structures:
#include <stdio.h>

struct Student {
int rollno;
char name[50];
float marks;
};

int main() {
struct Student students[3];

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


printf("Enter details for student %d:\n", i+1);
printf("Roll No: ");
scanf("%d", &students[i].rollno);
printf("Name: ");
scanf("%s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
}

printf("\nStudent Details:\n");
for (int i = 0; i < 3; i++) {
printf("Roll No: %d, Name: %s, Marks: %.2f\n",
students[i].rollno, students[i].name, students[i].marks);
}

return 0;
}

5. Difference Between Structure and Union:


Feature Structure Union
Definition A collection of variables of A memory location that can
different data types grouped hold different data types at
under a single name. different times.
Declaration struct tag { member1; union tag { member1;
member2; ... }; member2; ... };
Defining struct tag variable1, variable2; union tag variable1, variable2;
Initialization Can initialize all members Can only initialize the first
member
Accessing Access members using . or -> Access members using . or ->
Memory Allocation Allocates memory for all Allocates memory for the
members largest member
Size Size is the sum of the sizes of Size is the size of the largest
all members member
6. Difference Between Structures, Unions, and Arrays:
● Structure:
○ Groups variables of different data types under a single name.
○ Members are stored in contiguous memory locations.
● Union:
○ Shares a single memory location among its members.
○ Only one member can be active at a time.
● Array:
○ A collection of elements of the same data type.
○ Elements are stored in contiguous memory locations.
7. Types of Structures with Examples:
● Simple Structure:
struct Point {
int x;
int y;
};

● Nested Structure: (See example in Question 1)


● Self-Referential Structure:
struct Node {
int data;
struct Node *next;
};

8. Structures with Functions, Arrays, and Pointers:


● Functions:
○ Pass structures to functions as arguments.
○ Return structures from functions.
● Arrays:
○ Create arrays of structures. (See example in Question 4)
● Pointers:
○ Use pointers to access and manipulate structure members.
9. Difference Between Arrays and Strings:
● Array:
○ Can hold elements of any data type.
○ Size is fixed at compile time.
● String:
○ An array of characters.
○ Usually terminated by a null character ('\0').
10. C Program to Check for Palindrome:
#include <stdio.h>
#include <string.h>

int isPalindrome(char *str) {


int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) {
return 0; // Not palindrome
}
}
return 1; // Palindrome
}

int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);

if (isPalindrome(str)) {
printf("%s is a palindrome\n", str);
} else {
printf("%s is not a palindrome\n", str);
}

return 0;
}

11. String Handling Functions in C:


● strlen(str): Returns the length of the string.
● strcpy(dest, src): Copies the source string to the destination string.
● strcat(dest, src): Concatenates the source string to the end of the destination string.
● strcmp(str1, str2): Compares two strings lexicographically.
● strchr(str, ch): Finds the first occurrence of a character in a string.
● strstr(str1, str2): Finds the first occurrence of a substring within a string.
● tolower(ch): Converts a character to lowercase.
● toupper(ch): Converts a character to uppercase.
12. C Program to Count Words, Lines, and Characters:
#include <stdio.h>

int main() {
char ch;
int words = 0, lines = 0, chars = 0;

printf("Enter text (Ctrl+D to end):\n");


while ((ch = getchar()) != EOF) {
chars++;
if (ch == '\n') {
lines++;
}
if (ch == ' ' || ch == '\n' || ch == '\t') {
words++;
}
}

printf("Words: %d\n", words);


printf("Lines: %d\n", lines);
printf("Characters: %d\n", chars);

return 0;
}

13. C Program to Print String in Reverse Order:


#include <stdio.h>
#include <string.h>

int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);

int len = strlen(str);


for (int i = len - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");

return 0;

You might also like