SlideShare a Scribd company logo
Embedded C – Part IV
Pre-processing & User defined data types
Team Emertxe
Preprocessors
Preprocessor: what ?
● Preprocessor is a powerful tool with raw power
●
Preprocessor is often provided as a separate
tool with the C compilers
●
Preprocessor is a program that processes
the code before it passes through the compiler
Preprocessor:
Functionalities
● Inclusion of header files
● Macro expansion
● Conditional compilation
● Removing comments from the source code
● Processing of trigraph sequence.
Preprocessor:
Built-in Defines
__FILE__ : Represents the current source file name in which it appears.
__LINE__ : Represents the current line number during the preprocessing in
the source file.
__DATE__ : Represents the current date during the preprocessing.
__TIME__ : Represents the current time at that point of preprocessing.
__STDC__ : This constant is defined to be true if the compiler conforms to
ANSI/ISO C standard.
__func__ : Represents the current function name in which it appears
Preprocessor:
Built-in Defines Example
Output
Preprocessor:
Directives
● #include
● #define
● #undef
● #ifdef, #ifndef, #else, #endif
● #error
● #line
● #pragma
● Preprocessing directives are lines in
your program that start with `#'.
● The `#' is followed by an identier that
is the directive name.
For example,
#include <stdio.h>
Directive:
#include
Copy of a specified file included in place of the directive
#include <filename>
● Searches standard library for file
● Use for standard library files
#include "filename"
● Searches current directory, then standard library
● Use for user-defined files
Used for:
● Programs with multiple source files to be compiled together
Header file – has common declarations and definitions
(classes, structures, function prototypes)
Directive:
#define
PI is a Macro
● This is used to create symbolic constants and macros
● When program is preprocessed, all occurrences of symbolic constant
replaced with replacement text
#define identifier replacement-textSyntax
Example #define PI 3.14159
Macros with Arguments
● Operation defined in #define
● A macro without arguments is treated like a symbolic constant
● A macro with arguments has its arguments substituted for replacement text,
when the macro is expanded
● Performs a text substitution – no data type checking
Example #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) )
Would cause
area = CIRCLE_AREA( 4 );
To become
area = ( 3.14159 * ( 4 ) * ( 4 ) );
Macros with Arguments
DIY
1. Write a macro to find max of two given nos.
2. Write a macro to set nth bit of given character.
Directive:#undefine,
#ifdef, #ifndef, #else, #endif
#include<stdio.h>
#define ABC 25
#ifdef ABC
#undef ABC
#define ABC 50
#else
#define ABC 100
#endif
int main()
{
printf("%d",ABC);
return 0;
}
Directive:
#error
Syntax #error tokens
●
Tokens are sequences of characters separated by spaces "Error cannot process"
has 3 tokens
● Displays a message including the specified tokens as an error message
● Stops preprocessing and prevents program compilation
Directive:
#error
Output ?
Directive:
#line
● Renumbers subsequent code lines, starting with integer value
● File name can be included
● #line 100 "myFile.c"
● Lines are numbered from 100 beginning with next source code file
● Compiler messages will think that the error occurred in "myfile.C"
● Makes errors more meaningful
● Line numbers do not appear in source file
Preprocessor Operators
# (stringization operator)
– Causes a replacement text token to be converted to a string surrounded
by quotes
– The statement
#define HELLO( x ) printf( “Hello, ” #x “n” );
would cause
HELLO( John )
to become
printf( “Hello, ” “John” “n” );
Strings separated by whitespace are concatenated
when using printf
Preprocessor Operators
## (concatenation operator)
● Concatenates two tokens
● The statement
#define TOKENCONCAT( x, y ) x ## y
would cause
TOKENCONCAT( O, K )
to become
OK
Preprocessor Operators
Example
Advantages &
Disadvantages
Macros are not type-checked
Examples:
int k = max_macro(i, j);
/* works with int */
float max_float = max_macro(10.0, 20.0);
/* also works with float constants */
int k = max_function(i, j);
/* works with int */
float max_float = max_function (10.0, 20.0);
/* does not work - you can pass only integral values */
Advantages &
Disadvantages
Macros have side effects during textual replacement whereas
functions does not have
Examples:
int k = max_macro (i++, j);
/* we are in trouble as i++ is evaluated twice */
/* int k = (i++ > j) ? i++ : j */
int k = max_function (i++, j);
/* no problem, as it is not expanded, but a call to max_function is made */
Macros might result in faster code as textual replacement is done
and no function call overhead is involved.
User Defined Datatypes
Structures
Structures:
What
What ?
● It is user defined data type.
● It is used to group together different types of variables under the same name.
Example
To create the student record, which consist of
✔ Name
✔ Roll number
✔ Age
✔ Marks etc...
Structures:
why
Why ?
● It helps to construct a complex data type in more meaningful manner.
● Organising the data in a better & efficient way.
Arrays & Structures
Arrays Structures
Collection of similar data types Collections of different data types
Structures:
Declaration
Declaration Example
DIY:
Declare the structure called date, which contains the memebers
day, month, year
Structures:
Declaration of variables
Declaration of structures variables can be done in two ways
● With structure declaration
● Using structure tag
With structure declaration Using structure tag
Declaring the structure variable reserves the
space in memory
Structures:
Initialization of variables
The number, order, type of these variables should be same
as in the structure template
With structure declaration
Using structure tags
Structures:
Initialization of variables
Members of the structures cannot be initialize while defining
the structure
Invalid Initialization
Structures:
Partial Initialization
Example
Structures:
Accessing the members
Format for accessing the members
variable.member
Example
s1.name
s1.age
s1.roll_num
Dot( . ) operator is also called as period or
membership operator
Structures:
Accessing the members
Format for accessing the members
variable.member
Example
s1.name
s1.age
s1.roll_num
Dot( . ) operator is also called as period or
membership operator
Structures:
Assignment of structure variables
We can assign values of a structure variable to another structure variable, if both
variables are of the same structure type.
Example
Structures:
Storage Allocation & Padding
Let us consider an example,
ch num
Padded bytes
Total : 8 bytes
sizeof() operator can be used to find the total size of this structure
4 bytes
Structures:
Array of structures
All the structures of an array are stored in consecutive
memory location
Example:
S [ 0 ] S [ 1 ] S [ 2 ]
name age roll_
num
marks name age roll_num marks name age roll_num marks
Kenneth 25 43 67.25 John 24 35 76.50 Richie 26 36 80.00
Total : 32 * 3 = 96 bytes of memory
Structures:
Nested structures
Structures within structure Example
Accessing the members within structure
var1.var2.member1 Example: dob.t.day;
Structures:
Pointers to structures
Example
Defining the pointer Accessing the members thru' ptr struct
student *sptr = &s1;
pointer -> member;
sptr->name, sptr->age
Use -> operator to access the members using pointers
syntax
Example
Structures:
Functions & structures
Passing structure members as Arguments
Calling function
Called function
Structures:
Functions & structures
Passing structure variables as Arguments
Calling function
Called function
Structures:
Functions & structures
Passing pointers to structures as Arguments
Calling function
Called function
Structures:
Functions & structures
Returning structure variable from function
Calling function
Called function
Structures:
Functions & structures
Returning a pointer to structure from function
Calling function
Called function
Structures:
Self referential structures
A structure that contains pointers to structures of its own type.
Syntax: Example:
struct tag
{
datatype member1;
datatype member2;
:
:
struct tag *ptr1;
struct tag *ptr2;
};
More details will be dealt in Data structures
Unions
Unions:
Introduction
●
User defined data type
●
The syntax used for declaration of a union, declaration of variables and
accessing the members are all similar to that of structures, except the keyword
'union' intead of 'struct'
●
The main difference between union & structures is the way the memory is
allocated for the members.
● In structures, each member has its own memory, whereas in the union members
share the same memory locations.
●
Compiler allocates sufficient memory to hold the largest member in the union.
Unions:
Examples
●
Example to compare the memory allocation for the members of the unions &
structures.
structures unions
sizeof(struct sample) = 12 bytes sizeof(union sample) = 4 bytes
Since highest data type among the
members is 'float'
Unions:
Union inside structure
Example
This structure has three members,
1. Name
2. Roll number
3. Union member performance
Union will take only one value at a
time, either a percent rounded of to
whole number or gpa
typedefs
typedefs:
Introduction
● The purpose of typedef is to form complex types from more-basic machine types
and assign simpler names to such combinations.
● They are most often used when a standard declaration is cumbersome, potentially
confusing, or likely to vary from one implementation to another.
● Under C convention (such as in the C standard library or POSIX), types declared
with typedef end with '_t' (e.g., size_t, time_t). Such type names are reserved by
POSIX for future extensions and should generally be avoided for user defined
types.
typedefs:
Examples
Syntax
typedef data_type new_name;
Identifier
Existing data type
Keyword
Example
typdef unsigned int long ul_t;
Now, ul_t type can be used to declare the variables of the type unsigned int long.
Typedefs & pointers
Examples
typedef int* iptr;
Now, iptr is synonym for the int * or pointer to int.
iptr p, q; // p, q are of type int*
iptr *p; // Here, p is declared as pointer to pointer to int
Typedefs & arrays
Examples
typedef int intarr [ 10 ];
Now, intarr is synonym for the integer array of size 10.
intarr a;
Above example is equivalent to int a [ 10 ];
intarr a [ 5 ];
Above example is equivalent to int a [ 5][ 10 ];
enums
Enums:
Introduction
● Set of named integer constants
Syntax:
enum tag { member1, member2, member3,...};
Example:
enum month { Jan, Feb, Mar, Apr, May, June};
● Internally the compiler treats these enumerators as integer constant.
● Enumerators are automatically assigned integer values begining from 0,1,2,...
● In the above example, Jan-0, Feb-1, Mar-2,...
Bit-fields
Bit-fields
Introduction
● A bit field is set up with a structure declaration that labels each field and
determines its width.
Syntax:
Example:
Stay connected
About us: Emertxe is India’s one of the top IT finishing schools & self learning
kits provider. Our primary focus is on Embedded with diversification focus on
Java, Oracle and Android areas
Branch Office: Corporate Headquarters:
Emertxe Information Technologies, Emertxe Information Technologies,
No-1, 9th Cross, 5th Main, 83, Farah Towers, 1st
Floor,
Jayamahal Extension, MG Road,
Bangalore, Karnataka 560046 Bangalore, Karnataka - 560001
T: +91 809 555 7333 (M), +91 80 41289576 (L)
E: training@emertxe.com
https://www.facebook.com/Emertxe https://twitter.com/EmertxeTweet https://www.slideshare.net/EmertxeSlides
THANK YOU

More Related Content

PDF
C programming Ms. Pranoti Doke
PDF
C programming notes
PPTX
Introduction to C programming
PPTX
C101 – Intro to Programming with C
PPTX
Presentation on C++ programming
PPT
Brief introduction to the c programming language
PDF
Learning the C Language
PPT
C language
C programming Ms. Pranoti Doke
C programming notes
Introduction to C programming
C101 – Intro to Programming with C
Presentation on C++ programming
Brief introduction to the c programming language
Learning the C Language
C language

What's hot (20)

PDF
Embedded SW Interview Questions
PPTX
PDF
Embedded C - Optimization techniques
PDF
Introduction to c programming
PPTX
System Programming Unit III
PPT
Lexical Analysis
PPT
Basics1
PPTX
Compiler design and lexical analyser
PDF
Compilers Design
PPT
Introduction to programming with c,
PPT
Embedded c program and programming structure for beginners
PPTX
System Programming Unit IV
PPTX
Aniket tore
PPTX
7 compiler lab
PPT
C language introduction
PPT
Embedded c programming22 for fdp
PDF
C programming part1
PPTX
PPTX
Intermediate code- generation
Embedded SW Interview Questions
Embedded C - Optimization techniques
Introduction to c programming
System Programming Unit III
Lexical Analysis
Basics1
Compiler design and lexical analyser
Compilers Design
Introduction to programming with c,
Embedded c program and programming structure for beginners
System Programming Unit IV
Aniket tore
7 compiler lab
C language introduction
Embedded c programming22 for fdp
C programming part1
Intermediate code- generation
Ad

Viewers also liked (20)

PDF
C Programming - Refresher - Part II
PDF
C Programming - Refresher - Part I
PDF
C Programming - Refresher - Part III
PDF
Embedded C - Lecture 1
PDF
Data Structures & Algorithm design using C
PDF
Communication Protocols (UART, SPI,I2C)
PDF
Function lecture
PPT
user defined function
PDF
best notes in c language
PPT
Cmp104 lec 7 algorithm and flowcharts
PDF
Advformat_0609
PDF
PDF
DVB_Arch
PDF
DIC_video_coding_standards_07
PDF
Discrete cosine transform
DOCX
A project on advanced C language
PDF
whitepaper_mpeg-if_understanding_mpeg4
C Programming - Refresher - Part II
C Programming - Refresher - Part I
C Programming - Refresher - Part III
Embedded C - Lecture 1
Data Structures & Algorithm design using C
Communication Protocols (UART, SPI,I2C)
Function lecture
user defined function
best notes in c language
Cmp104 lec 7 algorithm and flowcharts
Advformat_0609
DVB_Arch
DIC_video_coding_standards_07
Discrete cosine transform
A project on advanced C language
whitepaper_mpeg-if_understanding_mpeg4
Ad

Similar to C Programming - Refresher - Part IV (20)

PPTX
Presentation 5th
PDF
Unit 4 qba
PDF
C programming session7
PDF
C programming session7
PPT
cs8251 unit 1 ppt
PDF
C basic questions&amp;ansrs by shiva kumar kella
PDF
Structures
PPTX
Lecture 3.mte 407
PPTX
C++ tutorial assignment - 23MTS5730.pptx
PPSX
Programming in C [Module One]
PPTX
Technical Interview
PDF
C++ Interview Questions and Answers PDF By ScholarHat
PDF
C Programming Language Introduction and C Tokens.pdf
PPT
Oops lecture 1
PPTX
Structured Languages
PPT
C++ tutorials
PPTX
Programming in C sesion 2
PPTX
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
PDF
An Introduction To C++Templates
PPTX
programming for problem solving in C and C++.pptx
Presentation 5th
Unit 4 qba
C programming session7
C programming session7
cs8251 unit 1 ppt
C basic questions&amp;ansrs by shiva kumar kella
Structures
Lecture 3.mte 407
C++ tutorial assignment - 23MTS5730.pptx
Programming in C [Module One]
Technical Interview
C++ Interview Questions and Answers PDF By ScholarHat
C Programming Language Introduction and C Tokens.pdf
Oops lecture 1
Structured Languages
C++ tutorials
Programming in C sesion 2
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
An Introduction To C++Templates
programming for problem solving in C and C++.pptx

More from Emertxe Information Technologies Pvt Ltd (20)

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Empathic Computing: Creating Shared Understanding
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Approach and Philosophy of On baking technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Tartificialntelligence_presentation.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
A Presentation on Artificial Intelligence
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Empathic Computing: Creating Shared Understanding
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Programs and apps: productivity, graphics, security and other tools
Approach and Philosophy of On baking technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MIND Revenue Release Quarter 2 2025 Press Release
Tartificialntelligence_presentation.pptx
cuic standard and advanced reporting.pdf
MYSQL Presentation for SQL database connectivity
A Presentation on Artificial Intelligence
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Network Security Unit 5.pdf for BCA BBA.
Assigned Numbers - 2025 - Bluetooth® Document
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf

C Programming - Refresher - Part IV

  • 1. Embedded C – Part IV Pre-processing & User defined data types Team Emertxe
  • 3. Preprocessor: what ? ● Preprocessor is a powerful tool with raw power ● Preprocessor is often provided as a separate tool with the C compilers ● Preprocessor is a program that processes the code before it passes through the compiler
  • 4. Preprocessor: Functionalities ● Inclusion of header files ● Macro expansion ● Conditional compilation ● Removing comments from the source code ● Processing of trigraph sequence.
  • 5. Preprocessor: Built-in Defines __FILE__ : Represents the current source file name in which it appears. __LINE__ : Represents the current line number during the preprocessing in the source file. __DATE__ : Represents the current date during the preprocessing. __TIME__ : Represents the current time at that point of preprocessing. __STDC__ : This constant is defined to be true if the compiler conforms to ANSI/ISO C standard. __func__ : Represents the current function name in which it appears
  • 7. Preprocessor: Directives ● #include ● #define ● #undef ● #ifdef, #ifndef, #else, #endif ● #error ● #line ● #pragma ● Preprocessing directives are lines in your program that start with `#'. ● The `#' is followed by an identier that is the directive name. For example, #include <stdio.h>
  • 8. Directive: #include Copy of a specified file included in place of the directive #include <filename> ● Searches standard library for file ● Use for standard library files #include "filename" ● Searches current directory, then standard library ● Use for user-defined files Used for: ● Programs with multiple source files to be compiled together Header file – has common declarations and definitions (classes, structures, function prototypes)
  • 9. Directive: #define PI is a Macro ● This is used to create symbolic constants and macros ● When program is preprocessed, all occurrences of symbolic constant replaced with replacement text #define identifier replacement-textSyntax Example #define PI 3.14159
  • 10. Macros with Arguments ● Operation defined in #define ● A macro without arguments is treated like a symbolic constant ● A macro with arguments has its arguments substituted for replacement text, when the macro is expanded ● Performs a text substitution – no data type checking Example #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) Would cause area = CIRCLE_AREA( 4 ); To become area = ( 3.14159 * ( 4 ) * ( 4 ) );
  • 11. Macros with Arguments DIY 1. Write a macro to find max of two given nos. 2. Write a macro to set nth bit of given character.
  • 12. Directive:#undefine, #ifdef, #ifndef, #else, #endif #include<stdio.h> #define ABC 25 #ifdef ABC #undef ABC #define ABC 50 #else #define ABC 100 #endif int main() { printf("%d",ABC); return 0; }
  • 13. Directive: #error Syntax #error tokens ● Tokens are sequences of characters separated by spaces "Error cannot process" has 3 tokens ● Displays a message including the specified tokens as an error message ● Stops preprocessing and prevents program compilation
  • 15. Directive: #line ● Renumbers subsequent code lines, starting with integer value ● File name can be included ● #line 100 "myFile.c" ● Lines are numbered from 100 beginning with next source code file ● Compiler messages will think that the error occurred in "myfile.C" ● Makes errors more meaningful ● Line numbers do not appear in source file
  • 16. Preprocessor Operators # (stringization operator) – Causes a replacement text token to be converted to a string surrounded by quotes – The statement #define HELLO( x ) printf( “Hello, ” #x “n” ); would cause HELLO( John ) to become printf( “Hello, ” “John” “n” ); Strings separated by whitespace are concatenated when using printf
  • 17. Preprocessor Operators ## (concatenation operator) ● Concatenates two tokens ● The statement #define TOKENCONCAT( x, y ) x ## y would cause TOKENCONCAT( O, K ) to become OK
  • 19. Advantages & Disadvantages Macros are not type-checked Examples: int k = max_macro(i, j); /* works with int */ float max_float = max_macro(10.0, 20.0); /* also works with float constants */ int k = max_function(i, j); /* works with int */ float max_float = max_function (10.0, 20.0); /* does not work - you can pass only integral values */
  • 20. Advantages & Disadvantages Macros have side effects during textual replacement whereas functions does not have Examples: int k = max_macro (i++, j); /* we are in trouble as i++ is evaluated twice */ /* int k = (i++ > j) ? i++ : j */ int k = max_function (i++, j); /* no problem, as it is not expanded, but a call to max_function is made */ Macros might result in faster code as textual replacement is done and no function call overhead is involved.
  • 23. Structures: What What ? ● It is user defined data type. ● It is used to group together different types of variables under the same name. Example To create the student record, which consist of ✔ Name ✔ Roll number ✔ Age ✔ Marks etc...
  • 24. Structures: why Why ? ● It helps to construct a complex data type in more meaningful manner. ● Organising the data in a better & efficient way. Arrays & Structures Arrays Structures Collection of similar data types Collections of different data types
  • 25. Structures: Declaration Declaration Example DIY: Declare the structure called date, which contains the memebers day, month, year
  • 26. Structures: Declaration of variables Declaration of structures variables can be done in two ways ● With structure declaration ● Using structure tag With structure declaration Using structure tag Declaring the structure variable reserves the space in memory
  • 27. Structures: Initialization of variables The number, order, type of these variables should be same as in the structure template With structure declaration Using structure tags
  • 28. Structures: Initialization of variables Members of the structures cannot be initialize while defining the structure Invalid Initialization
  • 30. Structures: Accessing the members Format for accessing the members variable.member Example s1.name s1.age s1.roll_num Dot( . ) operator is also called as period or membership operator
  • 31. Structures: Accessing the members Format for accessing the members variable.member Example s1.name s1.age s1.roll_num Dot( . ) operator is also called as period or membership operator
  • 32. Structures: Assignment of structure variables We can assign values of a structure variable to another structure variable, if both variables are of the same structure type. Example
  • 33. Structures: Storage Allocation & Padding Let us consider an example, ch num Padded bytes Total : 8 bytes sizeof() operator can be used to find the total size of this structure 4 bytes
  • 34. Structures: Array of structures All the structures of an array are stored in consecutive memory location Example: S [ 0 ] S [ 1 ] S [ 2 ] name age roll_ num marks name age roll_num marks name age roll_num marks Kenneth 25 43 67.25 John 24 35 76.50 Richie 26 36 80.00 Total : 32 * 3 = 96 bytes of memory
  • 35. Structures: Nested structures Structures within structure Example Accessing the members within structure var1.var2.member1 Example: dob.t.day;
  • 36. Structures: Pointers to structures Example Defining the pointer Accessing the members thru' ptr struct student *sptr = &s1; pointer -> member; sptr->name, sptr->age Use -> operator to access the members using pointers syntax Example
  • 37. Structures: Functions & structures Passing structure members as Arguments Calling function Called function
  • 38. Structures: Functions & structures Passing structure variables as Arguments Calling function Called function
  • 39. Structures: Functions & structures Passing pointers to structures as Arguments Calling function Called function
  • 40. Structures: Functions & structures Returning structure variable from function Calling function Called function
  • 41. Structures: Functions & structures Returning a pointer to structure from function Calling function Called function
  • 42. Structures: Self referential structures A structure that contains pointers to structures of its own type. Syntax: Example: struct tag { datatype member1; datatype member2; : : struct tag *ptr1; struct tag *ptr2; }; More details will be dealt in Data structures
  • 44. Unions: Introduction ● User defined data type ● The syntax used for declaration of a union, declaration of variables and accessing the members are all similar to that of structures, except the keyword 'union' intead of 'struct' ● The main difference between union & structures is the way the memory is allocated for the members. ● In structures, each member has its own memory, whereas in the union members share the same memory locations. ● Compiler allocates sufficient memory to hold the largest member in the union.
  • 45. Unions: Examples ● Example to compare the memory allocation for the members of the unions & structures. structures unions sizeof(struct sample) = 12 bytes sizeof(union sample) = 4 bytes Since highest data type among the members is 'float'
  • 46. Unions: Union inside structure Example This structure has three members, 1. Name 2. Roll number 3. Union member performance Union will take only one value at a time, either a percent rounded of to whole number or gpa
  • 48. typedefs: Introduction ● The purpose of typedef is to form complex types from more-basic machine types and assign simpler names to such combinations. ● They are most often used when a standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another. ● Under C convention (such as in the C standard library or POSIX), types declared with typedef end with '_t' (e.g., size_t, time_t). Such type names are reserved by POSIX for future extensions and should generally be avoided for user defined types.
  • 49. typedefs: Examples Syntax typedef data_type new_name; Identifier Existing data type Keyword Example typdef unsigned int long ul_t; Now, ul_t type can be used to declare the variables of the type unsigned int long.
  • 50. Typedefs & pointers Examples typedef int* iptr; Now, iptr is synonym for the int * or pointer to int. iptr p, q; // p, q are of type int* iptr *p; // Here, p is declared as pointer to pointer to int
  • 51. Typedefs & arrays Examples typedef int intarr [ 10 ]; Now, intarr is synonym for the integer array of size 10. intarr a; Above example is equivalent to int a [ 10 ]; intarr a [ 5 ]; Above example is equivalent to int a [ 5][ 10 ];
  • 52. enums
  • 53. Enums: Introduction ● Set of named integer constants Syntax: enum tag { member1, member2, member3,...}; Example: enum month { Jan, Feb, Mar, Apr, May, June}; ● Internally the compiler treats these enumerators as integer constant. ● Enumerators are automatically assigned integer values begining from 0,1,2,... ● In the above example, Jan-0, Feb-1, Mar-2,...
  • 55. Bit-fields Introduction ● A bit field is set up with a structure declaration that labels each field and determines its width. Syntax: Example:
  • 56. Stay connected About us: Emertxe is India’s one of the top IT finishing schools & self learning kits provider. Our primary focus is on Embedded with diversification focus on Java, Oracle and Android areas Branch Office: Corporate Headquarters: Emertxe Information Technologies, Emertxe Information Technologies, No-1, 9th Cross, 5th Main, 83, Farah Towers, 1st Floor, Jayamahal Extension, MG Road, Bangalore, Karnataka 560046 Bangalore, Karnataka - 560001 T: +91 809 555 7333 (M), +91 80 41289576 (L) E: [email protected] https://www.facebook.com/Emertxe https://twitter.com/EmertxeTweet https://www.slideshare.net/EmertxeSlides