unit5 - typedef to define new datatype bitfieds typedef is...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
‘ typedef ’ is a keyword,which allows you to specify a new name for a datatype which is already defined in c language program. Syntax: typedef <datatype> <newname> /* Re-defining int type as Integer type */ typedef int Integer; int main( ) { Interger a ,b , sub; a = 20,b = 10; sub = a - b; printf(“%d - %d = %d”, a, b, sub); } /* Defining structure with typedef to avoid repeated usage of struct keyword */ typedef struct { int hours; int minutes; } TIME ; int main( ) { TIME t1, t2 , *t; t = (TIME *) calloc (10, sizeof( TIME )); } typedef – to define new datatype bitfieds struct playcard { unsigned pips ; unsigned suit ; }; Above structure occupies 4 bytes of memory. But the member pips accepts a value between 1 to 13 and the member suit accepts any value of 0, 1, 2 and 3 . So we can create a more packed representation of above structure with bitfields. struct playcard { unsigned pips : 4; unsigned suit : 2; }; A bitfield is a set of adjacent bits within a single machine word. 4-bit field called pips that is capable of storing the 16 values 0 to 15, and a 2-bit field called suit that is capable of storing values 0, 1, 2, and 3. So the entire structure variable occupies only one byte. Note : arrays of bit fields and a pointer to address a bit field is not permitted.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Enumeration – a set of named integers, makes program more readable #include<stdio.h> int main() { int signal; printf ("\t\t\t MENU \n\t1.RED \n"); printf ("\t2.ORANGE\n\t3.GREEN \n“ ); printf ("\n\t Enter the signal : “ ); scanf (“%d”, &signal ); switch(signal) { case 1: printf(“\t Stop and Wait!"); break; case 2: printf(“\t Ready to start!"); break; case 3: printf(“\t Start and go!"); break; } } #include<stdio.h> enum color {RED = 1,ORANGE,GREEN }; int main() { enum color signal; printf ("\t\t\t MENU \n\t1.RED \n"); printf ("\t2.ORANGE\n\t3.GREEN\n"); printf ("\n\t Enter the signal : "); scanf ("%d", &signal); switch(signal) { case RED: printf("\t Stop and Wait!"); break; case ORANGE: printf("\t Ready to start!"); break; case GREEN: printf("\t Start and go!"); break; } } Declaration of enumeration : enum <enum_name> { member1, member2, …. …. …. } ; Example : enum option { YES, NO, CANCEL } ; By default YES has value 0, NO has value 1 and CANCEL has 2. enum direction { EAST = 1, SOUTH, WEST = 6, NORTH } ; Now EAST has value 1, SOUTH has value 2, WEST has value 6, and NORTH has value 7. Enumerated types can be converted implicitly or cast explicitly. int x = WEST ; /* Valid. x contains 6. */ enum direction y ; y = (enum direction ) 2 ; /* Valid. Y contains SOUTH */
Background image of page 2
scanf( ) and printf( ) functions read and write data which always uses the terminal (keyboard and screen) as the target. It becomes confusing and time consuming to use large volumes of data
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

unit5 - typedef to define new datatype bitfieds typedef is...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online