Unions A union is a custom data type used for storing several variables in the same memory space. Although you can access any of those variables at any.

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

CSC141- Introduction to Computer Programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures Spring 2013Programming and Data Structure1.
CMT Programming Software Applications
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
C Tokens Identifiers Keywords Constants Operators Special symbols.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Array, Structure and Union
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
Dr. Sajib Datta  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters as integers.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
Lecture 10 union, enumeration, bit
Chapter # 2 Part 2 Programs And data
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
Structures, Unions, Enumerations
Functions and Pointers
Lecture 7: Repeating a Known Number of Times
LESSON 3 IO, Variables and Operators
Input/output.
2.5 Another Java Application: Adding Integers
CSE1320 Loop Dr. Sajib Datta
TMF1414 Introduction to Programming
Visit for more Learning Resources
Module 2 Arrays and strings – example programs.
Pointers.
Functions and Pointers
Basic notes on pointers in C
DATA HANDLING.
Tejalal Choudhary “C Programming from Scratch” Pointers
Visit for more Learning Resources
Buy book Online -
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Pointers  Week 10.
Derived types.
Review of Arrays and Pointers
Chapter # 2 Part 2 Programs And data
Arrays Week 2.
In C Programming Language
C Programming Pointers
Lecture 14: Problems with Lots of Similar Data
DATA TYPES There are four basic data types associated with variables:
Course Outcomes of Programming In C (PIC) (17212, C203):
Introduction to C Programming
Structures, Unions, and Enumerations
Structures Declarations CSCI 230
Presentation transcript:

Unions A union is a custom data type used for storing several variables in the same memory space. Although you can access any of those variables at any time, you should only read from one of them at a time—assigning a value to one of them overwrites the values in the others.

Defining Unions define a union using the union keyword followed by the declarations of the union's members, enclosed in braces. declare a variable—using the data type followed by one or more variable names separated by commas, and ending with a semicolon. Then end the union definition with a semicolon after the closing brace.

Unions 14 ???? tag name // Declare a union union int_or_float { int i; float f; }; fields q i int int main() { union int_or_float q; q.i = 14; } 14 ???? f float 1/2/2019 3

Unions ???? 175.543 tag name // Declare a union union int_or_float { int i; float f; }; fields q i int int main() { union int_or_float q; q.i = 14; q.f = 175.543; } ???? 175.543 f float 1/2/2019

Unions 175 ???? tag name // Declare a union union int_or_float { int i; float f; }; fields q i int int main() { union int_or_float q; q.i = 14; q.f = 175.543; q.i = q.f; } 175 ???? f float 1/2/2019 5

Unions ???? 175.543 tag name // Declare a union union int_or_float { int i; float f; }; fields q i int int main() { union int_or_float q; q.i = 14; q.f = 175.543; } ???? 175.543 f float 1/2/2019 6

Unions 175 ???? tag name // Declare a union union int_or_float { int i; float f; }; fields q i int int main() { union int_or_float q; q.i = 14; q.f = 175.543; q.i = q.f; } 175 ???? f float 1/2/2019 7

union numbers { int i; float f; }; That defines a union named numbers, which contains two members, i and f, which are of type int and float, respectively.

Declaring Union Variables at Definition declare variables of a union type when you define the union type by putting the variable names after the closing brace of the union definition, but before the final semicolon union numbers { int i; float f; } first_number, second_number; That example declares two variables of type union numbers, first_number and second_number.

Declaring Union Variables After Definition You can declare variables of a union type after you define the union by using the union keyword and the name you gave the union type, followed by one or more variable names separated by commas. union numbers { int i; float f; }; union numbers first_number, second_number; That example declares two variables of type union numbers, first_number and second_number.

Initializing Union Members You can initialize the first member of a union variable when you declare it: union numbers { int i; float f; }; union numbers first_number = { 5 };

Another way to initialize a union member Union numbers first_number = { f: 3.14159 }; union numbers first_number = { .f = 3.14159 }; union numbers { int i; float f; } first_number = { 5 };

Accessing Union Members You can access the members of a union variable using the member access operator. union numbers { int i; float f; }; union numbers first_number; first_number.i = 5; first_number.f = 3.9; Notice in that example that giving a value to the f member overrides the value stored in the i member.

Size of Unions This size of a union is equal to the size of its largest member. Consider the first union example from this section: union numbers { int i; float f; }; The size of the union data type is the same as sizeof (float), because the float type is larger than the int type.

Example: #include<stdio Example: #include<stdio.h> Void main() { union a int I; char ch[2]; }; union a key; key.i=512; Printf(“\n key.i=%d”,key.i); Printf(“\n key.ch[0]=%d”,key.ch[0]); Printf(“\n key.ch[1]=%d”,key.ch[1]); }

Output: Key. i=512 Key. ch[0]=0 Key Output: Key.i=512 Key.ch[0]=0 Key.ch[1]=2 512 is an integer, a 2 byte number. Its binary equivalent will be 0000 0010 0000 0000 CPU’s follow little endian format- low byte stored before high byte.

Example: #include<stdio Example: #include<stdio.h> int main() { union a int i; char ch[2]; }; union a u; u.ch[0]=3; u.ch[1]=2; printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i); return 0; } Output: 3, 2, 515

#include<stdio.h> #include<conio.h> union marks { float perc; char grade; } void main () { union marks student1; student1.perc = 98.5; printf( "Marks are %f address is %16lu\n", student1.perc, &student1.perc); student1.grade = 'c'; printf("Grade is %c address is %16lu\n", student1.grade, &student1.grade);