0% found this document useful (0 votes)
27 views17 pages

Advance C CH 1

The document provides an overview of unions and enumerations in C programming, detailing how unions allow different data types to share the same memory location and how enumerations define a set of named constants. It includes examples of union and enumeration declarations, initialization, and usage in functions, alongside comparisons with structures. Additionally, it discusses the advantages of unions and the use of the typedef keyword for creating alternative names for data types.

Uploaded by

Hari Kalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views17 pages

Advance C CH 1

The document provides an overview of unions and enumerations in C programming, detailing how unions allow different data types to share the same memory location and how enumerations define a set of named constants. It includes examples of union and enumeration declarations, initialization, and usage in functions, alongside comparisons with structures. Additionally, it discusses the advantages of unions and the use of the typedef keyword for creating alternative names for data types.

Uploaded by

Hari Kalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Ch 1 : Union and Enumeration

• Union is a memory location that is shared by two or more different types of


variables. Declaring a union is similar to declaring a structures.
• Definition - A union is a special data type available in C that allows to store
different data types in same memory location. We can define a union with
many members, but only one member can contain a value at any given time.

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:-

• We can pass a union variable to a function as a parameter.


Example-
# include<stdio.h>
# include<stdlib.h>
union student
{ int rno; char nm[20]; int marks;}
void display(union student S1)
int main()
{ union student s;
Output:
display(s);
Roll no : 101
}
Name: suresh
void display(union student s1)
Marks:70
{ printf(“ Student detail”);
S1.rno=101;
Printf(“ Roll no: %d”, s1.rno);
Strcpy(s1.nm,”suresh” );
Printf(“ Name: %s”, s1.nm);
S1.marks=70;
Printf(“ Marks: %d”, s1.marks);
}
Initializing Union:- union are initialized with a brace-enclosed initializer that initializes only the
first member of union.
Ex:
union job
{
float salary;
int workerno;
}j={5000};

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

enum Level myVar;

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;

// Print the enum variable


printf("%d", myVar);

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy