Advance C CH 1
Advance C CH 1
Syntax:-
union <union-name>
{
Type member-name;
Type member-name;
……….
………..
………….
} union-variables;
Write a C program to accept details of book from user and display details using union
function
• #include <stdio.h>
• // Define a union for book details
• union book {
• char title[50];
• char author[50];
• char publisher[50];
• float price;
• };
• int main() {
• // Declare a union variable printf("Title: %s\n", b.title);
• union book b; printf("Author: %s\n", b.author);
• // Accept the details of the book from the user printf("Publisher: %s\n", b.publisher);
• printf("Enter the title of the book: ");
printf("Price: %.2f\n", b.price);
• scanf("%s", b.title);
return 0;
}
• printf("Enter the author of the book: ");
• scanf("%s", b.author);
• printf("Enter the publisher of the book: ");
• scanf("%s", b.publisher);
• printf("Enter the price of the book: ");
• scanf("%f", &b.price);
• // Display the details of the book
• printf("\nBook details:\n");
Difference between Structure and
Union
Initialization of union:- We can only
access a single variable at a time in
union.
• Example:- Output:-
#include<Stdio.h> Salary = 0.0
Number of worker = 100
union job
{ Float salary;
Note :- Salary is not printed bez only one member of union
Int workno; can be initialized at a time in union
} j;
Int main()
{
j.salary =12.3;
j.workno=100;
Printf(“Salary=%f\n”, j.salary);
Printf(“Number of workers=%d”, j.workno);
}
Nested union :- It is possible to have nested union i.e that is one union containing the another union inside. This
also be possible with Stucture also.
• #include <stdio.h>
union inner {
int x; char y;
};
union outer {
OutPut:-
float z; 10
union inner w; A
}; 3.14
int main() {
union outer u;
u.w.x = 10;
printf("%d\n", u.w.x);
u.w.y = 'a';
printf("%c\n", u.w.y);
u.z = 3.14;
printf("%.2f\n", u.z);
return 0;
}
Pointer to Union:- pointer is capable of storing the address of a
variable. Pointer which store address of union is called pointer to
union.
• Syntax:-
union student S; // Declaring union variable
union student *P; //Declaring union pointer
*P=&s; // Assigning address to union pointer
#include <stdio.h>
struct person
{
int age;
float weight;};
int main()
{
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr->age);
printf("Enter weight: ");
scanf("%f", &personPtr->weight);
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}
Ouput:-
• Enter age: 56
• Enter weight: 50
• Displaying:
• Age: 56
• weight: 50.000000
Union passing to function:-
• Advantage of union:-
• Efficient use of memory as it does not demand memory space for its all members
rather it require memory space for its largest member only.
• Same memory space can be use for different member of union.
• It can assign one union variable to another.
• It can pass union or pointer to union as function arg.
• Function can return union type.
• Also we can define pointer to union type object.
• Member of union can be access by using union variable.
Enumration:-It is user define
datatype.
An enum is a special type that represents
a group of constants (unchangeable values).
To create an enum, use the enum keyword,
followed by the name of the enum, and separate the enum
items with a comma: Note that the last item does not need a comma.
enum Level {
LOW,
MEDIUM,
HIGH
};
By default, the first item (LOW) has the value 0, the second
(MEDIUM) has the value 1, etc.
If you now try to print myVar, it will output 1, which
represents MEDIUM
• #include <stdio.h>
enum Level {
LOW,
MEDIUM,
HIGH
};
int main() {
// Create an enum variable and assign a value to it
enum Level myVar = MEDIUM;
return 0;
}
Output:-
#include <stdio.h>
enum Level {
LOW = 25,
MEDIUM = 50,
HIGH = 75
};
int main() {
enum Level myVar = MEDIUM;
printf("%d", myVar);
return 0;
}
Output:-
if you assign a value to one specific item, the next items will update their numbers accordingly:
#include <stdio.h>
enum Level {
LOW = 5,
MEDIUM,
HIGH
};
int main() {
enum Level myVar = MEDIUM;
printf("%d", myVar);
return 0;
}
OUTPUT:-
#include <stdio.h>
enum Level {
LOW = 1,
MEDIUM,
HIGH
OUTPUT:-
};
int main() {
enum Level myVar = MEDIUM;
switch (myVar) {
case 1:
printf("Low Level");
break;
case 2:
printf("Medium level");
break;
case 3:
printf("High level");
break;
}
return 0;
• typedef is a C keyword implemented to tell the compiler to assign alternative names to C's
pre-existing data types.
This keyword, typedef, is typically used with user-defined data types if the names of the
datatypes become
a bit convoluted or complicated for the programmer to obtain or use within the program.
The general format for implementing the typedef keyword is:
• Syntax:
• typedef <existing_names_of_datatype>
<alias__userGiven_name>;
#include<stdio.h>
#include<string.h>
typedef struct professor
{
char p_name[50];
int p_sal;
} prof;
void main(void)
{
prof pf;
printf("\n Enter Professor details: \n \n");
printf("\n Enter Professor name:\t");
scanf("% s", pf.p_name);
printf("\n Enter professor salary: \t");
scanf("% d", &pf.p_sal);
printf("\n Input done ! ");
}