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

week-10-prog

The document contains multiple C programs demonstrating various concepts such as creating a singly linked list, the differences between structures and unions, using bit-fields to read and print a date, and copying one structure variable to another. Each program includes code snippets and outputs that illustrate the functionality and behavior of the respective concepts. Overall, it serves as a practical guide for understanding fundamental C programming techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

week-10-prog

The document contains multiple C programs demonstrating various concepts such as creating a singly linked list, the differences between structures and unions, using bit-fields to read and print a date, and copying one structure variable to another. Each program includes code snippets and outputs that illustrate the functionality and behavior of the respective concepts. Overall, it serves as a practical guide for understanding fundamental C programming techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

/* Create and display a singly linked list using self-referential structure.

*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int main() {
// Create three nodes
struct node *head = NULL;
struct node *second = NULL;
struct node *third = NULL;
head = (struct node*)malloc(sizeof(struct node));
second = (struct node*)malloc(sizeof(struct node));
third = (struct node*)malloc(sizeof(struct node));
// Assign data to each node
head->data = 1;
second->data = 2;
third->data = 3;
// Link the nodes together
head->next = second;
second->next = third;
third->next = NULL;
// Traverse the linked list and display its contents
struct node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
return 0;
}

/* OUTPUT
123 */
/* C program to illustrate differences between structure and Union */

#include <stdio.h>
#include <string.h>
struct struct_example { // declaring structure
int integer;
float decimal;
char name[20];
};
union union_example { // declaring union
int integer;
float decimal;
char name[20];
};
int main()
{
struct struct_example s = { 18, 38, "aits-rajampet" };
union union_example u = { 18, 38, "AITS-RAJAMPET" };
printf("structure data:\n integer: %d\n"
"decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
printf("\nunion data:\n integer: %d\n"
"decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
printf("\nsizeof structure : %d\n", sizeof(s));
printf("sizeof union : %d\n", sizeof(u));
printf("\n Accessing all members at a time:");
s.integer = 183;
s.decimal = 90;
strcpy(s.name, "AITS");
printf("structure data:\n integer: %d\n "
"decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
u.integer = 183;
u.decimal = 90;
strcpy(u.name, "aits");
printf("\nunion data:\n integer: %d\n "
"decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
printf("\n Accessing one member at time:");
printf("\nstructure data:");
s.integer = 240;
printf("\ninteger: %d", s.integer);
s.decimal = 120;
printf("\ndecimal: %f", s.decimal);
strcpy(s.name, "C programming");
printf("\nname: %s\n", s.name);
printf("\n union data:");
u.integer = 240;
printf("\ninteger: %d", u.integer);
u.decimal = 120;
printf("\ndecimal: %f", u.decimal);
strcpy(u.name, "C programming");
printf("\nname: %s\n", u.name);
printf("\nAltering a member value:\n");
s.integer = 1218;
printf("structure data:\n integer: %d\n "
" decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
u.integer = 1218;
printf("union data:\n integer: %d\n"
" decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
return 0;
}
/* OUTPUT
structure data:
integer: 18
decimal: 38.00
name: aits-rajampet
union data:
integer: 18
decimal: 0.00
name: ↕
sizeof structure : 28
sizeof union : 20
Accessing all members at a time:structure data:
integer: 183
decimal: 90.00
name: AITS
union data:
integer: 1937008993
decimal: 19364284845316242000000000000000.00
name: aits
Accessing one member at time:
structure data:
integer: 240
decimal: 120.000000
name: C programming
union data:
integer: 240
decimal: 120.000000
name: C programming
Altering a member value:
structure data:
integer: 1218
decimal: 120.00
name: C programming
union data:
integer: 1218
decimal: 0.00
name: ┬♦
/*C Program Read and print a date using dd/mm/yyyy format using bit-fields */
#include <stdio.h>

// Define a structure with bit-fields for day, month, and year


struct Date {
unsigned int day : 6; // 1-31 can be represented with 6 bits
unsigned int month : 6; // 1-12 can be represented with 6 bits
unsigned int year : 14; // Using 14 bits for the year (you may adjust as needed)
};

int main() {
// Declare a variable of the Date structure
struct Date myDate;

// Temporary variables to store input


unsigned int tempDay, tempMonth, tempYear;

// Input the date from the user


printf("Enter date in dd/mm/yyyy format: ");
scanf("%u/%u/%u", &tempDay, &tempMonth, &tempYear);

// Assign the values to the bit-fields


myDate.day = tempDay;
myDate.month = tempMonth;
myDate.year = tempYear;

// Print the entered date


printf("Entered date: %02u/%02u/%04u\n", myDate.day, myDate.month,
myDate.year);

return 0;
}
Output:
Enter date in dd/mm/yyyy format: 20/12/2023
Entered date: 20/12/2023
/* Write a C program to copy one structure variable to another structure of the
same type. */
#include <stdio.h>
#include <string.h>
typedef struct {
char name[63];
enum { MALE, FEMALE, OTHER } gender;
int age;
float height;
} User;
void print_user(User *user) {
printf(
"(Name: %s, Gender: %s, age: %d, height %.1f)\n",
user->name,
user->gender == MALE ? "Male" :
user->gender == FEMALE ? "Female" : "Other",
user->age,
user->height
);
}
int main() {
User user = {"Naman", MALE, 21, 5}, user1;
strncpy(user1.name, user.name, sizeof(user.name));
// ^dest ^src ^size to copy
user1.gender = user.gender;
user1.age = user.age;
user1.height = user.height;
printf("user: "); print_user(&user);
printf("user copy: "); print_user(&user1);
return 0;
}
/* OUTPUT
user: (Name: Naman, Gender: Male, age: 21, height 5.0)
user copy: (Name: Naman, Gender: Male, age: 21, height 5.0) */

You might also like